Read OnLine Cpp Program Example



11 INHERITANCE
PRAC 1 PRAC 2

// inheritance example
// constructors and derived classes
#include<iostream.h>
#include<conio.h>
class mother
{
public :
mother()
{
cout<<"mother:no parameters\n";
}
mother (int a)
{
cout<<"mother:int parameter\n";
}
};

class daughter:public mother
{
public:
daughter (int a)
{
cout<<"daughter:int parameter\n\n";
}
};

class son:public mother
{
public:
son(int a) : mother (a)
{
cout<<"son:int parameter\n\n";
}
};

void main()
{
clrscr();
daughter d1(0);
son s1(0);
getch();
}
----OUTPUT----
mother: no parameters
daughter: int parameter
mother: int parameter
son: int parameter

// inheritance example
#include<iostream.h>
#include<conio.h>
class demo1
{
public:void add()
{
int a,b,ans;
a=5;
b=6;
ans=a+b;
cout<<"\nAdd="<<ans;
}
};
class demo2:public demo1
{
public:void sub()
{
int a,b,ans;
a=5;
b=10;
ans=a-b;
cout<<"Sub="<<ans;
}
};
void main()
{
clrscr();
demo2 d2;
d2.sub();
d2.add();
getch();
}
----OUTPUT----
Add= 11
Sub= -5

Previous Next
Download Download