This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Tuesday, February 28, 2023

Create a class student that stores name and roll. From this class, derive a class marks that stores marks for 3 subjects. Then from the class marks derive a class record which stores semester and average marks for 3 subjects. Create an object for class record and display name, roll, marks in 3 subjects, semester, and average marks for a student. Assume appropriate data types.

 Here is an example program that implements the above scenario:

#include <iostream>
#include <string>
using namespace std;
class Student {
protected:
    string name;
    int roll;
public:
    Student(string n, int r) {
        name = n;
        roll = r;
    }
};
class Marks : public Student {
protected:
    int sub1, sub2, sub3;
public:
    Marks(string n, int r, int s1, int s2, int s3) : Student(n, r) {
        sub1 = s1;
        sub2 = s2;
        sub3 = s3;
    }
};
class Record : public Marks {
protected:
    int semester;
public:
    Record(string n, int r, int s1, int s2, int s3, int sem) : Marks(n, r, s1, s2, s3) {
        semester = sem;
    }
    float getAverage() {
        return (sub1 + sub2 + sub3) / 3.0;
    }
    void display() {
        cout << "Name: " << name << endl;
        cout << "Roll: " << roll << endl;
        cout << "Marks in Subject 1: " << sub1 << endl;
        cout << "Marks in Subject 2: " << sub2 << endl;
        cout << "Marks in Subject 3: " << sub3 << endl;
        cout << "Semester: " << semester << endl;
        cout << "Average Marks: " << getAverage() << endl;
    }
};
int main() {
    Record r("John", 123, 85, 90, 95, 1);
    r.display();
    return 0;
}

Output:

Name: John
Roll: 123
Marks in Subject 1: 85
Marks in Subject 2: 90
Marks in Subject 3: 95
Semester: 1
Average Marks: 90

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget