Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.
Example
#include <iostream>
using namespace std;
// Base class: Student
class Student {
private:
int id;
char name[20];
public:
void setstudent()
{
cout<<"\n Enter student Id:";
cin>>id;
cout<<"\n Enter student Name:";
cin>>name;
}
void getstudent()
{
cout<<"\n *******Student Details******** \n";
cout<<"Id="<<id<<endl;
cout<<"Name="<<name<<endl;
}
};
// Base class: Marks
class Marks
{
protected:
int subj1,subj2,subj3;
public:
void setmarks()
{
cout<<"\n Enter 3 subjects marks";
cin>>subj1>>subj2>>subj3;
}
void getmarks()
{
cout<<"Mark of subject 1="<<subj1<<endl;
cout<<"Mark of subject 2="<<subj2<<endl;
cout<<"Mark of subject 3="<<subj3<<endl;
}
};
// Derived class: Result
class Result : public Student,public Marks
{
private:
int total;
float average;
public:
void getresult()
{
total = subj1 + subj2 + subj3;
average = total / 3.0;
cout << "Total Marks: " << total << endl;
cout << "Average Marks: " << average << endl;
}
};
int main() {
// Create an object of Result class (which also contains student info)
Result res;
// Display student information
res.setstudent();
res.setmarks();
res.getstudent();
res.getmarks();
res.getresult();
return 0;
}
Output:
Enter student Id:81901
Enter student Name:Prasanjeet
Enter 3 subjects marks 80 78 68
*******Student Details********
Id=81901
Name=Prasanjeet
Mark of subject 1=80
Mark of subject 2=78
Mark of subject 3=68
Total Marks: 226
Average Marks: 75.3333
Process returned 0 (0x0) execution time : 51.363 s
Press any key to continue.
No comments:
Post a Comment