Skip to main content

ProwessApps

Learn · Practice · Excel

   Define the classes according to the class hierarchy in the figure . class Student store the name and roll no. of student . class Marks store the marks in three subjects . class Result store the total marks of a student. Take the input for 5 student . Find the average and display. Include all the constructor.

C++ Code Example — Inheritance Programs

ADVERTISEMENT

   Define the classes according to the class hierarchy in the figure . class Student store the name and roll no. of student . class Marks store the marks in three subjects . class Result store the total marks of a student. Take the input for 5 student . Find the average and display. Include all the constructor.

Objective

Write a C++ program to demonstrate Multi-Level Inheritance using Student, Marks, and Result classes.

Algorithm / Approach

  1. Create a base class Student to hold name and roll number.
  2. Create a class Marks that inherits from Student (public Student).
  3. Create a class Result that inherits from Marks (public Marks).
  4. Include a default constructor in every class that prints a message.
  5. In main(), create an array of 5 Result objects and calculate their totals.
main.cpp
#include<iostream>
using namespace std;
class Student {
 public:
 string name;
 int roll;
 Student() {
  cout<<"Default Const of Student\n";
  }
};
class Marks : public Student {
 public :
 int m1,m2,m3;
 Marks(){
  cout<<"Default Const of Marks\n";
 }
};
class Result : public Marks {
 public:
 int total;
 float per;
 Result() { 
  cout<<"Const. of Result "<< endl;
 }
 void input(){
  cout<<"Enter name of Student ";
  getline(cin,name);
  cout<<"Enter the roll no. ";
  cin>>roll;
  cout<<"Enter Marks of 3 subjects ";
  cin>>m1>>m2>>m3;
  cin.ignore(1,'\n');
 }
 void display() {
  total = m1+m2+m3;
  per = (m1+m2+m3)/3.0;
  cout<< name<<"\t"<< roll<<"\t";
  cout<< total<<"\t"<< per<< endl;
 }
};
int main() {
 Result s[5];
 for(int i = 0; i<5; i++) {
  s[i].input();
 }
 for(int i = 0; i<5; i++) {
  s[i].display();
 }
 return 0;
 }

Expected Output

Default Const of Student
Default Const of Marks
Const. of Result
Default Const of Student
Default Const of Marks
Const. of Result
Default Const of Student
Default Const of Marks
Const. of Result
Default Const of Student
Default Const of Marks
Const. of Result
Default Const of Student
Default Const of Marks
Const. of Result
Enter name of Student Alok
Enter the roll no. 12
Enter Marks of 3 subjects 90 90 90
Enter name of Student Deepak
Enter the roll no. 23
Enter Marks of 3 subjects 85 85 85
Enter name of Student Ayan
Enter the roll no. 34
Enter Marks of 3 subjects 99 99 99
Enter name of Student Dan
Enter the roll no. 67
Enter Marks of 3 subjects 87 78 65
Enter name of Student Dan
Enter the roll no. 56
Enter Marks of 3 subjects 50 50 50
Alok    12      270     90
Deepak  23      255     85
Ayan    34      297     99
Dan     67      230     76.6667
Dan     56      150     50

Explanation of the Program

  • Multi-Level Inheritance creates a "grandparent &rarr; parent &rarr; child" relationship.
  • When you create an object of the grandchild class (Result), C++ automatically calls the constructors from the top of the family tree downwards. It builds the Student base first, then the Marks extension, and finally the Result extension.

Complexity

Time Complexity O(n) - Where n is the number of students (5).
Space Complexity O(n)
ADVERTISEMENT