Sure!
Here are more simple programming exercises based on object-oriented concepts like classes,
inheritance, constructors, destructors, and basic operators. These exercises are designed to be beginner-
friendly and to practice essential object-oriented programming skills.
Exercise 7: Student Class with Marks
Objective:
Create a class Student that stores the student's name and marks for three subjects. Implement methods
to calculate and display the average marks.
Requirements:
Create a constructor that initializes the student's name and marks.
Implement a method to calculate the average of the marks.
Implement a method to display the student's details (name and average).
Example:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
float marks[3];
public:
// Constructor to initialize student details
Student(string n, float m1, float m2, float m3) {
name = n;
marks[0] = m1;
marks[1] = m2;
marks[2] = m3;
}
// Method to calculate average marks
float average() {
return (marks[0] + marks[1] + marks[2]) / 3.0;
// Method to display student details
void displayDetails() {
cout << "Name: " << name << endl;
cout << "Average Marks: " << average() << endl;
};
int main() {
Student student1("John", 85, 90, 78);
[Link]();
return 0;
Exercise 8: Simple Bank System with Deposit and Withdrawal
Objective:
Create a simple bank system where a user can deposit and withdraw money. The system should track the
account balance and prevent overdrafts.
Requirements:
Create a class BankAccount with a private member balance.
Implement deposit and withdrawal methods.
Add a check to prevent withdrawal if the balance is insufficient.
Example:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance;
public:
// Constructor to initialize the balance
BankAccount(double initialBalance) {
if (initialBalance >= 0)
balance = initialBalance;
else
balance = 0;
// Method to deposit money
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Deposited: " << amount << endl;
} else {
cout << "Invalid deposit amount!" << endl;
// Method to withdraw money
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Withdrew: " << amount << endl;
} else {
cout << "Insufficient balance or invalid amount!" << endl;
// Method to display balance
void displayBalance() {
cout << "Current balance: " << balance << endl;
};
int main() {
BankAccount account(1000.0);
[Link]();
[Link](200.0);
[Link](500.0);
[Link]();
[Link](1000.0); // Insufficient funds
return 0;
Exercise 9: Simple Rectangle Area Calculation
Objective:
Create a Rectangle class that calculates the area of a rectangle using its length and width.
Requirements:
Create a class Rectangle with private members for length and width.
Implement a method calculateArea() to return the area.
Use a constructor to initialize the length and width.
Example:
#include <iostream>
using namespace std;
class Rectangle {
private:
double length, width;
public:
// Constructor to initialize dimensions
Rectangle(double l, double w) : length(l), width(w) {}
// Method to calculate area
double calculateArea() {
return length * width;
// Method to display rectangle details
void displayDetails() {
cout << "Length: " << length << ", Width: " << width << endl;
cout << "Area: " << calculateArea() << endl;
};
int main() {
Rectangle rect(5.0, 3.0);
[Link]();
return 0;
}
Exercise 10: Temperature Conversion (Celsius to Fahrenheit)
Objective:
Create a class Temperature that converts a temperature from Celsius to Fahrenheit and vice versa.
Requirements:
Create a class Temperature with a private member for the temperature in Celsius.
Implement methods to convert Celsius to Fahrenheit and Fahrenheit to Celsius.
Provide a method to display the converted temperature.
Example:
#include <iostream>
using namespace std;
class Temperature {
private:
double celsius;
public:
// Constructor to initialize the temperature in Celsius
Temperature(double temp) : celsius(temp) {}
// Method to convert Celsius to Fahrenheit
double toFahrenheit() {
return (celsius * 9.0 / 5.0) + 32;
// Method to convert Fahrenheit to Celsius
double toCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5.0 / 9.0;
}
// Method to display temperature in Celsius and Fahrenheit
void display() {
cout << "Temperature in Celsius: " << celsius << endl;
cout << "Temperature in Fahrenheit: " << toFahrenheit() << endl;
};
int main() {
Temperature temp(25.0); // 25°C
[Link]();
cout << "Converting 77°F to Celsius: " << [Link](77.0) << endl; // 77°F to Celsius
return 0;
Exercise 11: Class to Calculate Factorial
Objective:
Create a class FactorialCalculator to compute the factorial of a number.
Requirements:
Create a method calculateFactorial() that calculates the factorial using recursion.
Implement a constructor to initialize the number.
Example:
#include <iostream>
using namespace std;
class FactorialCalculator {
private:
int number;
public:
// Constructor to initialize the number
FactorialCalculator(int n) : number(n) {}
// Method to calculate factorial using recursion
int calculateFactorial() {
if (number <= 1)
return 1;
else
return number * FactorialCalculator(number - 1).calculateFactorial();
};
int main() {
FactorialCalculator fc(5);
cout << "Factorial of 5: " << [Link]() << endl;
return 0;
Exercise 12: Simple Calculator (Addition, Subtraction, Multiplication)
Objective:
Create a simple Calculator class that performs addition, subtraction, and multiplication.
Requirements:
Implement methods for addition, subtraction, and multiplication.
Create a method to display the result of each operation.
Example:
#include <iostream>
using namespace std;
class Calculator {
public:
// Method to add two numbers
double add(double a, double b) {
return a + b;
// Method to subtract two numbers
double subtract(double a, double b) {
return a - b;
// Method to multiply two numbers
double multiply(double a, double b) {
return a * b;
};
int main() {
Calculator calc;
double num1 = 10.5, num2 = 5.5;
cout << "Addition: " << [Link](num1, num2) << endl;
cout << "Subtraction: " << [Link](num1, num2) << endl;
cout << "Multiplication: " << [Link](num1, num2) << endl;
return 0;
}
Exercise 13: Class for Rectangle and Square (Inheritance)
Objective:
Create a base class Rectangle and derive a class Square from it. The Square class should inherit the
properties of the Rectangle class but ensure that the length and width are the same for squares.
Requirements:
Create a Rectangle class with length and width.
Derive a Square class from Rectangle where both length and width are equal.
Display the area of both rectangle and square.
Example:
#include <iostream>
using namespace std;
class Rectangle {
protected:
double length, width;
public:
// Constructor to initialize rectangle dimensions
Rectangle(double l, double w) : length(l), width(w) {}
// Method to calculate area
double area() {
return length * width;
};
class Square : public Rectangle {
public:
// Constructor to initialize square dimensions
Square(double side) : Rectangle(side, side) {}
// Method to display area of square
void displayArea() {
cout << "Area of Square: " << area() << endl;
};
int main() {
Rectangle rect(5.0, 4.0);
cout << "Area of Rectangle: " << [Link]() << endl;
Square square(4.0);
[Link]();
return 0;
These exercises give you a variety of simple tasks that are great for practicing object-oriented
programming principles
such as inheritance, encapsulation, constructors, destructors, and overloading. They can be extended
and modified to handle more advanced tasks as you progress!
Got it! Here's a revised set of simple programming exercises that focus on inheritance without using
virtual functions. These exercises still cover the core concepts of inheritance, such as base and derived
classes, constructors, and method overriding.
Exercise 1: Shapes Inheritance (Without Virtual Functions)
Objective:
Create a base class Shape and derive classes Circle and Rectangle from it. Each derived class should
calculate and display its area.
Requirements:
Create a Shape class with a function area() that is overridden by derived classes.
Create derived classes Circle and Rectangle, each implementing its own area() function.
Display the area for each shape.
Example:
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void displayArea() {
cout << "Area: Not Defined" << endl;
};
// Derived class: Circle
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
void displayArea() {
cout << "Area of Circle: " << 3.14159 * radius * radius << endl;
};
// Derived class: Rectangle
class Rectangle : public Shape {
private:
double length, width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
void displayArea() {
cout << "Area of Rectangle: " << length * width << endl;
};
int main() {
Circle c(5.0);
Rectangle r(4.0, 6.0);
[Link]();
[Link]();
return 0;
Exercise 2: Animal Inheritance (Without Virtual Functions)
Objective:
Create a base class Animal and a derived class Dog. Each class should have a method to display a sound,
but without using virtual functions.
Requirements:
Create an Animal class with a makeSound() method.
Create a derived class Dog that overrides makeSound() to print "Bark!".
Call makeSound() for both the animal and dog.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void makeSound() {
cout << "Animal makes a sound" << endl;
};
// Derived class: Dog
class Dog : public Animal {
public:
void makeSound() {
cout << "Bark!" << endl;
};
int main() {
Animal animal;
Dog dog;
[Link](); // Animal makes a sound
[Link](); // Bark!
return 0;
}
Exercise 3: Employee Inheritance
Objective:
Create a base class Employee with a method to display details, and a derived class Manager that adds
additional functionality for bonus calculation.
Requirements:
Create an Employee class with members for name and salary and a method displayDetails().
Create a derived Manager class that adds a method calculateBonus() to increase salary.
Display details of both the employee and manager.
Example:
#include <iostream>
#include <string>
using namespace std;
// Base class: Employee
class Employee {
protected:
string name;
double salary;
public:
Employee(string n, double s) : name(n), salary(s) {}
void displayDetails() {
cout << "Employee Name: " << name << endl;
cout << "Salary: " << salary << endl;
};
// Derived class: Manager
class Manager : public Employee {
public:
Manager(string n, double s) : Employee(n, s) {}
void calculateBonus() {
salary += 2000; // Adding bonus to salary
void displayDetails() {
cout << "Manager Name: " << name << endl;
cout << "Salary after Bonus: " << salary << endl;
};
int main() {
Employee emp("John", 50000);
Manager mgr("Jane", 70000);
[Link](); // Employee details
[Link](); // Adding bonus
[Link](); // Manager details with bonus
return 0;
Exercise 4: Vehicle Inheritance
Objective:
Create a base class Vehicle with a method to display the vehicle type, and derived classes Car and Truck
that specify different fuel efficiencies.
Requirements:
Create a Vehicle class with a displayFuelEfficiency() method.
Create derived classes Car and Truck, each overriding displayFuelEfficiency() with their own
values.
Display the fuel efficiency for both the car and truck.
Example:
#include <iostream>
using namespace std;
// Base class: Vehicle
class Vehicle {
public:
void displayFuelEfficiency() {
cout << "Vehicle fuel efficiency: Unknown" << endl;
};
// Derived class: Car
class Car : public Vehicle {
public:
void displayFuelEfficiency() {
cout << "Car fuel efficiency: 25 MPG" << endl;
};
// Derived class: Truck
class Truck : public Vehicle {
public:
void displayFuelEfficiency() {
cout << "Truck fuel efficiency: 15 MPG" << endl;
};
int main() {
Vehicle* vehicle1 = new Car();
Vehicle* vehicle2 = new Truck();
vehicle1->displayFuelEfficiency(); // Car fuel efficiency: 25 MPG
vehicle2->displayFuelEfficiency(); // Truck fuel efficiency: 15 MPG
delete vehicle1;
delete vehicle2;
return 0;
Exercise 5: Bank Account Inheritance
Objective:
Create a base class Account with a method to display the balance, and a derived class SavingsAccount
that adds a method to calculate interest.
Requirements:
Create an Account class with a member for balance and a method displayBalance().
Create a SavingsAccount class that inherits from Account and adds a method calculateInterest()
to apply interest to the balance.
Display the balance for both types of accounts and calculate interest for the savings account.
Example:
#include <iostream>
using namespace std;
// Base class: Account
class Account {
protected:
double balance;
public:
Account(double b) : balance(b) {}
void displayBalance() {
cout << "Account balance: " << balance << endl;
};
// Derived class: SavingsAccount
class SavingsAccount : public Account {
public:
SavingsAccount(double b) : Account(b) {}
void calculateInterest() {
balance += balance * 0.05; // Adding 5% interest
void displayBalance() {
cout << "Savings Account balance: " << balance << endl;
};
int main() {
Account acc(1000);
SavingsAccount savAcc(1000);
[Link](); // Account balance: 1000
[Link](); // Calculate interest for savings account
[Link](); // Savings Account balance: 1050
return 0;
Exercise 6: Person and Student Inheritance
Objective:
Create a base class Person and a derived class Student. The Student class should add functionality for
storing and displaying the student’s grade.
Requirements:
Create a Person class with a constructor to initialize the name.
Create a Student class that adds functionality to store and display the grade.
Display the details for both Person and Student.
Example:
#include <iostream>
#include <string>
using namespace std;
// Base class: Person
class Person {
protected:
string name;
public:
Person(string n) : name(n) {}
void displayName() {
cout << "Name: " << name << endl;
};
// Derived class: Student
class Student : public Person {
private:
char grade;
public:
Student(string n, char g) : Person(n), grade(g) {}
void displayGrade() {
cout << "Grade: " << grade << endl;
};
int main() {
Person p("Alice");
Student s("Bob", 'A');
[Link]();
[Link]();
[Link]();
return 0;
}
These exercises should help you understand the core concepts of inheritance in object-oriented
programming. The derived classes extend the functionality of the base classes, while avoiding the use of
virtual functions. The goal is to practice method overriding, constructors, and destructors while keeping
things simple and clear!