c++ inheritance programs examples

c++ inheritance programs examples

c++ inheritance programs examples

c++ inheritance programs examples is collection of programs for understanding how to use inheritance in c++. This collection of program includes simple inheritance, multiple inheritance and multi level inheritance. By implementing this c++ programs you can easily understand the types of inheritance.

//Written by Adarsh Patel

/*
Inheritance 01 – Simple Derived class as private from Base class
Single Inheritance
*/
#include <iostream.h>
#include <conio.h>

class B
{
private:
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
void line()
{
cout << “Base class Line function called…” << endl;
}
};

class D : private B
{
private:
int c;
public:
void mul();
void display();
void line()
{
cout << “Derived class line function called…” << endl;
}

};

void B :: get_ab()
{
cout << “Enter the value of A and B ” << endl;
cin >> a >> b;
}

int B :: get_a()
{
return a;
}

void B :: show_a()
{
cout << “Base class called…” << endl;
cout << ” A ==> ” << a << endl;
}
void D :: mul()
{
get_ab();
c = b * get_a();
}
void D :: display()
{
cout << “Derived class called ” << endl;
show_a();
cout << ” B ==> ” << b << endl;
cout << ” C ==> ” << c << endl;
}

//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();
D D1;    //derived class object

D1.mul();
D1.display();

cout << endl;

D1.mul();
D1.display();
D1.line();

getch();
}

//Written by Adarsh Patel

/*
Inheritance 02 –     Derived class using public visibility mode,
also with constructor (Automatic call)
Single Inheritance
*/
#include <iostream.h>
#include <conio.h>

class B
{
protected:
int a;
public:
int b;
B()
{
cout << “Base class constructor called…” << endl;
}
void get_ab();
void line()
{
cout << “Base class Line function called…” << endl;
}
~B()
{
cout << “Base class Destructor called…” << endl;
}
};

class D : public B
{
private:
int c;
public:
D()
{
cout << “Derived class construtor called…” << endl;
}
void mul();
void display();
void line()
{
cout << “Derived class line function called…” << endl;
}
~D()
{
cout << “Derived class destructor called… ” << endl;
}

};

void B :: get_ab()
{
cout << “Enter the value of A and B ” << endl;
cin >> a >> b;
}

void D :: mul()
{
get_ab();
c = b * a;
}
void D :: display()
{
cout << “Derived class called ” << endl;
cout << ” A ==> ” << a << endl;
cout << ” B ==> ” << b << endl;
cout << ” C ==> ” << c << endl;
}

//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();
D D1;    //derived class object

D1.get_ab();
D1.mul();
D1.display();

cout << endl;

D1.b = 20;
D1.mul();
D1.display();
D1.line();

getch();
}

//Written by Adarsh Patel

/*
Inheritance 03 –    Parameterize constructor call
In Photo Copy
*/
#include <iostream.h>
#include <conio.h>

class B
{
private:
int a;
protected:
void printinfo();
static int i;    //Static member must be protected or public
public:
//        B(int x=0) : a(x) {}    //Short method to initialize
//        Or
B(int x=0)
{
a = x;
cout << “Base class constructor called…” << ++i << endl;
cout << “Value of A : ” << a << endl;
}
~B()
{
cout << “Base class Destructor called…” << –i << endl;
}
void print()
{
B :: printinfo();
}
};

int B :: i =0;

void B :: printinfo()
{
cout << “A ==> ” << a;
cout << ” , Size of Base class B is : ” << sizeof(this->a) << endl;
}

class D : public B
{
private:
int b;
public:
D(int y=0) : B(y), b(y+10)
//        Or
//        D(int y=0)
{
//            B(y);
cout << “Derived class construtor called…” << ++i << endl;
//            b = y+10;

}
~D()
{
cout << “Derived class destructor called… ” << –i << endl;
}
void print();
};
void D :: print()
{
B :: printinfo();
cout << “B ==> ” <<  b;
cout << ” , Size of Derived class D is : ” << sizeof(this->b) << endl;
}

//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();
B b(100);    //With parameter initialize by 100 : base class only
B b1;    //Without parameter by 0
D d(200);    //Base class obj construct first then derived: a=200 b=210
D d1;    //a = 0  b=10
cout << endl << endl;
b.print();
d.print();
cout << endl << endl;
cout << “Sizeof Base    class is : ” << sizeof(b) << endl;
cout << “Sizeof Derived class is : ” << sizeof(d) << endl;

getch();
}

//Written by Adarsh Patel

