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.
#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;
}
Comments
Leave a comment