UNIT-III Inheritance Programs by VJTECH ACADEMY
//Single inheritance
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void display1()
{
cout<<"\nI am from Base class";
}
};
class Derived:public Base
{
public:
void display2()
{
cout<<"\nI am from Derived class";
}
};
void main()
{
Derived d1;
clrscr();
d1.display1();
d1.display2();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-III Inheritance Programs by VJTECH ACADEMY
//Single inheritance
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rollno;
char name[10];
public:
void get_stud_info()
{
cout<<"\nEnter student rollno:";
cin>>rollno;
cout<<"\nEnter student name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nRoll No:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter class test-1 marks:";
cin>>marks1;
cout<<"\nEnter class test-2 marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
void main()
{
Test t1;
clrscr();
t1.get_stud_info();
t1.get_marks();
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-III Inheritance Programs by VJTECH ACADEMY
t1.disp_stud_info();
t1.disp_marks();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-III Inheritance Programs by VJTECH ACADEMY
Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void display1()
{
cout<<"\nI am from class A";
}
};
class B:public A
{
public:
void display2()
{
cout<<"\nI am from class B";
}
};
class C:public B
{
public:
void display3()
{
cout<<"\nI am from class C";
}
};
void main()
{
C obj;
clrscr();
obj.display1();
obj.display2();
obj.display3();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-III Inheritance Programs by VJTECH ACADEMY
Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rollno;
char name[10];
public:
void get_stud_info()
{
cout<<"\nEnter Student Roll No:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nRoll No:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter class test-1 marks:";
cin>>marks1;
cout<<"\nEnter class test-2 marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Result:public Test
{
protected:
int total;
public:
void disp_total()
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-III Inheritance Programs by VJTECH ACADEMY
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
void main()
{
Result r1;
clrscr();
r1.get_stud_info();
r1.get_marks();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-III Inheritance Programs by VJTECH ACADEMY
Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void display1()
{
cout<<"\nI am from class A";
}
};
class B
{
public:
void display2()
{
cout<<"\nI am from class B";
}
};
class C:public A,public B
{
public:
void display3()
{
cout<<"\nI am from class C";
}
};
void main()
{
C obj;
clrscr();
obj.display1();
obj.display2();
obj.display3();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-III Inheritance Programs by VJTECH ACADEMY
Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rollno;
char name[10];
public:
void get_stud_info()
{
cout<<"\nEnter Student Roll No:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nRoll No:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter class test-1 marks:";
cin>>marks1;
cout<<"\nEnter class test-2 marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Result:public Student,public Test
{
protected:
int total;
public:
void disp_total()
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-III Inheritance Programs by VJTECH ACADEMY
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
void main()
{
Result r1;
clrscr();
r1.get_stud_info();
r1.get_marks();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-III Inheritance Programs by VJTECH ACADEMY
Hierarchical Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void displayA()
{
cout<<"\ndisplayA method of class A";
}
};
class B:public A
{
public:
void displayB()
{
cout<<"\ndisplayB method of class B";
}
};
class C:public A
{
public:
void displayC()
{
cout<<"\ndisplayC method of class C";
}
};
void main()
{
B b1;
C c1;
clrscr();
cout<<"\n***Object b1***";
[Link]();
[Link]();
cout<<"\n***Object c1***";
[Link]();
[Link]();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-III Inheritance Programs by VJTECH ACADEMY
Hierarchical Inheritance
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rollno;
char name[10];
public:
void get_stud_info()
{
cout<<"\nEnter Roll No:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent Roll No:"<<rollno;
cout<<"\nStudent Name:"<<name;
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-III Inheritance Programs by VJTECH ACADEMY
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter marks1:";
cin>>marks1;
cout<<"\nEnter marks2:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nTest-1 Marks:"<<marks1;
cout<<"\nTest-2 Marks:"<<marks2;
}
};
class Sport:public Student
{
protected:
int sport_wt;
public:
void get_sport_info()
{
cout<<"\nEnter Sport Weightage:";
cin>>sport_wt;
}
void disp_sport_info()
{
cout<<"\nSport Weightage:"<<sport_wt;
}
};
void main()
{
Test t1;
Sport s1;
clrscr();
cout<<"\n****Test Class****";
t1.get_stud_info();
t1.get_marks();
t1.disp_stud_info();
t1.disp_marks();
cout<<"\n****Sport Class****";
s1.get_stud_info();
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-III Inheritance Programs by VJTECH ACADEMY
s1.get_sport_info();
s1.disp_stud_info();
s1.disp_sport_info();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-III Inheritance Programs by VJTECH ACADEMY
Hierarchical Inheritance
#include<iostream.h>
#include<conio.h>
class Employee
{
protected:
int empid;
char name[10];
public:
void get_emp_info()
{
cout<<"\nEnter Emp Id:";
cin>>empid;
cout<<"\nEnter Emp Name:";
cin>>name;
}
void disp_emp_info()
{
cout<<"\nEmp ID:"<<empid;
cout<<"\nEmp Name:"<<name;
}
};
class Worker:public Employee
{
protected:
int overtime_sal;
public:
void get_worker_info()
{
cout<<"\nEnter Overtime salary:";
cin>>overtime_sal;
}
void disp_worker_info()
{
cout<<"\nOvertime Salary:"<<overtime_sal;
}
};
class Manager:public Employee
{
protected:
int additional_allowance;
public:
void get_manager_info()
{
cout<<"\nEnter additional allowance:";
cin>>additional_allowance;
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-III Inheritance Programs by VJTECH ACADEMY
}
void disp_manager_info()
{
cout<<"\nAditional Allowance:"<<additional_allowance;
}
};
void main()
{
Worker w1;
Manager m1;
clrscr();
cout<<"\n****Worker Class****";
w1.get_emp_info();
w1.get_worker_info();
w1.disp_emp_info();
w1.disp_worker_info();
cout<<"\n****Manager Class****";
m1.get_emp_info();
m1.get_manager_info();
m1.disp_emp_info();
m1.disp_manager_info();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-III Inheritance Programs by VJTECH ACADEMY
Hybrid Inheritance
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rollno;
char name[10];
public:
void get_stud_info()
{
cout<<"\nEnter student roll no:";
cin>>rollno;
cout<<"\nEnter student name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent Roll No:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter Test-1 Marks:";
cin>>marks1;
cout<<"\nEnter Test-2 Marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nClass Test-1 Marks:"<<marks1;
cout<<"\nClass Test-2 Marks:"<<marks2;
}
};
class Sport
{
protected:
int sport_wt;
public:
void get_sport_info()
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-III Inheritance Programs by VJTECH ACADEMY
{
cout<<"\nEnter Sport Weightage:";
cin>>sport_wt;
}
void disp_sport_info()
{
cout<<"\nSport Weightage:"<<sport_wt;
}
};
class Result:public Test,public Sport
{
protected:
int total;
public:
void disp_total()
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
void main()
{
Result r1;
clrscr();
r1.get_stud_info();
r1.get_marks();
r1.get_sport_info();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total();
r1.disp_sport_info();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-III Inheritance Programs by VJTECH ACADEMY
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-III Inheritance Programs by VJTECH ACADEMY
Hybrid Inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
void displayA()
{
cout<<"\nDisplayA method of class A";
}
};
class B:public A
{
public:
void displayB()
{
cout<<"\nDisplayB method of class B";
}
};
class C
{
public:
void displayC()
{
cout<<"\nDisplayC method of class C";
}
};
class D:public B,public C
{
public:
void displayD()
{
cout<<"\nDisplayD method of class D";
}
};
void main()
{
D d1;
clrscr();
[Link]();
[Link]();
[Link]();
[Link]();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-III Inheritance Programs by VJTECH ACADEMY
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-III Inheritance Programs by VJTECH ACADEMY
Method Overriding
#include<iostream.h>
#include<conio.h>
class A
{
public:
void display()
{
cout<<"\nDisplay method of class A";
}
};
class B:public A
{
public:
void display()
{
cout<<"\nDisplay method of class B";
}
};
void main()
{
B b1;
clrscr();
[Link]();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-III Inheritance Programs by VJTECH ACADEMY
Virtual Base class
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rollno;
char name[10];
public:
void get_stud_info()
{
cout<<"\nEnter Student Roll No:";
cin>>rollno;
cout<<"\nEnter Student Name:";
cin>>name;
}
void disp_stud_info()
{
cout<<"\nStudent Roll No:"<<rollno;
cout<<"\nStudent Name:"<<name;
}
};
class Test:virtual public Student
{
protected:
int marks1,marks2;
public:
void get_marks()
{
cout<<"\nEnter test-1 marks:";
cin>>marks1;
cout<<"\nEnter test-2 marks:";
cin>>marks2;
}
void disp_marks()
{
cout<<"\nTest-1 Marks:"<<marks1;
cout<<"\nTest-2 Marks:"<<marks2;
}
};
class Sport:virtual public Student
{
protected:
int sport_wt;
public:
void get_sport_info()
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
UNIT-III Inheritance Programs by VJTECH ACADEMY
{
cout<<"\nEnter sport weightage:";
cin>>sport_wt;
}
void disp_sport_info()
{
cout<<"\nSport weightage:"<<sport_wt;
}
};
class Result:public Test,public Sport
{
protected:
int total;
public:
void disp_total()
{
total=marks1+marks2;
cout<<"\nTotal Marks:"<<total;
}
};
void main()
{
Result r1;
clrscr();
r1.get_stud_info();
r1.get_marks();
r1.get_sport_info();
r1.disp_stud_info();
r1.disp_marks();
r1.disp_total();
r1.disp_sport_info();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23
UNIT-III Inheritance Programs by VJTECH ACADEMY
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 24
UNIT-III Inheritance Programs by VJTECH ACADEMY
Constructor in Derived class
#include<iostream.h>
#include<conio.h>
class Alpha
{
protected:
int x;
public:
Alpha(int i)
{
x=i;
}
void show_x()
{
cout<<"\nValue of x="<<x;
}
};
class Beta
{
protected:
int y;
public:
Beta(int j)
{
y=j;
}
void show_y()
{
cout<<"\nValue of y="<<y;
}
};
class Gamma:public Alpha,public Beta
{
protected:
int m,n;
public:
Gamma(int a,int b,int c,int d):Alpha(a),Beta(b)
{
m=c;
n=d;
}
void show_mn()
{
cout<<"\nValue of m="<<m;
cout<<"\nValue of n="<<n;
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 25
UNIT-III Inheritance Programs by VJTECH ACADEMY
};
void main()
{
Gamma g1(10,20,30,40);
clrscr();
g1.show_x();
g1.show_y();
g1.show_mn();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 26
UNIT-III Inheritance Programs by VJTECH ACADEMY
Constructor in Derived class
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"\nBase class constructor";
}
};
class B:public A
{
public:
B():A()
{
cout<<"\nDerived class constructor";
}
};
void main()
{
clrscr();
B b1;
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 27
UNIT-III Inheritance Programs by VJTECH ACADEMY
Write a program to implement single inheritance. Declare base class ‘Employee’ with emp_no and
emp_name. Declare derived class ‘Fitness’ with height and weight. Accept and display data for one
employee.
#include<iostream.h>
#include<conio.h>
class Employee
{
protected:
int emp_no;
char emp_name[10];
public:
void get_emp_info()
{
cout<<"\nEnter Employee No:";
cin>>emp_no;
cout<<"\nEnter Employee Name:";
cin>>emp_name;
}
void disp_emp_info()
{
cout<<"\nEmployee No:"<<emp_no;
cout<<"\nEmployee Name:"<<emp_name;
}
};
class Fitness:public Employee
{
protected:
float height,weight;
public:
void get_fitness_info()
{
cout<<"\nEnter employee height:";
cin>>height;
cout<<"\nEnter employee weight:";
cin>>weight;
}
void disp_fitness_info()
{
cout<<"\nEmployee Height:"<<height;
cout<<"\nEmployee Weight:"<<weight;
}
};
void main()
{
Fitness f1;
clrscr();
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 28
UNIT-III Inheritance Programs by VJTECH ACADEMY
f1.get_emp_info();
f1.get_fitness_info();
f1.disp_emp_info();
f1.disp_fitness_info();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 29
UNIT-III Inheritance Programs by VJTECH ACADEMY
#include<iostream.h>
#include<conio.h>
class Staff
{
protected:
int code;
public:
void get_staff_info()
{
cout<<"\nEnter staff code:";
cin>>code;
}
void disp_staff_info()
{
cout<<"\nStaff Code:"<<code;
}
};
class Teacher:public Staff
{
protected:
char subject[10];
public:
void get_teacher_info()
{
cout<<"\nEnter Teacher subject:";
cin>>subject;
}
void disp_teacher_info()
{
cout<<"\nTeacher Subject:"<<subject;
}
};
class Officer:public Staff
{
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 30
UNIT-III Inheritance Programs by VJTECH ACADEMY
protected:
char grade[10];
public:
void get_officer_info()
{
cout<<"\nEnter Officer Grade:";
cin>>grade;
}
void disp_officer_info()
{
cout<<"\nOfficer Grade:"<<grade;
}
};
void main()
{
Teacher t1;
Officer o1;
clrscr();
cout<<"\n****Teacher Class****";
t1.get_staff_info();
t1.get_teacher_info();
t1.disp_staff_info();
t1.disp_teacher_info();
cout<<"\n****Officer Class****";
o1.get_staff_info();
o1.get_officer_info();
o1.disp_staff_info();
o1.disp_officer_info();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 31
UNIT-III Inheritance Programs by VJTECH ACADEMY
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 32
UNIT-III Inheritance Programs by VJTECH ACADEMY
#include<iostream.h>
#include<conio.h>
class Employee
{
protected:
int emp_id;
char emp_name[10];
public:
Employee()
{
cout<<"\nEnter employee id:";
cin>>emp_id;
cout<<"\nEnter employee name:";
cin>>emp_name;
}
void putdata()
{
cout<<"\nEmployee ID:"<<emp_id;
cout<<"\nEmployee Name:"<<emp_name;
}
};
class Salary:private Employee
{
protected:
float basic_pay,HRA,DA,CLA;
public:
Salary():Employee()
{
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 33
UNIT-III Inheritance Programs by VJTECH ACADEMY
cout<<"\nEnter basic pay:";
cin>>basic_pay;
cout<<"\nEnter HRA:";
cin>>HRA;
cout<<"\nEnter DA:";
cin>>DA;
cout<<"\nEnter CLA:";
cin>>CLA;
}
void Calculate_Salary()
{
putdata();
float gross_salary;
gross_salary=(basic_pay+HRA+DA+CLA);
cout<<"\nBasic Pay:"<<basic_pay;
cout<<"\nHRA:"<<HRA;
cout<<"\nDA:"<<DA;
cout<<"\nCLA:"<<CLA;
cout<<"\nGross Salary:"<<gross_salary;
}
};
void main()
{
clrscr();
Salary s1;
s1.Calculate_Salary();
getch();
}
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 34
UNIT-III Inheritance Programs by VJTECH ACADEMY
C++ Programs by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 35