Call us at 0700-922-6559 or Click here

Consider an example of declaring the examination result. Design three classes student, exam, and result. The student has data members such as roll no, and name. Create the lass exam by inheriting the student class. The exam class adds data members representing the marks scored in 5 subjects. Derive the result from exam-class and it has own data members like total, avg.

#include <iostream>

using namespace std;

// Base class Student

class Student {

protected:

   int rollno;

   string name;

public:

   void get_student_details() {

       cout << “Enter roll number: “;

       cin >> rollno;

       cout << “Enter name: “;

       cin >> name;   }

   void put_student_details() {

       cout << “Roll number: ” << rollno << endl;

       cout << “Name: ” << name << endl;   }   };

// Derived class Exam

class Exam : public Student {

private:

   int marks[5];

public:

   void get_marks() {

       cout << “Enter marks in 5 subjects:\n”;

       for (int i = 0; i < 5; i++) {

           cin >> marks[i];      }   }   };

// Derived class Result

class Result : public Exam {

private:

   int total;

   float avg;

public:

   void calculate_result() {

       total = 0;

       for (int i = 0; i < 5; i++) {

           total += marks[i];     }

       avg = total / 5.0;   }

   void display_result() {

       put_student_details();  // Call base class function

       cout << “Total marks: ” << total << endl;

       cout << “Average: ” << avg << endl;   }    };

int main() {

   Result student1;

   cout << “\nEnter student details and marks:\n”;

   student1.get_student_details();

   student1.get_marks();

   student1.calculate_result();

   cout << “\nStudent result:\n”;

   student1.display_result();

   return 0;

}

Output:

Enter student details and marks:

Enter roll number: 123

Enter name: Priya

Enter marks in 5 subjects:

80 75 90 85 95

Student result:

Roll number: 123

Name: Priya

Total marks: 425

Average: 85.00

Here’s a step-by-step explanation of the code:

1. Header and Namespace:

  • #include <iostream>: Includes the iostream header for input/output operations.
  • using namespace std;: Brings the std namespace into scope for convenient use of its elements.

2. Base Class Student:

  • class Student { … }: Defines the base class Student, representing basic student information.
    • protected: int rollno; string name;: Protected members, accessible to derived classes, for roll number and name.
    • public: void get_student_details() { … }; void put_student_details() { … };: Public member functions to get and display student details.

3. Derived Class Exam:

  • class Exam : public Student { … }: Derived class representing exam details, inheriting from Student.
    • private: int marks[5];: Private member to store marks in 5 subjects.
    • public: void get_marks() { … };: Public member function to get marks from the user.

4. Derived Class Result:

  • class Result : public Exam { … }: Derived class representing exam results, inheriting from Exam.
    • private: int total; float avg;: Private members to store total marks and average.
    • public: void calculate_result() { … }; void display_result() { … };: Public member functions to calculate and display results.

5. Main Function:

  • int main() { … }: The program’s entry point.
    • Result student1;: Creates an object of the Result class.
    • **Prompts for student details and marks, calling appropriate functions.
    • **Calculates and displays the result using Result class me

Leave a Reply

Your email address will not be published. Required fields are marked *