In this section, we dive into several C++ projects that will help you apply what you've learned so far. These examples range from simple utilities like calculators and file encryption to more complex systems such as student management and banking applications. Each project is designed to reinforce key concepts while introducing new ones.
A basic calculator can perform arithmetic operations like addition, subtraction, multiplication, and division. This example will guide you through creating a simple console-based calculator.
1#include <iostream>23using namespace std;45int main() {6char op;7float num1, num2;89cout << "Enter operator either + or - or * or /: ";10cin >> op;1112cout << "Enter two operands: ";13cin >> num1 >> num2;1415switch(op) {16case '+':17cout << num1 << " + " << num2 << " = " << num1 + num2;18break;1920case '-':21cout << num1 << " - " << num2 << " = " << num1 - num2;22break;2324case '*':25cout << num1 << " * " << num2 << " = " << num1 * num2;26break;2728case '/':29if(num2 != 0.0)30cout << num1 << " / " << num2 << " = " << num1 / num2;31else32cout << "Error! Division by zero is not allowed.";33break;3435default:36// operator doesn't match any case constant (+, -, *, /)37cout << "Error! operator is not correct";38break;39}4041return 0;42}
Enter operator either + or - or * or /: + Enter two operands: 5.2 4.8 5.2 + 4.8 = 10
A student management system can help manage student records, including adding, updating, and displaying student information.
1#include <iostream>2#include <string>34using namespace std;56class Student {7public:8string name;9int age;10float gpa;1112void display() {13cout << "Name: " << name << endl;14cout << "Age: " << age << endl;15cout << "GPA: " << gpa << endl;16}17};1819int main() {20Student students[3];2122for(int i = 0; i < 3; i++) {23cout << "Enter details of student " << i+1 << ":" << endl;24cout << "Name: ";25cin >> students[i].name;26cout << "Age: ";27cin >> students[i].age;28cout << "GPA: ";29cin >> students[i].gpa;30}3132cout << "33Student Details:" << endl;34for(int i = 0; i < 3; i++) {35students[i].display();36}3738return 0;39}
Enter details of student 1: Name: Alice Age: 20 GPA: 3.5 Enter details of student 2: Name: Bob Age: 22 GPA: 3.8 Enter details of student 3: Name: Charlie Age: 19 GPA: 3.7 Student Details: Name: Alice Age: 20 GPA: 3.5 Name: Bob Age: 22 GPA: 3.8 Name: Charlie Age: 19 GPA: 3.7
Tic-tac-toe is a classic two-player game where players take turns marking spaces in a 3x3 grid with X or O, trying to get three of their marks in a row.
1#include <iostream>2using namespace std;34char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};56void displayBoard() {7cout << "-------------" << endl;8for (int i = 0; i < 3; i++) {9cout << "| ";10for (int j = 0; j < 3; j++)11cout << board[i][j] << " | ";12cout << endl;13cout << "-------------" << endl;14}15}1617bool checkWin(char player) {18// Check rows, columns and diagonals19for(int i=0; i<3; i++) {20if(board[i][0]==player && board[i][1]==player && board[i][2]==player)21return true;22if(board[0][i]==player && board[1][i]==player && board[2][i]==player)23return true;24}25if(board[0][0]==player && board[1][1]==player && board[2][2]==player)26return true;27if(board[0][2]==player && board[1][1]==player && board[2][0]==player)28return true;2930return false;31}3233int main() {34char currentPlayer = 'X';35bool gameWon = false;36int row, col;3738while(!gameWon) {39displayBoard();40cout << "Player " << currentPlayer << ", enter your move (row and column): ";41cin >> row >> col;4243if(row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ') {44board[row][col] = currentPlayer;45gameWon = checkWin(currentPlayer);46if(gameWon) {47displayBoard();48cout << "Congratulations! Player " << currentPlayer << " wins!" << endl;49}50else {51// Switch player52currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';53}54}55else {56cout << "Invalid move. Try again." << endl;57}58}5960return 0;61}
------------- | | | | ------------- | | X | | ------------- | | | | ------------- Player O, enter your move (row and column): 1 1 ------------- | | O | | ------------- | | X | | ------------- | | | | ------------- Congratulations! Player X wins!
A banking system can handle basic operations like creating accounts, depositing money, withdrawing money, and displaying account details.
1#include <iostream>2#include <string>3using namespace std;45class Account {6public:7string name;8int accNo;9float balance;1011void display() {12cout << "Account Holder: " << name << endl;13cout << "Account Number: " << accNo << endl;14cout << "Balance: $" << balance << endl;15}1617void deposit(float amount) {18balance += amount;19cout << "$" << amount << " deposited successfully." << endl;20}2122bool withdraw(float amount) {23if(amount > balance) {24cout << "Insufficient balance!" << endl;25return false;26}27balance -= amount;28cout << "$" << amount << " withdrawn successfully." << endl;29return true;30}31};3233int main() {34Account accounts[2];3536for(int i = 0; i < 2; i++) {37cout << "Enter details of account " << i+1 << ":" << endl;38cout << "Name: ";39cin >> accounts[i].name;40cout << "Account Number: ";41cin >> accounts[i].accNo;42cout << "Initial Balance: $";43cin >> accounts[i].balance;44}4546int choice, accNo, amount;47while(true) {48cout << "491. Deposit502. Withdraw513. Display Account Details524. Exit" << endl;53cout << "Enter your choice: ";54cin >> choice;5556if(choice == 4)57break;5859cout << "Enter account number: ";60cin >> accNo;6162for(int i = 0; i < 2; i++) {63if(accounts[i].accNo == accNo) {64switch(choice) {65case 1:66cout << "Enter amount to deposit: $";67cin >> amount;68accounts[i].deposit(amount);69break;70case 2:71cout << "Enter amount to withdraw: $";72cin >> amount;73accounts[i].withdraw(amount);74break;75case 3:76accounts[i].display();77break;78}79break;80}81}82}8384return 0;85}
Enter details of account 1: Name: John Account Number: 123456 Initial Balance: $500 Enter details of account 2: Name: Jane Account Number: 654321 Initial Balance: $1000 1. Deposit 2. Withdraw 3. Display Account Details 4. Exit Enter your choice: 1 Enter account number: 123456 Enter amount to deposit: $200 $200 deposited successfully. 1. Deposit 2. Withdraw 3. Display Account Details 4. Exit Enter your choice: 3 Enter account number: 123456 Account Holder: John Account Number: 123456 Balance: $700
File encryption involves converting the contents of a file into an encoded format to protect sensitive information.
1#include <iostream>2#include <fstream>3using namespace std;45void encryptFile(string inputFile, string outputFile) {6ifstream fin(inputFile);7ofstream fout(outputFile);89char ch;10while(fin.get(ch)) {11fout.put(ch + 1); // Simple encryption by shifting each character by 112}1314fin.close();15fout.close();16}1718int main() {19string inputFile, outputFile;2021cout << "Enter input file name: ";22cin >> inputFile;23cout << "Enter output file name: ";24cin >> outputFile;2526encryptFile(inputFile, outputFile);2728cout << "Encryption completed successfully." << endl;2930return 0;31}
Enter input file name: example.txt Enter output file name: encrypted.txt Encryption completed successfully.
| Concept | Description |
|---|---|
| Calculator | Simple arithmetic operations using switch statements. |
| Student Management System | Managing student records with classes and objects. |
| Tic-Tac-Toe | Implementing a classic game logic with functions and arrays. |
| Banking System | Handling bank account operations like deposit, withdraw, and display details. |
| File Encryption | Basic file encryption using character shifting for demonstration purposes. |
Congratulations on completing these projects! You've now gained hands-on experience with various C++ applications. The next step is to test your skills through exercises, quizzes, and code challenges. These will further reinforce your understanding and prepare you for more advanced topics in programming.
Move on to the Exercises, Quizzes & Code Challenges section to continue practicing and improving your C++ proficiency.