Sie sind auf Seite 1von 2

Hybrid Inheritance

class stud
{
Protected:
int rno;
Public:
Void getno(int n)
{ Rno=n; }
Void display_rno()
{ out<<Roll_no=<<rno<<\n;
}
};
Class test: Public stud
{
Protected:
int sub1,sub2;
Public:
Void get_mark(int m1,int m2)
{
Sub1=m1;
Sub2=m2;
}
Void display_mark()
{ Cout<<sub1<<sub1<<\n;
Cout<<sub2<<sub2<<\n; }
};
Class sports
{
Protected:
Float score;
Public :
Void get_score(float s)
{ Score=s; }

Void put_score()
{ Cout<<Sort :<<score<<\n; }
};
Class result: public test , public sports
{
Float total;
Public:
Void display();
};
Void result::display()
{
Total=sub1+sub2+score;
display_rno();
display_mark();
put_score();
cout<< total score:<<total<<\n;
}
int main()
{ Result s r1;
r1. getno(123);
r1. get_mark(60,80)
r1.get_score(6);
r1.display();

Constructors and Inheritance

class Base
{
public:
Base ( )
{
cout << "Inside Base constructor" <<
endl;
}
~Base ( )
{
cout << "Inside Base destructor" <<
endl;
}
};
class Derived : public Base
{
public:
Derived ( )
{
cout << "Inside Derived constructor"
<< endl;
}

~Derived ( )
{
cout << "Inside Derived destructor"
<< endl;
}
};
void main( )
{
Derived x;
}
Base class constructors and derived
class destructors are called first
In the code above, when the object "x" is
created, first the Base class constructor
is called, and after that the Derived class

constructor is called. Because the


Derived class inherits from the Base
class, both the Base class and Derived
class constructors will be called when a
Derived class object is created.
When the main function is finished
running, the object x's destructor will
get called first, and after that the Base
class destructor will be called.
So, the output of the code above is:
Inside Base constructor
Inside Derived constructor
Inside Derived destructor
Inside Base destructor

Das könnte Ihnen auch gefallen