Knowee
Questions
Features
Study Tools

Program 5 Identify the type of inheritance stated. Write C++ program to implement the scenario given using the identified inheritance. Address the ambiguity in this scenario. Class Base Data members: team_name, N -total_match, int score[N] - an array to store scores for N matches. Member function: void readscore() -to read the scores. Constructor- to initialize the data members Class Derived 1 void display (int score[size])- to print N scores. Class Derived 2 Data member : sum void display (int score[size])- to find the sum N scores. Class C-Derived 3 Member function:void average() -to find average of N scores. Create an object for class D and access the methods. Sample Input 4 90 45 78 89 Scores: 90 45 78 89 Sum:302.00 Average75.50

Question

Program 5 Identify the type of inheritance stated. Write C++ program to implement the scenario given using the identified inheritance. Address the ambiguity in this scenario. Class Base Data members: team_name, N -total_match, int score[N] - an array to store scores for N matches. Member function: void readscore() -to read the scores.
Constructor- to initialize the data members Class Derived 1 void display (int score[size])- to print N scores. Class Derived 2 Data member : sum void display (int score[size])- to find the sum N scores. Class C-Derived 3 Member function:void average() -to find average of N scores. Create an object for class D and access the methods. Sample Input 4 90 45 78 89 Scores: 90 45 78 89 Sum:302.00 Average75.50

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The type of inheritance stated in the problem is Multiple Inheritance. In C++, Multiple Inheritance is a feature where a class can inherit from more than one classes. The derived class inherits all the features from the base class.

Here is a C++ program to implement the scenario:

#include<iostream>
using namespace std;

class Base {
    protected:
    string team_name;
    int N;
    int *score;
    public:
    Base(int n) {
        N = n;
        score = new int[N];
    }
    void readscore() {
        cout << "Enter scores: ";
        for(int i=0; i<N; i++) {
            cin >> score[i];
        }
    }
};

class Derived1: public virtual Base {
    public:
    Derived1(int n):Base(n) {}
    void display() {
        cout << "Scores: ";
        for(int i=0; i<N; i++) {
            cout << score[i] << " ";
        }
        cout << endl;
    }
};

class Derived2: public virtual Base {
    protected:
    int sum;
    public:
    Derived2(int n):Base(n) {
        sum = 0;
    }
    void display() {
        for(int i=0; i<N; i++) {
            sum += score[i];
        }
        cout << "Sum: " << sum << endl;
    }
};

class Derived3: public Derived1, public Derived2 {
    public:
    Derived3(int n):Base(n),Derived1(n),Derived2(n) {}
    void average() {
        cout << "Average: " << (float)sum/N << endl;
    }
};

int main() {
    int n;
    cout << "Enter number of matches: ";
    cin >> n;
    Derived3 d(n);
    d.readscore();
    d.Derived1::display();
    d.Derived2::display();
    d.average();
    return 0;
}

In this program, we have used virtual inheritance to resolve the ambiguity problem. Virtual inheritance is a process that allows you to specify the superclass from which the subclass will inherit. This can prevent multiple entities of a multiple-inherited superclass appearing in an object's layout.

This problem has been solved

Similar Questions

Player classThe Player class will specify the attributes and behaviours of a player. An object of the Player class will have at least the following fields:Name – the name of the playerScore – the highest game score The data type of each field must be chosen carefully and you must be able to justify the choice of the data type of the fields. You may want to include comments in the class to state any assumptions made. The class must also have a default constructor and a non-default constructor that accepts a value for the name of the player.The Player class should also have appropriate accessor and mutator methods for its fields. Validation of values for fields should also be implemented. You should not allow an object of class Player to be set to an invalid state. There should be no input from the terminal or output to the screen via methods from the Player class. A Player object should also be able to return its state in the form of a String.

Write a program to demonstrate multiple inheritance. Consider two base classes worker(int code, char name, float salary), HR who calculates the different allowances like (float DA, HRA). You have another class manger with calculates (float TA(is 10% of salary), gross salary) where the properties of worker and HR is used in this class. Write a test application class to create an array of 10 workers and display their details with gross salary.

Single File Programming QuestionProblem StatementJamie is managing a school and is looking to find out the number of students in the class based on the sum of their scores and the average score.He wants to design a program that takes the average score and sum of scores as input and outputs the calculated number of students in the class using pointers.Input format :The first line of input consists of an integer value n, representing the sum of scores.The second line of input consists of a floating-point value f, representing the average score.Output format :The output displays the calculated number of students in the class.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1051.0 ≤ f ≤ 100.0Sample test cases :Input 1 :187575.45Output 1 :24Input 2 :20010.25Output 2 :19Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:*

We want to calculate the total marks of each student of a class in Physics,Chemistry and Mathematics and the average marks of the class. The number of students in the class are entered by the user. Create a class named Marks with data members for roll number, name and marks. Create three other classes inheriting the Marks class, namely Physics, Chemistry and Mathematics, which are used to define marks in individual subject of each student. Roll number of each student will be generated automatically using inheritance by c++

Single File Programming QuestionProblem statementRaja is designing a simple game where players earn points based on their performance. Each player starts with a default score of 100. If a player completes a level, they earn 20 points. However, for each unsuccessful attempt, they lose 10 points. Additionally, if a player's score drops below 50, they are disqualified. Write a program that takes the number of successful and unsuccessful attempts from the user and calculates the player's final score using the Relational and Ternary operators.ExamplesInput215OutputThe player is disqualified.Explanation: In this case, the player has 2 successful attempts and 15 unsuccessful attempts.finalScore = 100 + (2 * 20) - (15 * 10) = 100 + 40 - 150 = -10Since the final score is below 50, the player is disqualified.Input5010OutputThe player's final score is 1000.Explanation: In this case, the player has 50 successful attempts and 10 unsuccessful attempts.finalScore = 100 + (50 * 20) - (10 * 10) = 100 + 1000 - 100 = 1000The final score is 1000, and it is greater than or equal to 50, so the player is not disqualified.Input format :The first line of input consists of an integer value 'n', representing the number of successful attempts.The second line of input consists of an integer value 'n1', representing the number of unsuccessful attempts.Output format :If the player is disqualified it displays as: "The player is disqualified."If the player is qualified, the final score is displayed in the format "The player's final score is [score]."Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 501 ≤ n1 ≤ 30Sample test cases :Input 1 :215Output 1 :The player is disqualified.Input 2 :1030Output 2 :The player is disqualified.Input 3 :5010Output 3 :The player's final score is 1000.Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.