Sep
16
//Multiple Inheritance in C++ Code-1
#include<iostream.h>
class A{
public:
void fun1(){
cout<<“fun1() is called from class A”<<endl;
}
};
class B{
public:
void fun2(){
cout<<“fun2() is called from class B”<<endl;
}
};
class C:public A, public B{
};
void main(){
C c1;
c1.fun1();
c1.fun2();
}
//Multiple Inheritance in C++ Code-2
#include<iostream.h>
class A{
public:
void fun1(){
cout<<“fun1() is called from class A”<<endl;
}
};
class B{
public:
void fun2(){
cout<<“fun2() is called from class B”<<endl;
}
};
class C: A, B{
public:
void fun3(){
fun1();
fun2();
}
};
void main(){
C c1;
/*c1.fun1();
c1.fun2();*/
c1.fun3();
}
//Multiple Inheritance in C++ Code-3
#include<iostream.h>
class A{
public:
int numA;
void getNumA(){
cout<<“Enter Value for numA: “;
cin>>numA;
}
};
class B{
public:
int numB;
void getNumB(){
cout<<“Enter Value for numB: “;
cin>>numB;
}
};
class C: public A, public B{
};
void main(){
C c1;
c1.getNumA();
c1.getNumB();
cout<<c1.numA+c1.numB<<endl;
}
//Multiple Inheritance in C++ Code-4
#include<iostream.h>
class A{
int numA;
public:
void getNumA(){
cout<<“Enter Value for numA: “;
cin>>numA;
}
int returnNumA(){
return(numA);
}
};
class B{
int numB;
public:
void getNumB(){
cout<<“Enter Value for numB: “;
cin>>numB;
}
int returnNumB(){
return(numB);
}
};
class C: public A, public B{
};
void main(){
C c1;
c1.getNumA();
c1.getNumB();
cout<<c1.returnNumA()+c1.returnNumB()<<endl;
}
//Multiple Inheritance in C++ Code-5
#include<iostream.h>
class A{
int numA;
public:
void getNumA(){
cout<<“Enter Value for numA: “;
cin>>numA;
}
int returnNumA(){
return(numA);
}
};
class B{
int numB;
public:
void getNumB(){
cout<<“Enter Value for numB: “;
cin>>numB;
}
int returnNumB(){
return(numB);
}
};
class C: public A, public B{
int numC;
public:
void getNumC(){
cout<<“Enter Value for numC: “;
cin>>numC;
}
int returnNumC(){
return(numC);
}
};
void main(){
C c1;
c1.getNumA();
c1.getNumB();
c1.getNumC();
int result=c1.returnNumA()+c1.returnNumB()+c1.returnNumC();
cout<<result<<endl;
}