C++ 36: Multilevel, Hierarchical, Multiple and Hybrid Inheritance

 Multilevel Inheritance


Example:
#include <iostream>
using namespace std;

class Person
{
protected:
    char name[20];
    int age;

public:
    void readdata()
    {
        cout << "Enter your NAme: ";
        cin >> name;
        cout << "Enter your age: ";
        cin >> age;
    }
    void showdata()
    {
        cout << "Name is: " << name << endl;
        cout << "Age is: " << age << endl;
    }
};
class Citizen : public Person
{
protected:
    char nat[10];

public:
    void readdata()
    {
        Person::readdata();
        cout << "Enter your Namtionality: " << endl;
        cin >> nat;
    }
    void showdata()
    {
        Person::showdata();
        cout << "Nationality is: " << nat << endl;
    }
};
class Student : public Citizen
{
    int roll_no;
    char dept[10];

public:
    void readdata()
    {
        Citizen::readdata();
        cout << "Enter Your Department: " << endl;
        cin >> dept;
        cout << "Enter your RollNo: " << endl;
        cin >> roll_no;
    }
    void showdata()
    {
        Citizen::showdata();
        cout << "Department is: " << dept << endl;
        cout << "Roll No: " << roll_no << endl;
    }
};
int main()
{
    Student A;
    A.readdata();
    A.showdata();

    return 0;
}

Output:

Enter your Name: Jack
Enter your age: 20
Enter your Namtionality: 
Ind
Enter Your Department:
CS
Enter your RollNo:
102
Name is: Jack
Age is: 20
Nationality is: Ind
Department is: CS
Roll No: 102


Hierarchical Inheritance






#include <iostream>
using namespace std;

class Person //Base Class
{
protected:
    char Name[20];
    int age;

public:
    void readdata(void)
    {
        cout << "Enter Name: ";
        cin >> Name;
        cout << "Enter age: ";
        cin >> age;
    }
    void showdata(void)
    {
        cout << "Name is :" << Name << endl;
        cout << "Age is : " << age << endl;
    }
};
class Student : public Person
{
    int roll;
    char dept[10];

public:
    void readdata(void)
    {
        Person::readdata();
        cout << "Enter Roll NO: ";
        cin >> roll;
        cout << "Eneter Dept.: ";
        cin >> dept;
    }
    void showdata(void)
    {
        Person::showdata();
        cout << "RollNo is :" << roll << endl;
        cout << "Dept is :" << dept << endl;
    }
};

class Employee : public Person
{
    int ENO;
    char job[10];

public:
    void readdata(void)
    {
        Person::readdata();
        cout << "Enter Empployee No: ";
        cin >> ENO;
        cout << "Enter job: ";
        cin >> job;
    }
    void showdata(void)
    {
        Person::showdata();
        cout << "Employee no: " << ENO << endl;
        cout << "Job: " << job << endl;
    }
};
int main()
{
    Person c;
    c.readdata();
    c.showdata();

    Student b;
    a.readdata();
    a.showdata();

    Employee a;
    a.readdata();
    a.showdata();
    return 0;
}

Output:

Enter Name: Jack
Enter age: 20
Name is :Jack
Age is : 20
Enter Name: James
Enter age: 22
Enter Roll NO: 102
Eneter Dept.: Cs
Name is :James
Age is : 22
RollNo is :102
Dept is :Cs
Enter Name: Haris
Enter age: 25
Enter Empployee No: 24
Enter job: Manager
Name is :Haris
Age is : 25
Employee no: 24
Job: Manager


Multiple Inheritance




#include <iostream>
using namespace std;

class Person
{
protected:
    char name[20];
    int age;

public:
    void readdata(void)
    {
        cout << "Enter name: " << endl;
        cin >> name;
        cout << "Enter age: " << endl;
        cin >> age;
    }
    void showdata(void)
    {
        cout << "Name is: " << name << endl;
        cout << "Age is: " << age << endl;
    }
};
class Citizen
{
protected:
    char nat[10];

public:
    void readdata(void)
    {
        cout << "Enter nationality: " << endl;
        cin >> nat;
    }
    void showdata(void)
    {
        cout << "Nationality is: " << nat << endl;
    }
};
class Employee : public Personpublic Citizen
{
    int EmpNo;
    char job[100];

public:
    void readdata(void)
    {
        Person::readdata();
        Citizen::readdata();
        cout << "Enter employee no: ";
        cin >> EmpNo;
        cout << "Enter job: ";
        cin >> job;
    }
    void showdata(void)
    {
        Person::showdata();
        Citizen::showdata();
        cout << "Employee no: " << EmpNo << endl;
        cout << "Job no: " << job << endl;
    }
};

int main()
{
    Employee a;
    a.readdata();
    a.showdata();

    return 0;
}

Output:

Enter name:
Hary
Enter age:
23
Enter nationality:
Ind
Enter employee no: 2
Enter job: SDM
Name is: Hary
Age is: 23
Nationality is: Ind
Employee no: 2
Job no: SDM



Hybrid Inheritance





#include <iostream.h>
#include <conio.h>

using namespace std;

class ClassA
{
public:
    int a;
};

class ClassB : public ClassA
{
public:
    int b;
};
class ClassC : public ClassA
{
public:
    int c;
};

class ClassD : public ClassBpublic ClassC
{
public:
    int d;
};

void main()
{

    ClassD obj;

    //obj.a = 10;                //Statement 1, Error
    //obj.a = 100;               //Statement 2, Error

    obj.ClassB::a = 10;  //Statement 3
    obj.ClassC::a = 100; //Statement 4

    obj.b = 20;
    obj.c = 30;
    obj.d = 40;

    cout << "\n A from ClassB : " << obj.ClassB::a;
    cout << "\n A from ClassC : " << obj.ClassC::a;

    cout << "\n B : " << obj.b;
    cout << "\n C : " << obj.c;
    cout << "\n D : " << obj.d;
}

Output:


 A from ClassB : 10
 A from ClassC : 100
 B : 20
 C : 30
 D : 40





















Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected