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;
}
#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
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