This is a sample C++ program on multilevel inheritance. Using Multilevel Inheritance, this program accept input from the user and displays the details of a student or employee.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
class person
{
private:
int age;
char name[20];
public:
void read()
{
cout<<"Enter Age & Name: ";
cin>>age;
gets (name);
}
void disp()
{
cout<<'\n'<<'\n'<<"******Details Of Person******"<<'\n'<<'\n';
cout<<"Age: "<<age<<'\n';
cout<<"Name: ";
puts (name);
}
};
class student: public person
{
private:
int rollno, clss, marks;
public:
void read()
{
cout<<"Enter The Roll Number, Class & Marks";
cin>>rollno>>clss>>marks;
}
void disp()
{
cout<<"Roll Number: "<<rollno<<'\n';
cout<<"Class: "<<clss<<'\n';
cout<<"Marks: "<<marks<<'\n';
}
};
class employee: public person
{
int id;
char desig[25];
char dept[20];
public:
void read()
{
cout<<"Enter The Employee ID, Department & Designation: ";
cin>>id>>dept>>desig;
}
void disp()
{
cout<<"Employee ID: "<<id<<'\n';
cout<<"Department: "&l<t;dept<<'\n';
cout<<"Designation: "<<desig;
}
};
void main()
{
clrscr();
student S[50];
person P[50];
employee E[50];
int n, i, ch;
cout<<"Enter Limit";
cin>>n;
cout<<"******MENU******"<<'\n';
cout<<"1. Student "<<'\n';
cout<<"2. Employee "<<'\n';
cout<<"Enter Your Choice..."<<'\n';
cin>>ch;
if (ch==1)
{
for (int i=0;i<n;i++)
{
P[i].read();
S[i].read();
P[i].disp();
S[i].disp();
cout<<'\n';
}
}
else if (ch==2)
{
for (int i=0;i<n;i++)
{
P[i].read();
E[i].read();
P[i].disp();
E[i].disp();
cout<<'\n';
}
}
else
cout<<"Invalid Choice!";
getch();
}
OUTPUT:
Enter Limit
1
******MENU******
1. Student
2. Employee
Enter Your Choice...
1
Enter Age & Name:
18
Meenakshi
Enter The Roll Number, Class & Marks
24
12
95
******Details Of Person******
Age: 18
Name: Meenakshi
Roll number: 24
Class: 12
Marks: 95
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com