Multiple inheritances:
- Multiple inheritances have a number of base classes and one derived class.
- It means the numbers of base classes are derived from one derived class.
Syntax:
Example:
Output:
Multiple inheritances:
Syntax:
class a
{
//Base
class1
};
class b
{
//Base
class2
};
class c:public a,public b
{
//Derived
class
};
//Object creation
Derived_class_name object_name;
object_name.function_name();
Example:
#include<iostream.h>
#include<conio.h>
class first //Base class1
{
public:
int num1;
void get_1st_value()
{
cout<<"\n
Enter first value: ";
cin>>num1;
}
};
class second //Base class2
{
public:
int num2;
void get_2nd_value()
{
cout<<"\n
Enter Second value: ";
cin>>num2;
}
};
class add: public first, public second //Derived
class
{
public:
int num3;
void operation()
{
num3=num1+num2;
}
void display()
{
cout<<"\n \n
Output:";
cout<<"\n \n
First number = "<<num1;
cout<<"\n \n
Second number = "<<num2;
cout<<"\n \n
First number + Second number =
"<<num3;
}
};
void main()
{
clrscr();
add a;
a.get_1st_value();
a.get_2nd_value();
a.operation();
a.display();
getch();
}
Output:
No comments:
Post a Comment