Object Oriented Programming Practical 11
Question 1
WAP to implement inheritance shown below figure. Assume suitable member function.

furniture.cpp
C
#include<iostream.h>
#include<conio.h>
class furniture
{
int price;
char material[100];
public:
void accept()
{
cout<<"\nEnter material of furniture: ";
cin>>material;
cout<<"\nEnter furniture price: ";
cin>>price;
}
void display()
{
cout<<"\nFurniture material: "<<material;
cout<<"\nFurniture number: "<<price;
}
};
class table:public furniture
{
int height;
char surface[30];
public:
void getdata()
{
cout<<"\nEnter height of table: ";
cin>>height;
cout<<"\nEnter turface of table: ";
cin>>surface;
}
void putdata()
{
cout<<"\nHeight of table: "<<height;
cout<<"\nturface of table : "<<surface;
}
};
void main()
{
table t;
t.accept();
t.getdata();
t.display();
t.putdata();
getch();
}
Output

Question 2
Write a C++ program to define a class "Employee" having data members emp_no, emp_name and emp_designation. Derive a class "Salary" from "Employee" having data members basic, hra, da, gross_sal. Accept and display data for one employee.
employee.cpp
C
#include<iostream.h>
#include<conio.h>
class Employee
{
int emp_no;
char emp_name[100],emp_designation[50];
public:
void accept()
{
cout<<"\nEnter name of employee: ";
cin>>emp_name;
cout<<"\nEnter employee number: ";
cin>>emp_no;
cout<<"\nEnter employee designation: ";
cin>>emp_designation;
}
void display()
{
cout<<"\nEmployee name: "<<emp_name;
cout<<"\nEmployee number: "<<emp_no;
cout<<"\nEmployee designation: "<<emp_designation;
}
};
class salary:public Employee
{
int basic;
float hra,da,gross_sal;
public:
void getdata()
{
cout<<"\nEnter basic salary of employee: ";
cin>>basic;
}
void putdata()
{
hra=(74.5*basic)/100;
da=(15*basic)/100;
gross_sal=basic+hra+da;
cout<<"\nBasic salary: "<<basic;
cout<<"\nHRA: "<<hra;
cout<<"\nDA : "<<da;
cout<<"\nGross salary : "<<gross_sal;
}
};
void main()
{
salary s;
s.accept();
s.getdata();
s.display();
s.putdata();
getch();
}
Output

Question 3
Write a C++ program to define a class "Student" having data members roll_no, name. Derive a class "Marks" from "Student" having data members m1,m2,m3, total and percentage. Accept and display data for one student.
student.cpp
C
#include<iostream.h>
#include<conio.h>
class student
{
int rollno;
char name[100];
public:
void accept()
{
cout<<"\nEnter name of student: ";
cin>>name;
cout<<"\nEnter roll number of student: ";
cin>>rollno;
}
void display()
{
cout<<"\nStudent name: "<<name;
cout<<"\nStudent rollno: "<<rollno;
}
};
class marks:public student
{
int m1,m2,m3,total;
float percent;
public:
void getdata()
{
cout<<"\nEnter marks 1: ";
cin>>m1;
cout<<"\nEnter marks 2: ";
cin>>m2;
cout<<"\nEnter marks 3: ";
cin>>m3;
}
void putdata()
{
total=m1+m2+m3;
percent=(total*100)/300.00;
cout<<"\nTest percent : "<<percent<<endl;
}
};
int main()
{
clrscr();
marks m;
m.accept();
m.getdata();
m.display();
m.putdata();
getch();
return 0;
}
Output

Question 4
Write a C++ program to implement following Multilevel Inheritance

person.cpp
C
#include<iostream.h>
#include<conio.h>
class person
{
int age;
char name[100],gender[10];
public:
void accept()
{
cout<<"\nEnter name of person: ";
cin>>name;
cout<<"\nEnter age of person: ";
cin>>age;
cout<<"\nEnter gender: ";
cin>>gender;
}
void display()
{
cout<<"\nPerson name: "<<name;
cout<<"\nPerson age: "<<age;
cout<<"\nPerson gender: "<<gender;
}
};
class employee:public person
{
int emp_id;
float salary;
char company[50];
public:
void getdata()
{
cout<<"\nEnter employee id: ";
cin>>emp_id;
cout<<"\nEnter company name: ";
cin>>company;
cout<<"\nEnter employee salary: ";
cin>>salary;
}
void putdata()
{
cout<<"\nEmployee id: "<<emp_id;
cout<<"\nEmployee company : "<<company;
cout<<"\nEmployee salary : "<<salary;
}
};
class programmer:public employee
{
int no_of_prog_lang_known;
public:
void get()
{
cout<<"\nEnter number of programmign languages known: ";
cin>>no_of_prog_lang_known;
}
void put()
{
cout<<"\nNumber of programmign languages known: "<<no_of_prog_lang_known;
}
};
void main()
{
clrscr();
programmer p;
p.accept();
p.getdata();
p.get();
p.display();
p.putdata();
p.put();
getch();
}
Output
