C++ is widely used in real-world applications across domains like game development, finance, operating systems, and robotics. In this tutorial, we’ll explore real-life examples of C++ programs that showcase its practical use in solving everyday problems. These examples are perfect for understanding how C++ concepts apply to real-world scenarios.
1. Bank Account Management
This program demonstrates a simple bank account system with deposit and withdrawal functionality.
#include <iostream>
using namespace std;
class BankAccount {
private:
string accountHolder;
double balance;
public:
BankAccount(string name, double initialBalance) {
accountHolder = name;
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
cout << "Deposited: $" << amount << ". New balance: $" << balance << endl;
}
void withdraw(double amount) {
if (amount > balance) {
cout << "Insufficient funds!" << endl;
} else {
balance -= amount;
cout << "Withdrawn: $" << amount << ". Remaining balance: $" << balance << endl;
}
}
void displayBalance() {
cout << "Account Holder: " << accountHolder << ", Balance: $" << balance << endl;
}
};
int main() {
BankAccount myAccount("John Doe", 1000);
myAccount.displayBalance();
myAccount.deposit(500);
myAccount.withdraw(200);
myAccount.withdraw(1500);
return 0;
}
Output:
Account Holder: John Doe, Balance: $1000
Deposited: $500. New balance: $1500
Withdrawn: $200. Remaining balance: $1300
Insufficient funds!
2. Student Grading System
This program calculates and assigns grades based on a student’s marks.
#include <iostream>
using namespace std;
char calculateGrade(float marks) {
if (marks >= 90) return 'A';
else if (marks >= 80) return 'B';
else if (marks >= 70) return 'C';
else if (marks >= 60) return 'D';
else return 'F';
}
int main() {
string name;
float marks;
cout << "Enter student name: ";
cin >> name;
cout << "Enter marks (out of 100): ";
cin >> marks;
char grade = calculateGrade(marks);
cout << "Student: " << name << ", Marks: " << marks << ", Grade: " << grade << endl;
return 0;
}
Input:
Alice
85
Output:
Student: Alice, Marks: 85, Grade: B
3. Restaurant Billing System
This program calculates the bill for a restaurant order.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, double> menu = {{"Burger", 5.99}, {"Pizza", 8.99}, {"Fries", 2.99}, {"Soda", 1.49}};
string item;
char choice;
double total = 0;
cout << "Menu:\n";
for (auto pair : menu) {
cout << pair.first << " - $" << pair.second << endl;
}
do {
cout << "Enter item to order: ";
cin >> item;
if (menu.find(item) != menu.end()) {
total += menu[item];
} else {
cout << "Item not on menu!" << endl;
}
cout << "Add another item? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << "Total bill: $" << total << endl;
return 0;
}
Input:
Pizza
y
Fries
n
Output:
Total bill: $11.98
4. Library Management System
This example simulates a basic library system where books can be added and displayed.
#include <iostream>
#include <vector>
using namespace std;
class Book {
public:
string title;
string author;
Book(string t, string a) : title(t), author(a) {}
};
class Library {
private:
vector<Book> books;
public:
void addBook(string title, string author) {
books.push_back(Book(title, author));
}
void displayBooks() {
cout << "Books in the library:\n";
for (const auto& book : books) {
cout << "Title: " << book.title << ", Author: " << book.author << endl;
}
}
};
int main() {
Library myLibrary;
myLibrary.addBook("The Great Gatsby", "F. Scott Fitzgerald");
myLibrary.addBook("To Kill a Mockingbird", "Harper Lee");
myLibrary.displayBooks();
return 0;
}
Output:
Books in the library:
Title: The Great Gatsby, Author: F. Scott Fitzgerald
Title: To Kill a Mockingbird, Author: Harper Lee
5. Weather Temperature Converter
This program converts temperatures between Celsius and Fahrenheit.
#include <iostream>
using namespace std;
double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
int main() {
double temp;
char choice;
cout << "Enter temperature: ";
cin >> temp;
cout << "Convert to (C)elsius or (F)ahrenheit? ";
cin >> choice;
if (choice == 'C' || choice == 'c') {
cout << "Temperature in Celsius: " << fahrenheitToCelsius(temp) << "°C" << endl;
} else if (choice == 'F' || choice == 'f') {
cout << "Temperature in Fahrenheit: " << celsiusToFahrenheit(temp) << "°F" << endl;
} else {
cout << "Invalid choice!" << endl;
}
return 0;
}
Input:
100
C
Output:
Temperature in Celsius: 37.7778°C
6. Employee Payroll System
This program calculates the monthly salary of an employee.
#include <iostream>
using namespace std;
class Employee {
public:
string name;
double hourlyWage;
int hoursWorked;
Employee(string n, double wage, int hours) : name(n), hourlyWage(wage), hoursWorked(hours) {}
double calculateSalary() {
return hourlyWage * hoursWorked;
}
};
int main() {
Employee emp("John Smith", 20.5, 160); // Example employee
cout << "Employee Name: " << emp.name << endl;
cout << "Monthly Salary: $" << emp.calculateSalary() << endl;
return 0;
}
Output:
Employee Name: John Smith
Monthly Salary: $3280
Summary
These real-life examples demonstrate how C++ can be applied to practical problems. From managing data to creating interactive systems, C++ remains a versatile and powerful language for building robust applications.
For more tutorials, visit The Coding College and elevate your C++ skills! 🚀