#include<iostream>
using namespace std;
class Student{ //Base / Parent class
int roll; // Private member will not inherit
protected: // if private member needs to inherit
char name[30];
public:
void get_data() {
cout << “Enter roll: “;
cin >> roll;
}
void show_data();
};
void Student::show_data(){
cout << “–Student info– “<<endl;
cout << “Roll: ” << roll << endl;
}
class Result: public Student
{// Child/derived class: Single level inheritance
private:
float total, marks[3];
public:
void get_info(){
get_data();
cout<<“Enter student name: “;
cin>>name; // Protected member of parent class
cout<<“Enter marks of s1, s2 and s3:”;
cin>>marks[0];
cin>>marks[1];
cin>>marks[2];
calc_total();
}
void calc_total(){
total = marks[0] + marks[1] + marks[2];
}
void show_result(){
show_data();
cout<<“Name: “<<name<<endl;
cout<<“Total marks: “<<total;
}
};
int main(){
Result r1;
r1.get_info();
r1.show_result();
return 0;
}

Recent Comments