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, per and grade.
The class marks is the derived class of the class student and the class physical. Take input for the details and display them.
//Multiple inheritance #include<iostream> using namespace std; class student { private: int roll; char name[20]; public: void read(); void show(); }; class physical { private: float age,ht,wt; public: void read(); void show(); }; class marks:public student,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() { cout<<"Enter age , height and weight "; cin>>age>>ht>>wt; } void physical::show() { cout<<"age "<<age<<" height "<<ht<<" weight "<<wt<<endl; } void marks::read() { student::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() { student::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 12
size of class marks 56
Enter roll and name 101
Kapil
Enter age , height and weight 12
2
36
Enter marks of m1,m2 and m3 98
97
95
roll 101 name Kapil
age 12 height 2 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 salary is the derived class of the class employee and the class office. Take input for the details and display them.
//Multiple inheritance #include<iostream> using namespace std; class employee { private: int empno; char name[20]; public: void read(); void show(); }; class office { private: int dno; char dname[20]; char desig[20]; public: void read(); void show(); }; class salary:public employee,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() { cout<<"Enter dno , dname and Designation "; cin>>dno>>dname>>desig; } void office::show() { cout<<"dno "<<dno<<" dname "<<dname<<" Desig "<<desig<<endl; } void salary::read() { employee::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() { employee::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 44
size of class salary 100
Enter empno and name 1001
Anmol
Enter dno , dname and Designation 10
Sales
Manager
Enter Basic salary 45000
Empno 1001 name Anmol
dno 10 dname Sales Desig Manager
Basic Salary 45000 Net Salary 101700