Write a c++ Program to get the average marks of six subjects using the Multiple Inheritance With output
Sure! Below is a simple C++ program that demonstrates multiple inheritance to calculate the average marks of six subjects. In this example, we'll have two base classes: `SubjectMarks` to store the marks for each subject, and `AverageMarks` to compute the average using the marks from `SubjectMarks`. The derived class `Student` will inherit from both base classes.
Here's the implementation:
```cpp
#include <iostream>
using namespace std;
// Base class to hold marks for six subjects
class SubjectMarks {
protected:
float marks[6];
public:
void inputMarks() {
cout << "Enter marks for six subjects:\n";
for (int i = 0; i < 6; i++) {
cout << "Subject " << (i + 1) << ": ";
cin >> marks[i];
}
}
};
// Base class to calculate average marks
class AverageMarks {
protected:
float average;
public:
void calculateAverage(float marks[]) {
float sum = 0;
for (int i = 0; i < 6; i++) {
sum += marks[i];
}
average = sum / 6;
}
void displayAverage() {
cout << "Average Marks: " << average << endl;
}
};
// Derived class that inherits from SubjectMarks and AverageMarks
class Student : public SubjectMarks, public AverageMarks {
public:
void compute() {
inputMarks(); // Get marks from user
calculateAverage(marks); // Calculate average
displayAverage(); // Display average
}
};
int main() {
Student student; // Create an instance of Student
student.compute(); // Call compute to process marks and output average
return 0;
}
```
### How the Program Works:
1. **SubjectMarks Class**: This class contains an array to store the marks of six subjects and a function to input these marks.
2. **AverageMarks Class**: This class contains a function to compute the average of the marks provided from the `SubjectMarks` class's array and a function to display the average.
3. **Student Class**: This class inherits from both `SubjectMarks` and `AverageMarks`. It has a method `compute()` that orchestrates the calling of input and processing functions.
4. **Main Function**: The main function creates an instance of the `Student` class and calls the `compute()` method, which handles the entire process.
### Example Output
Below is an example of what the output would look like when a user runs the program and provides input:
```
Enter marks for six subjects:
Subject 1: 85
Subject 2: 90
Subject 3: 78
Subject 4: 88
Subject 5: 92
Subject 6: 95
Average Marks: 88.6667
```
### Note:
- Ensure the input values for the marks are valid numbers (0-100 for marks).
- C++ compilers need to be set up for your execution environment and you should compile it properly to see the results.