Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts

Sunday, 6 May 2012

Program Of Constructor And Destructor In Inheritance In C++

#include<iostream.h>
#include<conio.h>
class base
{
private:
int a;
public:
void getdatab(int x)
{
a=x;
}
void displayb()
{
cout<<"\n base value is="<<a;
}
} ;

class derive : public base     // Single Inheritence
{
private:
int b;
public:
{
b=y;
}
void displayd()
{
cout<<"\n derive value is="<<b;
}
{
 cout<<"Destructor Fired";

}
} ;

main()
{
clrscr();
derive d1(7);
d1.getdatab(5);
d1.displayb();
d1.displayd();

getch();
}

Tuesday, 1 May 2012

Program Of Inheritance Using Constructor In C++

#include<iostream.h>
#include<conio.h>
class base
{
public:
base()
{
}
~base()
{
}
} ;

class derive : public base     // Single Inheritence
{

private:
int *a;
public:
derive()
{
cout<<"\n Derive CAlss Consructor";
a = new int [15];
}
~derive()
{
 cout<<"\n Derive CLass Destructor";
 delete []a;
}
} ;

main()
{
clrscr();
base  *ptr;
ptr = new derive;
delete ptr;
getch();
}

Saturday, 28 April 2012

Program Of Single Inheritance In C++

#include<iostream.h>
#include<conio.h>
class base
{
private:
int a;
public:
{
a=x;
}
void displayb()
{
cout<<"\n base value is="<<a;
}
} ;

class derive : public base     // Single Inheritence
{
private:
int b;
public:
void getdatad(int y)
{
b=y;
}
void displayd()
{
cout<<"\n derive value is="<<b;
}
} ;

main()
{
clrscr();
derive d1;
d1.getdatab(5);
d1.getdatad(7);
d1.displayb();
d1.displayd();

getch();
}
--------------------------------------------------
Single Inheritance

Multiple inheritance is a feature of some object-oriented 

computer programming languages in which a class can 

inherit behaviors and features from more than one 


Simple Single_Inheritance Graph

Program Of Multilevel Inheritance In C++

#include<iostream.h>
#include<conio.h>
class base
{
private:
int a;
public:
{
a=x;
}
void displayb()
{
cout<<"\n base value is="<<a;
}
} ;

class derive : public base     // Single Inheritence
{
private:
int b;
public:
void getdatad(int y)
{
b=y;
}
void displayd()
{
cout<<"\n derive value is="<<b;
}
} ;

class derive1 : public derive     // Multilevel Inheritence
{
private:
int c;
public:
void getdatad1(int z)
{
c=z;
}
{
cout<<"\n derive1 value is="<<c;
}
} ;

main()
{
clrscr();
derive1 d1;
d1.getdatab(5);
d1.getdatad(7);
d1.getdatad1(9);
d1.displayb();
d1.displayd();
d1.displayd1();
getch();
}
------------------------------------------
Multilevel Inheritance

Derived a new class from another derived class.It is known 
as multilevel inheritance.
example:
grandfather->father->son.


Friday, 27 April 2012

Program Of Multiple Inheritance In C++

#include<iostream.h>
#include<conio.h>
class base
{
private:
int a;
public:
{
a=x;
}
void displayb()
{
cout<<"\n base value is="<<a;
}
} ;

{
private:
int b;
public:
void getdatad(int y)
{
b=y;
}
void displayd()
{
cout<<"\n derive value is="<<b;
}
} ;

class derive1 : public base, public derive     // Multiple Inheritence
{
private:
int c;
public:
{
c=z;
}
void displayd1()
{
cout<<"\n derive1 value is="<<c;
}
} ;

main()
{
clrscr();
derive1 d1;
d1.getdatab(5);
d1.getdatad(7);
d1.getdatad1(9);
d1.displayb();
d1.displayd();
d1.displayd1();
getch();
}
--------------------------------------------------------------------------------------
Define Multiple Inheritance


Multiple inheritance 1

Deriving directly from more than one class is usually called multiple inheritance. Since it's widely believed that this concept complicates the design and debuggers can have a hard time with it, multiple inheritance can be a controversial topic. However, multiple inheritance is an important feature in C++ and C++ programmers think of it as a very good structuring tool.

Program Of Multiple Inheritance In C++

#include<iostream.h>
#include<conio.h>
class base
{
private:
int a;
public:
{
a=x;
}
void displayb()
{
cout<<"\n base value is="<<a;
}
} ;

class derive
{
private:
int b;
public:
void getdatad(int y)
{
b=y;
}
{
cout<<"\n derive value is="<<b;
}
} ;

