10 C++ Programs: Multilevel
Inheritance (Beginner to Expert)
Beginner - Program 1: Basic Multilevel Inheritance
**Program Statement:**
Create three classes: A, B, and C. B inherits from A, and C inherits from B. Each class displays
its own message.
**Code:**
#include <iostream>
using namespace std;
class A {
public:
void showA() {
cout << "Class A" << endl;
}
};
class B : public A {
public:
void showB() {
cout << "Class B" << endl;
}
};
class C : public B {
public:
void showC() {
cout << "Class C" << endl;
}
};
int main() {
C obj;
[Link]();
[Link]();
[Link]();
return 0;
}
Beginner - Program 2: Constructor Chaining
**Program Statement:**
Create a chain of constructors in three classes. Observe automatic calling of parent
constructors.
**Code:**
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Constructor A" << endl;
}
};
class B : public A {
public:
B() {
cout << "Constructor B" << endl;
}
};
class C : public B {
public:
C() {
cout << "Constructor C" << endl;
}
};
int main() {
C obj;
return 0;
}
Beginner - Program 3: Student Details System
**Program Statement:**
Build a system using Person → Student → Result. Collect and display full student profile.
**Code:**
#include <iostream>
using namespace std;
class Person {
protected:
string name;
public:
void setName(string n) { name = n; }
};
class Student : public Person {
protected:
int rollNo;
public:
void setRollNo(int r) { rollNo = r; }
};
class Result : public Student {
private:
float marks;
public:
void setMarks(float m) { marks = m; }
void display() {
cout << "Name: " << name << ", Roll No: " << rollNo << ", Marks: "
<< marks << endl;
}
};
int main() {
Result r;
[Link]("Ali");
[Link](101);
[Link](88.5);
[Link]();
return 0;
}
Intermediate - Program 4: Bank Account with Transaction History
**Program Statement:**
Model a banking system using Person → Account → Transaction. Show details of last
transaction.
**Code:**
#include <iostream>
using namespace std;
class Person {
protected:
string name;
public:
void setName(string n) { name = n; }
};
class Account : public Person {
protected:
int accNo;
public:
void setAccNo(int a) { accNo = a; }
};
class Transaction : public Account {
private:
float amount;
public:
void setAmount(float amt) { amount = amt; }
void display() {
cout << "Name: " << name << ", Account: " << accNo << ", Amount:
$" << amount << endl;
}
};
int main() {
Transaction t;
[Link]("Sara");
[Link](12345);
[Link](2500.75);
[Link]();
return 0;
}
Intermediate - Program 5: Employee Payroll System
**Program Statement:**
Develop an employee system using Employee → Salary → Payslip. Show salary after 10%
bonus.
**Code:**
#include <iostream>
using namespace std;
class Employee {
protected:
string name;
public:
void setName(string n) { name = n; }
};
class Salary : public Employee {
protected:
float basic;
public:
void setSalary(float s) { basic = s; }
};
class Payslip : public Salary {
public:
void displayPayslip() {
cout << "Name: " << name << ", Net Salary: $" << (basic + 0.1 *
basic) << endl;
}
};
int main() {
Payslip p;
[Link]("John");
[Link](50000);
[Link]();
return 0;
}
Intermediate - Program 6: Vehicle → Car → ElectricCar
**Program Statement:**
Create a class hierarchy for ElectricCar using multilevel inheritance. Show all specifications.
**Code:**
#include <iostream>
using namespace std;
class Vehicle {
protected:
string brand;
public:
void setBrand(string b) { brand = b; }
};
class Car : public Vehicle {
protected:
string model;
public:
void setModel(string m) { model = m; }
};
class ElectricCar : public Car {
private:
int battery;
public:
void setBattery(int b) { battery = b; }
void display() {
cout << "Brand: " << brand << ", Model: " << model << ", Battery:
" << battery << " kWh" << endl;
}
};
int main() {
ElectricCar ec;
[Link]("Tesla");
[Link]("Model 3");
[Link](75);
[Link]();
return 0;
}