Detailed C++ Practical Programs
Question: Write a C++ program to manage the bank account of a person.
Explanation:
This program allows users to deposit money, withdraw money, and check their account balance. It
uses a class to represent the bank account.
Code:
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
string accountHolder;
double balance;
public:
BankAccount(string name, double initialBalance) {
accountHolder = name;
balance = initialBalance;
void deposit(double amount) {
balance += amount;
cout << "Amount deposited: " << amount << endl;
void withdraw(double amount) {
if (amount > balance) {
cout << "Insufficient balance!" << endl;
} else {
balance -= amount;
cout << "Amount withdrawn: " << amount << endl;
void displayBalance() {
cout << "Account Holder: " << accountHolder << endl;
cout << "Current Balance: " << balance << endl;
};
int main() {
BankAccount account("John Doe", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.displayBalance();
return 0;
Sample Output:
Amount deposited: 500
Amount withdrawn: 200
Account Holder: John Doe
Current Balance: 1300
Question: Write a C++ program to demonstrate polymorphism using function
overloading.
Explanation:
This program uses function overloading to add integers, doubles, and multiple integers. Function
overloading allows multiple functions with the same name but different parameters.
Code:
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
int add(int a, int b, int c) {
return a + b + c;
};
int main() {
Calculator calc;
cout << "Sum of two integers: " << calc.add(5, 10) << endl;
cout << "Sum of two doubles: " << calc.add(5.5, 10.5) << endl;
cout << "Sum of three integers: " << calc.add(1, 2, 3) << endl;
return 0;
Sample Output:
Sum of two integers: 15
Sum of two doubles: 16
Sum of three integers: 6
Question: Write a C++ program to sort numbers in an array using selection sort.
Explanation:
This program sorts an array of numbers using the selection sort algorithm, which finds the smallest
element in each iteration and places it at the correct position.
Code:
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
swap(arr[i], arr[minIndex]);
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
Sample Output:
Sorted array: 11 12 22 25 64
Question: Write a C++ program to demonstrate multilevel inheritance.
Explanation:
This program demonstrates multilevel inheritance, where a class inherits from a derived class,
forming a chain of inheritance.
Code:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
};
class Mammal : public Animal {
public:
void walk() {
cout << "This mammal walks on land." << endl;
};
class Dog : public Mammal {
public:
void bark() {
cout << "This dog barks." << endl;
}
};
int main() {
Dog myDog;
myDog.eat();
myDog.walk();
myDog.bark();
return 0;
Sample Output:
This animal eats food.
This mammal walks on land.
This dog barks.