Experiment No: 3
Problem Statement: Design a Student Database using multiple inheritance with the
following classes:
a) Person (Base Class) – Stores name and age.
b) Academic (Base Class) – Stores roll number and marks in 3 subjects.
c) Student (Derived Class) – Inherits from both Person and Academic,
Calculates total marks and percentage.
Objective:
To implement a C++ program that uses multiple inheritance to create a student database
containing personal and academic information and computes total marks and percentage.
Theory:
Classes and Inheritance: In object-oriented programming, inheritance allows one class to
inherit attributes and methods from another. Multiple inheritance means a class can
inherit from more than one base class.
Base Classes:
- Person: Stores name and age.
- Academic: Stores roll number and marks in three subjects.
- Derived Class:
- Student: Inherits from both Person and Academic. Adds functionality to compute total
marks and percentage.
Algorithm:
1. Create a class Person with data members name and age.
2. Create another class Academic with data members rollNumber and marks[3].
3. Create a class Student that inherits publicly from both Person and Academic.
4. Add member functions in Student to compute totalMarks and percentage.
5. Input details, compute result, and display the full student record.
UML Diagram
Program Code:
#include <iostream>
#include <string>
using namespace std;
// Base Class 1: Person
class Person {
protected:
string name;
int age;
public:
void getPersonDetails() {
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
void showPersonDetails() {
cout << "Name: " << name << "\nAge: " << age << endl;
};
// Base Class 2: Academic
class Academic {
protected:
int rollNumber;
float marks[3];
public:
void getAcademicDetails() {
cout << "Enter Roll Number: ";
cin >> rollNumber;
for (int i = 0; i < 3; ++i) {
cout << "Enter marks for Subject " << (i + 1) << ": ";
cin >> marks[i];
void showAcademicDetails() {
cout << "Roll Number: " << rollNumber << endl;
for (int i = 0; i < 3; ++i) {
cout << "Marks in Subject " << (i + 1) << ": " << marks[i] << endl;
};
// Derived Class: Student
class Student : public Person, public Academic {
private:
float totalMarks;
float percentage;
public:
void calculateResult() {
totalMarks = marks[0] + marks[1] + marks[2];
percentage = totalMarks / 3.0;
void displayResult() {
showPersonDetails();
showAcademicDetails();
cout << "Total Marks: " << totalMarks << "\nPercentage: " << percentage << "%"
<< endl;
};
int main() {
Student s;
[Link](); // Clear input buffer
[Link]();
[Link]();
[Link]();
cout << "\n--- Student Record ---" << endl;
[Link]();
return 0;
Sample Output:
Enter Name: Amruta Adwant
Enter Age: 21
Enter Roll Number: 101
Enter marks for Subject 1: 85
Enter marks for Subject 2: 92
Enter marks for Subject 3: 88
--- Student Record ---
Name: Amruta Adwant
Age: 21
Roll Number: 101
Marks in Subject 1: 85
Marks in Subject 2: 92
Marks in Subject 3: 88
Total Marks: 265
Percentage: 88.3333%
Viva Questions:
1. What is multiple inheritance in C++?
2. Why do we use protected instead of private in base classes?
3. What is the purpose of [Link]()?
4. Can constructors be inherited in C++?
5. What is ambiguity in multiple inheritance and how is it resolved?
6. What is the difference between public, protected, and private inheritance?
7. Can we access private members of base class directly in the derived class?
8. What would happen if both base classes had a function with the same name?
9. How do you calculate the percentage from total marks?
10. Can a class inherit from more than two classes in C++?
Advance Learners
1. Modify the program to include grade assignment based on percentage.
2. Add file handling to store and retrieve student data.
3. Allow the entry of multiple student records and display them in sorted order.