Answer to Question #240159 in C++ for lemhs

Question #240159

Create a class called ‘Student’ holding the basic information about the student including the roll.no and name.

Derive a class called ‘Academics’ from the student with the protected members for accessing three different subject marks.

Derive another class from Student called ‘Extracurricular’ with the sports mark.

Now compute the result marks(average of academics + sports marks) creating a class result inheriting academics and extracurricular


1
Expert's answer
2021-09-21T11:02:07-0400
#include <iostream>
#include <string>
using namespace std;


class Student {
private:
    string name;
    int rollNo;
public:
    Student(string name, int rollNo) : name(name), rollNo(rollNo)
    {}
    string Name() const
    { return name; }
    int RollNo() const
    { return rollNo; }
};


class Academics : public Student {
private:
    int math;
    int eng;
    int sci;
public:
    Academics(string name, int rollNo) : Student(name, rollNo)
    { }
    void SetMarks(int math, int eng, int sci) {
        this->math = math;
        this->eng = eng;
        this->sci = sci;
    }
    double GetAvrMark() const
    {
        return (math + eng + sci) / 3.0;
    }
};


class Extracurricular : public Academics {
private:
    int sport;
public:
    Extracurricular(string name, int rollNo) : Academics(name, rollNo)
    {}
    void SetSportMarks(int marks) {
        sport = marks;
    }
    double GetResultMarks() const
    {
        return GetAvrMark() + sport;
    }
};


int main()
{
    Academics st1("Jhon Doe", 123);
    st1.SetMarks(4, 3,4);
    cout << "Average marks for " << st1.Name() << " is "
         << st1.GetAvrMark() << endl;
    
    Extracurricular st2("Robert Rocky", 111);
    st2.SetMarks(2,3,1);
    st2.SetSportMarks(5);
    cout << "Result marks for " << st2.Name() << " is "
         << st2.GetResultMarks() << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS