Skip to main content

Multi-level Inheritance in C++


 #include <bits/stdc++.h>
using namespace std;

class A
{

    int rollno;

public:
    void set_roll(int r)
    {
        rollno = r;
    }
    void get_roll()
    {
        cout << "The Roll No is: " << rollno << endl;
    }
};
class B : public A
{
  protected:
    float math;
    float programming;

public:
    void set_data(float m, float p)
    {
        math = m;
        programming = p;
    }
    void get_data()
    {

        cout << "The Marks of Math are: " << math << endl;
        cout << "The Marks of Programming are: " << programming << endl;
    }
};

class C : public B
{
protected:
    float per;

public:
    void display();
};
void C ::display()
{
    get_roll();
    get_data();
    cout << "You got: " << (math + programming) / 2 << "%" << endl;
}
int main()
{
    C i;
    i.set_roll(4025);
    i.set_data(89.3, 76.3);
    i.display();
    return 0;
}

Comments