Hierarchical Inheritance: In this inheritance,the properties or functions of one parent class is inherited by multiple or more child class.
syntax: class Parent
{
code specific to parent
};
class Child: visibility mode Parent
{
code related to child
};
class ChildOne : visibility mode Parent
{
code related to ChildOne
};
Example:
1. Create a class called Student with two data member to represent name and age of the student. Use member function to read and print these data. From this class, derive a class called Boarder with data member to represent room number.
Derive another class called DayScholar from the class Student with data member to represent address and bus number of the student. In both derived classes,
use member function to read and print the respective data.
#include<iostream.h>.
#include<conio.h>
class Student
{
protected:
char name[20];
int roll;
public:
void inputs()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter roll no:";
cin>>roll;
}
void prints()
{
cout<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
}
};
class Boarder : public Student
{
int roomnumber;
public :
void inputb()
{
cout<<"Enter room number:";
cin>>roomnumber;
}
void printb()
{
cout<<"Room Number:"<<roomnumber;
}
};
class DayScholar: public Student
{
char address[20];
int busnumber;
public:
void inputda()
{
cout<<"Enter the address:";
cin>>address;
cout<<"Enter bus number:";
cin>>busnumber;
}
void printda()
{
cout<<"Address:"<<address<<endl<<"Bus Number:"<<busnumber<<endl;
}
};
void main()
{
Boarder b;
DayScholar d;
b.inputs();
b.inputb();
cout<<endl;
b.prints();
b.printb();
cout<<endl;
d.inputs();
d.inputda();
cout<<endl;
d.prints();
d.printda();
getch();
}
2.*Q1. Write a program to create a class named Person which has name and age as data members and member functions to read and display its data.
Create another class Student derived from class Person which has roll and marks and another different derived class Employee having employeeid and salary.Display the details?
#include<iostream.h>
#include<conio.h>
class Person
{
protected:
char name[50];
int age;
public:
void setPerson()
{
cout<<endl;
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void displayPerson()
{
cout<<"Name:"<<name<<endl<<"Age:"<<age<<endl;
}
};
class Student :public Person
{
protected:int roll;
int marks;
public:void setStudent()
{
cout<<"Enter roll:";
cin>>roll;
cout<<"Enter marks:";
cin>>marks;
}
void displayStudent()
{
cout<<"Roll:"<<roll<<endl<<"Marks:"<<marks<<endl;
}
};
class Employee: public Person
{
int employeeid;
int salary;
public: void setEmployee()
{
cout<<"Enter employeeid:";
cin>>employeeid;
cout<<"Enter salary:";
cin>>salary;
}
void displayEmployee()
{
cout<<"Employee id:"<<employeeid<<endl<<"Salary:"<<salary<<endl;
}
};
void main()
{
Employee E;
Student S;
cout<<"Details of Student"<<endl;
E.setPerson();
S.setStudent();
cout<<endl;
E.displayPerson();
S.displayStudent();
cout<<endl;
cout<<"Details of Employee"<<endl;
E.setEmployee();
E.displayEmployee();
getch();
}
Comments
Post a Comment