Program For Different Types Of INHERITANCES

                      

1. Simple Inheritance.
Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;

class student
{
protected:
    int Roll_No;
    char name[20];
    char branch[10];
public:
    void getData()
    {
        cout<<"Enter the name: ";
        cin.getline(name,20);
        cout<<"Enter the Branch :";
        cin.getline(branch,10);
        cout<<"Enter the Roll_No : ";
        cin>>Roll_No;
    }
};

class test : public student
{
private:
    float marks;
public:
    void getMarks()
    {
        getData();
        cout<<"\nEnter the Marks :";
        cin>>marks;
    }
 
  void show()
    {
        cout<<"\nName :"<<name;
        cout<<"\nRoll No. :"<<Roll_No;
        cout<<"\nBranch :"<<branch;
        cout<<"\nMarks :"<<marks;
    }
};

int main()
{
    test t;
    t.getMarks();
    t.show();

}


2.Multiple Inheritance

Code:

#include<conio.h>
#include<iostream>
#include<stdio.h>
using namespace std;

class student
{
protected:
    int Roll_No;
    char name[20];
public:
    void getData()
    {
        cout<<"Enter the Name : ";
        gets(name);
        cout<<"Enter the Roll Number : ";
        cin>>Roll_No;

    }
};

class test
{
protected:
    float Sci;
    float maths;
    float english;
public:
    void getMarks()
    {
        cout<<"\nEnter the marks of Science :";
        cin>>Sci;
        cout<<"Enter the marks of Maths   :";
        cin>>maths;
        cout<<"Enter the marks of English :";
        cin>>english;
    }};

class result : public student , public test
{
protected:
    float per;
public:
    void calresult()
    {
        per=maths+Sci+english;
        per=per/3;
    }
    void show()
    {
        cout<<"\nName      :"<<name;
        cout<<"\nRoll_No   :"<<Roll_No;
        cout<<"\nPercentage:"<<per<<"%\n";
    }
};

int main()
{   result stud;
    stud.getData();
    stud.getMarks();
    stud.calresult();
    stud.show();
}

3. Multilevel Inheritance

Code:

#include<conio.h>
#include<iostream>
#include<stdio.h>
using namespace std;

class student
{
protected:
    int Roll_No;
    char name[20];
public:
    void getData()
    {
        cout<<"Enter the Name : ";
        gets(name);
        cout<<"Enter the Roll Number : ";
        cin>>Roll_No;

    }
};

class test:public student
{
protected:
    float Sci;
    float maths;
    float english;
public:
    void getMarks()
    {
        cout<<"\nEnter the marks of Science :";
        cin>>Sci;
        cout<<"Enter the marks of Maths   :";
        cin>>maths;
        cout<<"Enter the marks of English :";
        cin>>english;
    }
};

class result :public test
{
protected:
    float per;
public:
    void calresult()
    {
        per=maths+Sci+english;
        per=per/3;
    }
    void show()
    {
        cout<<"\nName      :"<<name;
        cout<<"\nRoll_No   :"<<Roll_No;
        cout<<"\nPercentage:"<<per<<"%\n";
    }
};

int main()
{
    result stud;
    stud.getData();
    stud.getMarks();
    stud.calresult();
    stud.show();
}

4. Hierarchical Inheritance

Code:

#include<conio.h>
#include<iostream>
#include<stdio.h>
using namespace std;

class student
{
protected:
    int Roll_No;
    char name[20];
public:
    void getData()
    {
        cout<<"Enter the Name : ";
        gets(name);
        cout<<"Enter the Roll Number : ";
        cin>>Roll_No;

    }
};

class test:public student
{
public:
    float Sci;
    float maths;
    float english;

    void getMarks()
    {
        cout<<"\nEnter the marks of Science :";
        cin>>Sci;
        cout<<"Enter the marks of Maths   :";
        cin>>maths;
        cout<<"Enter the marks of English :";
        cin>>english;
    }
    void display()
    {
        cout<<"\nName      :"<<name;
        cout<<"\nRoll_No   :"<<Roll_No;
    }
};

class result : public student
{
protected:
    float per;
public:
    void calresult(test t1)
    {
        per=t1.maths+t1.Sci+t1.english;
        per=per/3;
    }
    void show()
    {
        cout<<"\nPercentage:"<<per<<"%\n";
    }

};

int main()
{
    test t;
    t.getData();
    t.getMarks();
    result r;
    r.calresult(t);
    t.display();
    r.show();
}


5. Hybrid Inheritance

Code:

#include<conio.h>
#include<iostream>
#include<stdio.h>
using namespace std;

class student
{
protected:
    int Roll_No;
    char name[20];
public:
    void getData()
    {
        cout<<"Enter the Name : ";
        gets(name);
        cout<<"Enter the Roll Number : ";
        cin>>Roll_No;

    }
};

class test:public student
{
protected:
    float Sci;
    float maths;
    float english;
public:
    void getMarks()
    {
        cout<<"\nEnter the marks of Science :";
        cin>>Sci;
        cout<<"Enter the marks of Maths   :";
        cin>>maths;
        cout<<"Enter the marks of English :";
        cin>>english;
    }
};

class sports
{
protected:
    char grade;
public:
    void getGrade()
    {
        cout<<"\nEnter the Grade : ";
        cin>>grade;
    }
};

class result :public test , public sports
{
protected:
    float per;
public:
    void calresult()
    {
        per=maths+Sci+english;
        per=per/3;
    }
    void show()
    {
        cout<<"\nName        :"<<name;
        cout<<"\nRoll_No     :"<<Roll_No;
        cout<<"\nSports Grade:"<<grade;
        cout<<"\nPercentage  :"<<per<<"%\n";

    }
};

int main()
{
    result stud;
    stud.getData();
    stud.getMarks();
    stud.getGrade();
    stud.calresult();
    stud.show();
}



Share this

Related Posts

Previous
Next Post »

Powered by Blogger.

Popular Posts