C++: Inheritance |Multilevel inheritance

Example:1
Write a C++ program to declare a class named student with attributes as roll and name. Declare another class named physical with attributes as age, height and weight. Declare another class named marks with attibutes as m1, m2, m3, total and per.
The class physical is the derived class of class student. The class marks is the derived class of the class physical.
Take input for the details and display them.

ClassA: Student(roll,name)

ClassB: Physical(age,ht,wt)

ClassC: Marks(m1,m2,m3,total,per)
Sol:

#include<iostream>
using namespace std;
class student
{
	private:
		int roll;
		char name[20];
	public:
		void read();
		void show();
};
class physical:public student
{
	private:
		float age,ht,wt;
	public:
		void read();
		void show();
};
class marks:public physical
{
	private:
		float m1,m2,m3,total,per;
	public:
		void read();
		void cal();
		void show();
};
void student::read()
{
	cout<<"Enter roll and name ";
	cin>>roll>>name;
}
void student::show()
{
	cout<<"roll "<<roll<<" name "<<name<<endl;
}
void physical::read()
{
	student::read();
	cout<<"Enter age,height and weight ";
	cin>>age>>ht>>wt;
}
void physical::show()
{
	student::show();
	cout<<"age "<<age<<" height "<<ht<<" weight "<<wt<<endl;
}
void marks::read()
{
	physical::read();
	cout<<"Enter marks of m1,m2 and m3 ";
	cin>>m1>>m2>>m3;
}
void marks::cal()
{
	total=m1+m2+m3;
	per=total/3;
}
void marks::show()
{
	physical::show();
	cal();
	cout<<"Total "<<total<<" Per "<<per<<endl;
}
int main()
{
	marks m;
	cout<<"size of class student "<<sizeof(student)<<endl;
	cout<<"size of class physical "<<sizeof(physical)<<endl;
	cout<<"size of class marks "<<sizeof(marks)<<endl;
	m.read();
	m.show();
	return(0);
}

Output:

size of class student 24
size of class physical 36
size of class marks 56
Enter roll and name 1004
Kapil
Enter age , height and weight 12
5
36
Enter marks of m1,m2 and m3 98
97
95
roll 1004 name Kapil
age 12 height 5 weight 36
Total 290 Per 96.6667

Example:2
Write a C++ program to declare a class named employee with attributes as employee number and name. Declare another class named office with attributes as department number, department name and designation. Declare another class named salary with attributes as basic salary, hra, da, ta, cca, pf, it and net.
The class office is the derived class of class employee. The class salary is the derived class of the class office.
Take input for the details and display them.

ClassA: employee(empno,name)

ClassB: office(deptno,deptname,desig)

ClassC: salary(bsal,hra,da,ta,cca,pf,it,net)

Details are calculated as given below:
hra=15% of bsal => hra=.15*bsal;
ta=18% of bsal => ta=.18*bsal;
da=98% of bsal => da=.98*bsal;
cca=14% of bsal => cca=.14*bsal;
pf=10% of bsal => pf=.10*bsal;
it=10% of bsal => it=.10*bsal;

net=bsal+hra+da+ta+cca-pf-it;

//multi level inheritance
#include<iostream>
using namespace std;
class employee
{
	private:
		int empno;
		char name[20];
	public:
		void read();
		void show();
};
class office:public employee
{
	private:
		int dno;
		char dname[20];
		char desig[20];
	public:
		void read();
		void show();
};
class salary:public office
{
	private:
		float bsal,hra,da,ta,cca,pf,it,net;
	public:
		void read();
		void cal();
		void show();
};
void employee::read()
{
	cout<<"Enter empno and name ";
	cin>>empno>>name;
}
void employee::show()
{
	cout<<"Empno "<<empno<<" name "<<name<<endl;
}
void office::read()
{
	employee::read();
	cout<<"Enter dno , dname and desig ";
	cin>>dno>>dname>>desig;
}
void office::show()
{
	employee::show();
	cout<<"dno "<<dno<<" dname "<<dname<<" desig "<<desig<<endl;
}
void salary::read()
{
	office::read();
	cout<<"Enter Basic salary ";
	cin>>bsal;
}
void salary::cal()
{
	hra=.15*bsal;
	ta=.18*bsal;
	da=.98*bsal;
	cca=.15*bsal;
	pf=.10*bsal;
	it=.10*bsal;
	net=bsal+hra+da+ta+cca-pf-it;
}
void salary::show()
{
	office::show();
	cal();
	cout<<"Basic Salary "<<bsal<<" Net Salary "<<net<<endl;
}
int main()
{
	salary s;
	cout<<"size of class employee "<<sizeof(employee)<<endl;
	cout<<"size of class office "<<sizeof(office)<<endl;
	cout<<"size of class salary "<<sizeof(salary)<<endl;
	s.read();
	s.show();
	return(0);
}

Output:

size of class employee 24
size of class office 68
size of class salary 100
Enter empno and name 1006
Mohit
Enter dno , dname and desig 10
Sales
Manager
Enter Basic salary 45000
Empno 1006 name Mohit
dno 10 dname Sales desig Manager
Basic Salary 45000 Net Salary 101700