Sie sind auf Seite 1von 16

Multiple inheritance

#include<iostream.h> class A { int a1,a2; public : void getdata() { cout<<"\n Enter value of a1 and a2"; cin>>a1>>a2; } void putdata() { cout<<"\n value of a1 is" <<a1"and a2 is"<<a2; } };

class B { int b1,b2; public : void indata() { cout<<"\n Enter the value of b1 nad b2"; cin>>b1>>b2; }

void outdata() { cout<<"\n the value of b1 is" <<b1 "and b2 is:<<b2; } }; class C: public A,public B { int c1,c2; public : void input() { cout<<"\n enter the value of c1 and c2"; cin>>c1>>c2; } C }

void output() { cout<<"\nvalue of c1 is"<<c1"and c2 is"<<c2; } }; void main() { C cc; cc.getdata(); //member function of class A cc.indata(); //member function of class B cc.input(); //member function of class C cc.putdata(); //member function of class A cc.outdata(); //member function of class B cc.output(); //member function of class C }

Output : Enter the value of a1 and a2 5 4 Enter the value of b1 and b2 8 7 enter the value of c1 and c1 9 3 The value of a1 is 5 and a2 is 4 The value of b1 is 8 and b2 is 7 The value of c1 is 9 and c2 is 3

Multiple inher
#include<iostream.h> #include<conio.h> class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"; cin>>m1>>m2; } };

class sports { protected: int sm; // sm = Sports mark public: void getsm() { cout<<"\nEnter the sports mark :"; cin>>sm; } };

class statement:public student,public sports { int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal cout<<"\n\tAverage : "<<avg; } };

: "<<tot;

void main() { clrscr(); statement obj; obj.get(); obj.getsm(); obj.display(); getch(); }

Hybrid inher
#include<iostream.h> Class A { Int a,b; public : void getdata() {cout<<"\n Enter the value of a and b"; cin>>a>>b; }

void putdata() { cout<<"\n The value of a is :"<<a "and b is"<<b; } }; class B : public A { int c,d; public :

void intdata() { cout<<"\n Enter the value of c and d ";cin>>c>>d; } void outdata() { cout<<"\n The value of c"<<c"and d is"<<d;}};

class C: public A { int e,f; public : void input() { cout<<"\n Enter the value of e and f; cin>>e>>f; }

void output() { cout<<"\nthe value of e is"<<e"and f is" <<f; }

void main() { B obj1; C obj2; obj1.getdata(); //member function of classA obj1.indata(); //member function of classB obj2.getdata(); //member function ofclassA obj2.input(); //member function of class C obj1.putdata(); //member function of class A obj1.outdata(); //member function of class B obj2.output(); //member function of class A obj2.outdata(); //member function of class C }

Das könnte Ihnen auch gefallen