Program On Hybrid inheritance


  • Create a class student with four protected data members Sub1, Sub2, Sub3, and stu_name;
  • Create a child class test with two public member functions (getdetails() and calc_marks();)
  • Create a class sports with one protected datamember scores (out of 100) and create one member function getscores(); to get the details of scores
  • Create a child class result of the class test and sports with one public member function print_grade() after calculating the average of (sub1, Sub2, Sub3, scores) and print grade.


#include <iostream>

using namespace std;

class student
{
protected:
    int sub1,sub2,sub3;
    string stu_name;
};

class test :public student
{
public:
    void getdetails()
    {
        cout<<"\nEnter Student Name:";
        cin>>stu_name;
        cout<<"\nEnter Subject 1 Marks:";
        cin>>sub1;
        cout<<"\nEnter Subject 2 Marks:";
        cin>>sub2;
        cout<<"\nEnter Subject 3 Marks:";
        cin>>sub3;
    }

    void calc_marks()
    {
        float total;
        total=sub1+sub2+sub3;
        cout<<"\nTotal Marks is:"<<total<<" Out Of 300";
    }

};

class sports
{
protected:
    float scores;       //max input be 100
public:
    void get_scores()
    {
        cout<<"\nEnter the score in sports : (Out Of 100)";
        cin>>scores;
        cout<<"\nThe score in sports is:"<<scores<<"\tOut of 100";
    }
};

class results :public test,public sports
{
public:
    void print_grade()
    {
        float avg;
        avg=(sub1+sub2+sub3+scores)/4;
        cout<<"\nThe Average of Marks is: "<<avg;

            if (81<=avg && avg<=100)
                cout<<"\nThe Grade is : A+";
            else if (71<=avg && avg<=80)
                cout<<"\n The Grade is : B+";
            else if (51<=avg && avg<=70)
                cout<<"\n The Grade is : C+";
            else if (41<=avg && avg<=50)
                cout<<"\n The Grade is : D";
            else
                cout<<"\n Sorry!! You Are Fail";
    }
};

int main()
{
    results aa;
    aa.getdetails();
    aa.calc_marks();
    aa.get_scores();
    aa.print_grade();
}

Output:


Enter Student Name: Utkarsh

Enter Subject 1 Marks:45

Enter Subject 2 Marks:78

Enter Subject 3 Marks:86

Total Marks is:209 Out Of 300
Enter the score in sports : (Out Of 100)82

The score in sports is:82       Out of 100
The Average of Marks is: 72.75
 The Grade is: B+

If you learned from this post share with others. Happy Sharing!