class derive1 : public base, public derive     // Multiple Inheritence
{
private:
int c;
public:
void getdatad1(int z)
{
c=z;
}
{
cout<<"\n derive1 value is="<<c;
}
} ;

main()
{
clrscr();
derive1 d1;
d1.getdatab(5);
d1.getdatad(7);
d1.getdatad1(9);
d1.displayb();
d1.displayd();
d1.displayd1();
getch();
}

Program Of Hierarchical Inheritance In C++

#include<iostream.h>
#include<conio.h>
class person
{
private:
char name[20];
long int phno;
public:
void read()
{
cout<<"\n Enter name ";
cin>>name;
cout<<"\n Enter Phno.=";
cin>>phno;
}
void show()
{
cout<<"\n Name="<<name;
cout<<"\n Phone="<<phno;
}
};
{
private:
int rollno;
char course[20];
public:
void read()
{
person::read();
cout<<"\n Enter Roll No.=";
cin>>rollno;
cout<<"\n Enter Course=";
cin>>course;
}
void show()
{
person ::show();
cout<<"\n Roll No.="<<rollno;
cout<<"\n Course="<<course;
}

};
{
private:
char dept_name[10];
char qual[10];
public:
void read()
{
person::read();
cout<<"\n Enter dept_name andQualification=";
cin>>dept_name>>qual;
}
void show()
{
person::show();
cout<<"\n Departement="<<dept_name;
cout<<"\n Qualififcation="<<qual;
}
};

main()
{
clrscr();
student s1;
cout<<"\n************************ Enter student Information******************";
s1.read();
cout<<"\n************************ Displaying Student Information*************";
s1.show();
teacher t1;
cout<<"\n************************** Enter Teacher Information*****************";
t1.read();
cout<<"\n*****************************Displaying Teacher Information************";
t1.show();

getch();
}
----------------------------------

Hierarchical Inheritance



When two or more classes are derived from a single

 base class, then 

Inheritance is called the hierarchical inheritance. The 


representation

of the hierarchical inheritance is shown in the 


following Fig.
Photobucket

Program Of Hybrid Inheritance In C++

#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
char name[20];
public:
void read()
{
cout<<"\n Enter roll no.";
cin>>rollno;
cout<<"\n Enter Name=";
cin>>name;
}
void show()
{
cout<<"\n Roll No.="<<rollno;
cout<<"\n Name="<<name;
}
};
class exam_internal: public student
{
protected:
int sub1_marks;
int sub2_marks;
public:
{
cout<<"\n Enter Internal MArks1=";
cin>>sub1_marks;
cout<<"\n Enter Internal MArks2=";
cin>>sub2_marks;
}
{
cout<<"\n Intrenal marks1 ="<<sub1_marks;
cout<<"\n Internal marks2= "<<sub2_marks;
}
};

class exam_external
{
protected:
int sub1_exmarks;
int sub2_exmarks;
public:
void read_marks()
{
cout<<"\n Enter EXternal MArks1=";
cin>>sub1_exmarks;
cout<<"n Enter External MArks2=";
cin>>sub2_exmarks;
}
void display_marks()
{
cout<<"\n EXtrenal marks1 ="<<sub1_exmarks;
cout<<"\n External marks2= "<<sub2_exmarks;
}
};
class result : public exam_external,public exam_internal
{
private:
int total_marks;
public:
void cal_result()
{
total_marks=sub1_marks+sub2_marks+sub1_exmarks+sub2_exmarks;
cout<<"\n Total marks="<<total_marks;
}
};
main()
{
clrscr();
result r1;
cout<<"\n********************** Enter Information***********************";
r1.read();
cout<<"\n********************** Enter marks of Internal Examination*******";
r1.exam_internal::read_marks();
//r1.read_marks1();
cout<<"\n********************** Enter marks of Exterrnal Examination***********";
r1.exam_external::read_marks();
cout<<"\n********************** Displaying student Detail*******************";
r1.show();
r1.exam_internal::display_marks();
r1.exam_external::display_marks();
cout<<"\n********************** Calculating & Displaying Marks***************";
r1.cal_result();
getch();
}
-----------------------------------------
Hybrid Inheritance

Hybrid Inheritance is the combination of two or more 

inheritances : single,

 multiple,multilevel or hierarchical Inheritances. The 

following is a C++ Program to for Calculating 

the marks secured by a student.A Parent class with student 

identification is created and another class called marks is 

inherited from the main class.This class marks is further 

inherited by another class called sports and finally the sports 

class is inherited by the percentage class to calculated the 

percentage of marks.This is the best example for Hybrid 

Inheritance in c++