Create class `Student` and `Sports` to accept marks and sports score respectively. Derive a class `Result` from both and display total score.Demonstrate multiple Inheritance.

SOLUTION....

Multiple Inheritance in C++

C++ Program: Multiple Inheritance Example


#include <iostream>
using namespace std;

// Base class 1
class Student {
protected:
    int marks;
public:
    void getMarks(int m) {
        marks = m;
    }
    void showMarks() {
        cout << "Academic Marks: " << marks << endl;
    }
};

// Base class 2
class Sports {
protected:
    int score;
public:
    void getScore(int s) {
        score = s;
    }
    void showScore() {
        cout << "Sports Score: " << score << endl;
    }
};

// Derived class (Multiple Inheritance)
class Result : public Student, public Sports {
public:
    void displayResult() {
        int total = marks + score;
        cout << "-------------------------" << endl;
        showMarks();
        showScore();
        cout << "Total Score: " << total << endl;
        cout << "-------------------------" << endl;
    }
};

// Main function
int main() {
    Result r;
    
    // Accept input
    int m, s;
    cout << "Enter academic marks: ";
    cin >> m;
    cout << "Enter sports score: ";
    cin >> s;

    // Assign values
    r.getMarks(m);
    r.getScore(s);

    // Display result
    r.displayResult();

    return 0;
}
    

OUTPUT

Leave a Reply

Your email address will not be published. Required fields are marked *