C++: Inheritance |Single inheritance

Example:4
Declare a class named student with attributes as roll and name. Declare another class named marks with attributes as m1,m2,m3,total per, and grade. The class marks is the derived class of the class student. Take input for the details and display them.

Base Class:Student(roll,name)
Derived class:Marks(m1,m2,m3,total,per,grade)
Sol:

//single inheritance
#include<iostream>
#include<string.h>
using namespace std;
class student
{
	private:
		int roll;
		char name[20];
	public:
		void read();
		void show();
};
class marks:public student
{
	private:
		float m1,m2,m3,total,per;
		char grade[5];
	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 marks::read()
{
	student::read();
	cout<<"Enter marks of m1,m2 and m3 ";
	cin>>m1>>m2>>m3;
}
void marks::cal()
{
	total=m1+m2+m3;
	per=total/3;
	if(per>=90)
	strcpy(grade,"A+");
	else
	 if(per>=80)
	 strcpy(grade,"A");
	 else
	  if(per>=70)
	  strcpy(grade,"B+");
	  else
	   if(per>=60)
	   strcpy(grade,"B");
	   else
	    if(per>=50)
	    strcpy(grade,"C");
	    else
	     if(per>=40)
	     strcpy(grade,"D");
	     else
	     strcpy(grade,"F");
}
void marks::show()
{
	student::show();
	cal();
	cout<<"Total "<<total<<" Per "<<per<<" Grade "<<grade<<endl;
}
int main()
{
	marks m;
	m.read();
	m.show();
	return(0);
}

Output:

Enter roll and name 1001
Kishan
Enter marks of m1,m2 and m3 98
97
95
Roll 1001 name Kishan
Total 290 Per 96.6667 Grade A+

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

Base class: employee (empno,name)
Derived class :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;

Sol:

//single inheritance
#include<iostream>
using namespace std;
class employee
{
	private:
		int empno;
		char name[20];
	public:
		void read();
		void show();
};
class salary:public employee
{
	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 salary::read()
{
	employee::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()
{
	employee::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 salary "<<sizeof(salary)<<endl;
	s.read();
	s.show();
	return(0);
}

Output:

size of class employee 24
size of class salary 56
Enter empno and name 1005
Sumit
Enter Basic salary 54000
Empno 1005 name Sumit
Basic Salary 54000 Net Salary 122040