/*
Inheritance 04 –    Multiple Inheritance

*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class Student
{
protected:
int roll_no;
public:
void get_data();
};

void Student :: get_data()
{
cout << “Enter Roll Number :”;
cin >> roll_no;
}

class Subject
{
protected:
int sub1,sub2,sub3,total;
float per;
public:
void get_data();
};

void Subject :: get_data()
{
cout << “Enter Sub1 Sub2 Mark :”;
cin >> sub1 >> sub2;
}

class Result : public Student, public Subject
{
public:
void cal_data();
void display();
};

void Result :: cal_data()
{
total = sub1 + sub2;
per = total / 2.0;
}

void Result :: display()
{
cout << “The result is as follow : \n”;
cout << “Roll    Sub1      Sub2        Total   Per\n”;
cout << roll_no     << setw(10)
<< sub1        << setw(10)
<< sub2        << setw(10)
<< total         << setw(10)
<< per        << endl;
}
//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();

Result R1;
R1.Student :: get_data();
R1.Subject :: get_data();
R1.cal_data();
R1.display();

getch();
}

/*
———————-                ———————-
class student                        class subject
———————-                ———————-
||                                ||
||                                ||
||                                ||
==========================================
||
||
———————-
class Result
———————-
*/

//Written by Adarsh Patel

/*
Inheritance 05 –    Multiple Inheritance
With Constructor & Destructor

*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class Student
{
protected:
int roll_no;
Student(int);
~Student()
{
cout << “Student destructor called… ” << endl;
}
};

Student :: Student(int no)
{
cout << “Student Constructor called…” << endl;
roll_no = no;
}

class Subject
{
protected:
int sub1,sub2,sub3,total;
float per;
Subject(int,int);
~Subject()
{
cout << “Subject Destructor called…” << endl;
}
};

Subject :: Subject(int s1, int s2)
{
cout << “Subject Constructor called…” << endl;
sub1 = s1;
sub2 = s2;
}

class Result : public Student, public Subject
{
public:
void cal_data();
void display();
Result(int,int,int);
~Result()
{
cout << “Result Destructor called…” << endl;
}
};

Result :: Result(int no,int m1, int m2) : Student(no), Subject(m1,m2)
{
cout << “Result Constructor called…” << endl;
}
void Result :: cal_data()
{
total = sub1 + sub2;
per = total / 2.0;
}

void Result :: display()
{
cout << “The result is as follow : \n”;
cout << “Roll    Sub1      Sub2        Total   Per\n”;
cout << roll_no     << setw(10)
<< sub1        << setw(10)
<< sub2        << setw(10)
<< total         << setw(10)
<< per        << endl;
}
//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();

Result R1(100,56,57);
R1.cal_data();
R1.display();

getch();
}

/*
———————-                ———————-
class student                        class subject
———————-                ———————-
||                                ||
||                                ||
||                                ||
==========================================
||
||
———————-
class Result
———————-
*/

//Written by Adarsh Patel

/*
Inheritance 06 –
With Constructor & Destructor

*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>

class Match
{
protected:
char name[10];
int play;
public:
float average(int b)
{
return ( (float) b/play );
}
Match()
{
cout << “Match class constructor called…” << endl;
}
~Match()
{
cout << “Match class Destructor called…” << endl;
}
};

class One_day : private Match
{
protected:
int o_run;
public:
One_day(char *ptr, int a, int run)
{
cout << “One day class constructor called… ” << endl;
strcpy(name,ptr);
play = a;
o_run = run;
}
void display();
~One_day()
{
cout << “One day class destructor called… ” << endl;
}
};
void One_day :: display()
{
cout << “One day match ” << endl;
cout << setw(15) << name
<< setw(5) << play
<< setw(8) << o_run
<< setw(5) << average(o_run)
<< endl;

}
class Test : private Match
{
protected:
int t_run;
public:
Test(char *ptr, int a, int run)
{
cout << “Test class constructor called…” << endl;
strcpy(name,ptr);
play = a;
t_run = run;
}
void display();
~Test()
{
cout << “Test class destructor called… ” << endl;
}
};
void Test :: display()
{
cout << “Test match ” << endl;
cout << setw(15) << name
<< setw(5) << play
<< setw(8) << t_run
<< setw(5) << average(t_run)
<< endl;

}

//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();
One_day Obj1(“Adarsh”,100,15000);
Test Obj2(“Adars”,100,15000);

Obj1.display();
Obj2.display();

getch();
}

/*

———————-
class match
———————-
||
||
==========================================
||                                ||
||                                ||
||                                ||
———————-                ———————-
class one day                        class test
———————-                ———————-

*/

//Written by Adarsh Patel

