Tuesday, 24 February 2015

Inheritance

Types

Single Inheritance
Multiple Inheritance
Multi level Inheritance
Hybrid Inheritance
Parent class & child class 
Base class & derived class

#include<iostream.h>
#include<conio.h>

class PCP     //class name
{
private:            //access specifier
float dos,msoffice,foxpro;
public:             //access specifier
void pcp_get_fees();     //member function
void pcp_list_fees();      //member function
};
class HDCA:public PCP    //inherits the class
{
private:         //access specifier
float unix,c,cpp;
public:
void hdca_get_fees();         //member function
void hdca_list_fees();        //member function
};
void PCP::pcp_get_fees()    //class name and member
              function scope resolution operator will come
{
cout<<”Enter the fees amount for dos:”;
cin>>dos;
cout<<”Enter the fees amount for msoffice:”;
cin>>msoffice;
cout<<”Enter the fees amount for foxpro:”;
cin>>foxpro;
}
void PCP::pcp_list_fees()   // class name and member
              function scope resolution operator will come
{
cout<<”Dos:”<<dos<<endl;
cout<<”Foxpro:”<<foxpro<<endl;
cout<<”Msoffice:”<<msoffice<<endl;
}
void HDCA::hdca_get_fees()//class name and member function scope resolution operator will come
{
pcp_get_fees();
cout<<”Enter the fees amount for unix:”;
cin>>unix;
cout<<”Enter the fees amount for C:”;
cin>>c;
cout<<”Enter the fees amount for C++:”;
cin>>cpp;
}
void HDCA::hdca_list_fees()//class name and member function scope resolution operator will come
{
pcp_list_fees();
cout<<”Unix:”<<unix<<endl;
cout<<”C:”<<c<<endl;
cout<<”C++:”<<cpp<<endl;
}
void main()
{
PCP p;          //object
HDCA h;     //object
Clrscr();
Cout<<endl<<”Fees details for pcp”<<endl;
p.pcp_get_fees();
cout<<endl<<”Fees details for HDCA”<<endl;
h.hdca_get_fees();
cout<<endl<<”Fees list for pcp”<<endl;
p.pcp_list_fees();
cout<<endl<<”Fees list for hdca”<<endl;
h.hdca_list_fees();
getch();
}


#include<iostream.h>
#include<conio.h>

class baseA      //class name
{
public:    //access specifier
void show()       //member function
{
cout<<”Base class show function”<<endl;
}
};
class deriA:public baseA      //Inherits the class
{
public:       //access specifier
void show()         //member function
{
cout<<”Derive class show function”<<endl;
}
};
void main()
{
baseA b;              //object
deriA d;            //object
clrscr();
b.show();
d.show();
getch();
}



#include<iostream.h>
#include<conio.h>

class rectangle        //class name
{
private:         //access specifier
int length,breadth;
public:         //access specifier
rectangle()        //constructor
{
length=breadth=0;
}
rectangle(int a,int b)       //constructor overloading
{
length=a;
breadth=b;
}
void show()        //member function
{
cout<<”Length:”<<length<<”Breadth:”<<breadth<<endl;
}
};
class cuberect:public rectangle     //Inheirts the class
{
private:         //access specifier
int height;
public:         //access specifier
cuberect()         //constructor
{
height=0;
}
cuberect(int a,int b,int c):rectangle(a,b)  //Constructor
                           overloading class name body also
{
height=c;
}
void show()           //member function
{
rectangle::show()      //class name body
cout<<”Height:”<<height<<endl;
}
};
void main()
{
cuberect c1,c2(10,20,30);       //object
clrscr();
c1.show();
c2.show();
getch();
}

#include<iostream.h>
#include<conio.h>

class base1     //class name
{
protected:       //access specifier
int var1;
public:             //access specifier
void disp_base1()        //member function
{
cout<<”Var1 is:”<<var1<<endl;
}
};
class base2      //member function
{
protected:      //access specifier
int var2;
public:           //access specifier
void disp_base2()       //member function
{
cout<<”Var2 is:”<<var2<<endl;
}
};
class deri:public base1,public base2//Inheirts theClass
{
{
private:      //access specifier
int var3;
public:        //access specifier
deri(int a,int b,int c)       //Constructor overloading
{
var1=a;
var2=b;
var3=c;
}
void disp_me()      //member function
{
cout<<”Var3 is:”<<var3<<endl;
}
};
void main()
{
deri d(10,20,30);       //object
clrscr();
d.disp_base1();
d.disp_base2();
d.disp_me();
getch();

}

No comments:

Post a Comment