WAPs in C++ which creates a multilevel inheritance hierarchy of Person, Employee and Teacher classes by explaining differences between public, private and protected inheritances with public, private and protected data.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Person{
private:
string lastName;
public:
string name;
void setName(string s)
{
name = s;
}
};
class Employee : public Person{
public:
string profession;
public:
int ID;
void setID(int n)
{
ID = n;
}
};
class Teacher : private Employee{
private:
int teacherCode;
public:
string subject;
void setSubj(string s)
{
subject = s;
}
};
int main()
{
Teacher t1;
// t1.setID(10); ---->this will not work as function setID is private for class Teacher
// cout<<t1.ID; so we cannot access the memeber functions of class Employee outside the class
t1.setSubj("math");
cout<<t1.subject;
// t1.setName("ABC"); ----> this will not work since class Employee is inherited private
// so all features of class Person are also inherited private
// cout<<t1.name; so we cannot access class Person member functions outside class Teacher
}
Comments
Leave a comment