/*
Inheritance 07 –
With Constructor & Destructor
*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class Employee
{
protected:
int emp_no, basic;
char name[10];
public:
Employee();
void put_emp();
};
Employee :: Employee()
{
cout << “Employee Constructor called…” << endl;
cout << “Enter Employee Number and Name : “;
cin >> emp_no >> name;
cout << “Enter Basic Salary : “;
cin >> basic;
}
void Employee :: put_emp()
{
cout << setw(5) << emp_no << setw(5) << name << setw(5) << basic << endl;
}

class Da_hra : virtual public Employee
{
protected:
int da_amt, hra_amt;
public:
Da_hra(float d, float h)
{
cout << “Da hra constructor called… ” << endl;
da_amt = d *  basic;
hra_amt = h * basic;
}
};
class Allounce : public virtual Employee
{
protected:
int tran_al, exam_al;
public:
Allounce(int t, int e)
{
cout << “Allounce Constructor called…. ” << endl;
tran_al = t;
exam_al = e;
}
};

class Salary : public Da_hra, public Allounce
{
private:
int total;
public:
Salary(float d, float h, int t, int e) : Employee(), Da_hra(d,h), Allounce(t,e)
{
cout << “Salary constructor called… ” << endl;
}
void display();
};
void Salary :: display()
{
total = basic + da_amt + hra_amt + tran_al + exam_al;
put_emp();
cout << setw(5) << da_amt << setw(5) << hra_amt << setw(5) << tran_al
<< setw(5) << exam_al << endl;
cout << “Total Salary : ” << total << endl;
}

//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();
Salary S1(0.50,0.15,400,200);
S1.display();

getch();
}

/*
———————-
class Employee
———————-
||
||
==========================================
||                                ||
||                                ||
||                                ||
———————-                ———————-
class Da hra                        class Allounce
———————-                ———————-
||                                ||
||                                ||
||                                ||
==========================================
||
||
———————-
class Salary
———————-
*/

//Written by Adarsh Patel

/*
Inheritance 08 –

*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>

class B
{
public:
void display()
{
cout << “Display Base Class ” << endl;
}
virtual void show()
{
cout << “Show Base class ” << endl;
}
B()
{
cout << “Base class Constructor called…” << endl;
}
~B()
{
cout << “Base class Destructor called… ” << endl;
}
};
class D : public B
{
public:
void display()
{
cout << “Display Derived class” << endl;
}
void show()
{
cout << “Show derived class” << endl;
}
D()
{
cout << “Derived class Constructor called…” << endl;
}
~D()
{
cout << “Derived class Destructor called…” << endl;
}
};
void main()
{
clrscr();

B b;
printf(“\nAddress of Base class object : %u”,b);

D d;
printf(“\nAddress of Derived class object : %u”,d);

B *ptr;
printf(“\nAddress of Base class pointer is : %u”,ptr);
cout << “\n\n Ptr points base class \n” << endl;

ptr = &b;

printf(“\nContent of Base class pointer is : %u”,*ptr);

ptr->display();
ptr->show();

cout << “\n\n Ptr points derived class ” << endl;
ptr = &d;
printf(“\nContent of derived pointer is : %u”,ptr);

ptr->display();
ptr->show();

getch();
}

//Written by Adarsh Patel

/*
Inheritance 09 –

*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class Media
{
protected:
char title[20];
float price;
public:
Media(char *ptr,float p)
{
strcpy(title,ptr);
price = p;
}
virtual ~Media()
{
cout << “Virtual destructor called…” << endl;
}
virtual void display()=0;        //pure virtual function
//          virtual void display(){};
};
class Cd : public Media
{
private:
char c_type[20];
int qty;
public:
Cd(char *ptr,float p, char *c, int q) : Media(ptr,p)
{
strcpy(c_type,c);
qty = q;
}
void display()
{
cout << “Cd Type  : ” << c_type << endl;
cout << “Quantity : ” << qty << endl;
}

};
class Floppy : public Media
{
private:
char f_type[20];
int qty;
public:
Floppy(char *ptr, float p, char *c,int q) : Media(ptr,p)
{
strcpy(f_type,c);
qty = q;
}
void display()
{
cout << “Floppy Type  : ” << f_type << endl;
cout << “Quantity     : ” << qty << endl;
}

};
void main()
{
clrscr();
Cd C1(“Samsung”,10.00,”700 MB”,5);
Floppy F1(“Sony”,12.00,”1.44 MB”,5);

Media *list[2];

list[0] = &C1;
list[1] = &F1;

cout << “\t\t\t….. Media Detail” << endl;
cout << “\t….. Cd Detail” << endl;
list[0]->display();

cout << “\t….. Floppy Detail” << endl;
list[1]->display();

getch();
}

//Written by Adarsh Patel

/*
Inheritance 10 –

*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

class Poly
{
protected:
int width, height;
public:
void set_val(int a, int b)
{
width = a;
height = b;
}
virtual float area()=0;
void print_area()
{
cout << this->area() << endl;
}
virtual ~Poly()
{
cout << “Virtual poly destructor called…” << endl;
}
};
class Rect : public Poly
{
public:
float area()
{
cout << “Class Rectangle called…” << endl;
cout << “Rectangle area calculated” << endl;

return(width*height);
}
~Rect()
{
cout << “Destructor Rectangle called…”<< endl;
}
};
class Triangle : public Poly
{
public:
float area()
{
cout << “Class triangle called… “<< endl;
cout << “Triangle area calculated ” << endl;
return(width*height/2.0);
}
~Triangle()
{
cout << “Triangle destructor called…” << endl;
}
};
void main()
{
clrscr();
Rect R;
Triangle T;

Poly *list[2];
list[0] = &R;
list[1] = &T;
list[0] ->set_val(4,5);
list[1] ->set_val(5,5);

list[0]->print_area();
list[1]->print_area();
}

C Tutorial

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative