Answer to Question #180441 in C++ for Abdullah

Question #180441

Create a class named Marks with data members for roll number, name and totalmarks. Create another class inheriting the Marks class, namely Physics which is used to define marks in physics of each student. Display all data using member functions. The number of students in the class is 3.​


1
Expert's answer
2021-04-11T22:42:10-0400
#include <iostream>
using namespace std;
class Marks{
    int rollnumber, totalmarks;
    string name;
    public:
    Marks(int n, string s, int marks){
        name = s;
        rollnumber = n;
        totalmarks = marks;
    }
    virtual void display() const{
        cout<<"Roll number: "<<rollnumber<<endl;
        cout<<"Student name: "<<name<<endl;
        cout<<"Total marks: "<<totalmarks<<endl;
    }
};
class Physics : public Marks{
    int mark;
    public:
    typedef Marks Base;
    Physics(int n, string s, int marks, int phyc) : Marks(n, s, marks){
        mark = phyc;
    }
    void display(){
        Base::display();
        cout<<"Physics marks: "<<mark<<endl;
    }
};
int main(){
    Physics arr[3] = {Physics(1, "Peter", 475, 80), Physics(2, "John", 456, 73), Physics(3, "James", 463, 87)};
    for(int i = 0; i < 3; i++){
        arr[i].display();
        cout<<endl;
    }
    return 0;
}

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