Multiple Inheritance Inheritance In C++
Inheritance is a process of creating new classes from existing classes. Existing class is known as base class and new class is known as derived class. The derived class is also called as sub class or child class and base class is called as super class or parent class. The derived class inherits all the features of base class .
The main advantages of of inheritance are
reusability
to increase the reliability of the code
to add more enhancements to the base class
2. Multiple Inheritance:
The class can inherit the features of two or more classes is known as multiple inheritance. Multiple inheritance allows to combine the features of several existing classes into new classes. It is just like the child inherits all the biological features of parents.
The general syntax for derived class with Multiple Inheritance
class A
{
_________
_________
_________
};
class B
{
_________
_________
_________
};
class C: public A, public B
{
____________
____________
};
Here class A and class B are base classes and class C is derived class which is derived from both the base classes A and B. In multiple inheritance the derived class C can combine all the features of base class A and B.
A program to illustrate how multiple inheritance can be declared and defined in a program. This program consist two base classes and one derived class. The base classes are basic_info and academic. The derived class is financial_assist.
//Multiple Inheritance
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class basic_info
{
private:
char name[20];
int rollno;
char sex;
public:
void getdata();
void display();
};//end of class definition
class academic
{
private:
char course[20];
char sem[10];
int rank;
public:
void getdata();
void display();
};//end of class definition
class financial_assist : private basic_info, private academic
{
private:
float amount;
public:
void getdata();
void display();
};//end of class definition
void basic_info::getdata()
{
cout<<”Enter name”<<endl;
cin>>name;
cout<<”Enter rollno”<<endl;
cin>>rollno;
cout<<”Enter sex”<<endl;
cin>>sex;
}
void basic_info::display()
{
cout<<name <<” “;
cout<<rollno<<” “;
cout<<sex<<” “;
}
void academic :: getdata()
{
cout<<”Course name”<<endl;
cin>>course;
cout<<”Sem”<<endl;
cin>>sem;
cout<<”Rank”<<endl;
cin>>rank;
}
void academic :: display()
{
cout<<”Course is”<<endl;
cout<<course;
cout<<”Semester is”<<endl;
cout<<sem;
cout<<”Rank is”<<endl;
cout<<rank<<” “;
}
void financial_assit::getdata()
{
basic_info :: getdata();
basic_info :: getdata();
cout<<”Enter ammount “<<endl;
cin>>amount;
}
void fitness::display()
{
basic_info::display();
academic :: display();
cout<<”Amout is”<<endl;
cout<<setprecision(2);
cout<<emount<<” ”;
}
void main()
{
financial_assist f;
cout<<” Enter information”<<endl;
cout<<” Financial assist”<<endl;
f,getdata();
cout<<endl;
cout<<” Academic Performance for financial assistance”<<endl;
cout<<”______________________”<<endl;
cout<<”Name Rollno Sex course sem rank amount”<<endl;
cout<<”______________________”<<endl;
f.display();
cout<<endl;
cout<<”_____________________”<<endl;
}
0 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