Section 1: Introduction to C++
C++ is a powerful and fast programming language used for building applications, games, operating systems, and more. It was developed by Bjarne Stroustrup as an extension of the C language, adding object-oriented features such as classes and inheritance.
๐น Your First C++ Program
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
๐งฉ Explanation
#include <iostream>โ Allows you to use input/output likecoutandcin.using namespace std;โ Lets you writecoutinstead ofstd::cout.int main()โ The starting point of every C++ program.cout << "Hello, World!";โ Prints text to the screen.return 0;โ Ends the program successfully.
๐ป Try It Yourself
You can test this code on an online compiler like Programiz C++ Compiler.
๐ง Exercise
Modify the program to print your own message:
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to my C++ learning website!";
return 0;
}
Section 2: Variables and Data Types
In C++, variables are used to store data values such as numbers, characters, or text. Every variable must have a name and a data type that defines what kind of value it holds.
๐น Example Program
#include <iostream>
using namespace std;
int main() {
int age = 18; // Integer variable
float height = 5.9; // Decimal number
char grade = 'A'; // Single character
string name = "Bright"; // Text
bool isStudent = true; // True or False
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Grade: " << grade << endl;
cout << "Is Student: " << isStudent << endl;
return 0;
}
๐งฉ Explanation
intโ Stores whole numbers (e.g., 10, -5, 200).floatโ Stores decimal numbers (e.g., 4.5, 3.14).charโ Stores single characters inside single quotes (e.g., 'A').stringโ Stores text inside double quotes (e.g., "Hello").boolโ Storestrueorfalsevalues.endlโ Moves to a new line in output.
๐ป Output Example
Name: Bright Age: 18 Height: 5.9 Grade: A Is Student: 1
๐ง Exercise
Try changing the values of the variables and print your own details.
#include <iostream>
using namespace std;
int main() {
string name = "YourName";
int age = 20;
float height = 6.2;
char grade = 'B';
bool isStudent = false;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Height: " << height << endl;
cout << "Grade: " << grade << endl;
cout << "Is Student: " << isStudent << endl;
return 0;
}
Section 3: Getting User Input in C++
In C++, you can take input from the user using the cin command.
It reads data entered through the keyboard and stores it in variables.
๐น Example Program
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
cin >> name; // take input from user
cout << "Enter your age: ";
cin >> age;
cout << "\nHello " << name << "! You are " << age << " years old." << endl;
return 0;
}
๐งฉ Explanation
cinโ Used to receive input from the user.>>โ Extraction operator (reads data into a variable).coutโ Used to display output on the screen.\nโ Creates a new line (like pressing Enter).
๐ป Output Example
Enter your name: Bright Enter your age: 18 Hello Bright! You are 18 years old.
โ ๏ธ Note
cin only reads one word. If you type a full name like "Bright Twumasi", it will only take "Bright".
To read full sentences or names with spaces, use getline() instead.
๐น Example Using getline()
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
cout << "Enter your full name: ";
getline(cin, fullName); // reads entire line including spaces
cout << "Hello, " << fullName << "!" << endl;
return 0;
}
๐ง Exercise
Create a program that asks the user for their name, age, and favorite color, then displays a message like:
Hello Bright, you are 18 years old and your favorite color is blue!
Section 5: IfโElse Statements (Decision Making)
Sometimes your program needs to make decisions โ for example, checking if a person is old enough to vote or if a number is positive or negative. In C++, you can do this using the if and else statements.
๐น Example Program: Check Voting Eligibility
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are eligible to vote!" << endl;
} else {
cout << "Sorry, you are too young to vote." << endl;
}
return 0;
}
๐งฉ Explanation
if (condition)โ Checks whether the condition is true.elseโ Runs when theifcondition is false.>=โ Means "greater than or equal to".
๐ป Example Output
Enter your age: 19 You are eligible to vote!
๐น Example 2: Even or Odd Number
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
๐ง Explanation
%โ Finds the remainder after division.- If a number divided by 2 has a remainder of 0, itโs even; otherwise, itโs odd.
โก Extra Tip
You can chain multiple conditions using else if to handle more possibilities.
๐น Example 3: Grade System
#include <iostream>
using namespace std;
int main() {
int score;
cout << "Enter your score: ";
cin >> score;
if (score >= 80) {
cout << "Grade: A" << endl;
} else if (score >= 70) {
cout << "Grade: B" << endl;
} else if (score >= 60) {
cout << "Grade: C" << endl;
} else if (score >= 50) {
cout << "Grade: D" << endl;
} else {
cout << "Grade: F (Fail)" << endl;
}
return 0;
}
๐ง Exercise
Create a program that asks for a user's temperature reading and tells them:
- โItโs too cold!โ if temperature is below 15ยฐC
- โNice weather!โ if itโs between 15ยฐC and 30ยฐC
- โItโs hot!โ if above 30ยฐC
๐ง C++ Basics Quiz (Sections 1โ5)
Test your knowledge from Introduction to IfโElse Statements. Select the best answer for each question and click Submit.
๐ฎ C++ Game Development
C++ is one of the best languages for building games โ it's fast, powerful, and used in big-name engines like Unreal Engine and Cocos2d-x.
๐งฉ What Youโll Learn
- How games are made with C++
- Popular game development libraries
- Building a simple console game
โ๏ธ 1๏ธโฃ How C++ Is Used in Games
Most modern game engines use C++ because it gives full control over memory and performance. For example, Unreal Engine (used for games like Fortnite) is fully written in C++.
๐งฐ 2๏ธโฃ Popular Game Libraries for C++
- SFML (Simple and Fast Multimedia Library) โ for 2D games and graphics
- SDL (Simple DirectMedia Layer) โ used for input, sound, and rendering
- OpenGL โ used for 3D graphics
- Unreal Engine โ professional AAA game engine built with C++
๐น๏ธ 3๏ธโฃ Example: Number Guessing Game in C++
Letโs start with a small console-based game to practice logic and loops. This game picks a random number, and the player must guess it!
#include <iostream>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
using namespace std;
int main() {
srand(time(0)); // Seed random number generator
int secretNumber = rand() % 10 + 1; // Random number between 1 and 10
int guess;
int tries = 0;
cout << "๐ฏ Welcome to the Number Guessing Game!" << endl;
cout << "I'm thinking of a number between 1 and 10..." << endl;
do {
cout << "Enter your guess: ";
cin >> guess;
tries++;
if (guess > secretNumber)
cout << "Too high! Try again.\n";
else if (guess < secretNumber)
cout << "Too low! Try again.\n";
else
cout << "๐ Congratulations! You guessed it in " << tries << " tries.\n";
} while (guess != secretNumber);
return 0;
}
๐ก Explanation:
srand(time(0))seeds the random number generator so the number changes each time.rand() % 10 + 1generates a random number between 1 and 10.- A
do-whileloop repeats until the correct number is guessed.
๐ Next Steps
- Try expanding the game โ add score limits or levels.
- Learn SFML to make graphical games (draw characters, play sounds, etc.).
- Build a small 2D game like Pong or Snake.
Keep practicing โ every big game started as a simple idea coded line by line.
๐ฎ C++ Game Development
C++ is one of the best languages for building games โ it's fast, powerful, and used in big-name engines like Unreal Engine and Cocos2d-x.
๐งฉ What Youโll Learn
- How games are made with C++
- Popular game development libraries
- Building a simple console game
โ๏ธ 1๏ธโฃ How C++ Is Used in Games
Most modern game engines use C++ because it gives full control over memory and performance. For example, Unreal Engine (used for games like Fortnite) is fully written in C++.
๐งฐ 2๏ธโฃ Popular Game Libraries for C++
- SFML (Simple and Fast Multimedia Library) โ for 2D games and graphics
- SDL (Simple DirectMedia Layer) โ used for input, sound, and rendering
- OpenGL โ used for 3D graphics
- Unreal Engine โ professional AAA game engine built with C++
๐น๏ธ 3๏ธโฃ Example: Number Guessing Game in C++
Letโs start with a small console-based game to practice logic and loops. This game picks a random number, and the player must guess it!
#include <iostream>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
using namespace std;
int main() {
srand(time(0)); // Seed random number generator
int secretNumber = rand() % 10 + 1; // Random number between 1 and 10
int guess;
int tries = 0;
cout << "๐ฏ Welcome to the Number Guessing Game!" << endl;
cout << "I'm thinking of a number between 1 and 10..." << endl;
do {
cout << "Enter your guess: ";
cin >> guess;
tries++;
if (guess > secretNumber)
cout << "Too high! Try again.\n";
else if (guess < secretNumber)
cout << "Too low! Try again.\n";
else
cout << "๐ Congratulations! You guessed it in " << tries << " tries.\n";
} while (guess != secretNumber);
return 0;
}
๐ก Explanation:
srand(time(0))seeds the random number generator so the number changes each time.rand() % 10 + 1generates a random number between 1 and 10.- A
do-whileloop repeats until the correct number is guessed.
๐ Next Steps
- Try expanding the game โ add score limits or levels.
- Learn SFML to make graphical games (draw characters, play sounds, etc.).
- Build a small 2D game like Pong or Snake.
Keep practicing โ every big game started as a simple idea coded line by line.
๐ฎ Play C++ Console Game (In Browser)
๐ฐ C++ in Finance and Banking
C++ plays a major role in finance, banking, and stock trading systems because of its speed and accuracy. Itโs used to build trading engines, financial modeling systems, and secure transaction platforms.
๐ฆ Why Finance Companies Use C++
- โก Speed: C++ executes millions of calculations per second โ perfect for trading and risk analysis.
- ๐ง Precision: Handles large numbers, interest rates, and currency conversions accurately.
- ๐ Security: Gives full control of memory, preventing hacking or data leaks.
- ๐ Scalability: Can run complex systems like stock exchanges or bank servers efficiently.
๐ก Example: Simple Interest Calculator in C++
#include <iostream>
using namespace std;
int main() {
double principal, rate, time, interest;
cout << "๐ฐ Enter principal amount: ";
cin >> principal;
cout << "๐ Enter rate of interest (%): ";
cin >> rate;
cout << "โฑ๏ธ Enter time (in years): ";
cin >> time;
interest = (principal * rate * time) / 100.0;
cout << "----------------------------------" << endl;
cout << "Simple Interest = " << interest << endl;
cout << "Total Amount = " << principal + interest << endl;
cout << "----------------------------------" << endl;
return 0;
}
๐ง Explanation:
principalโ The amount borrowed or invested.rateโ The interest rate (in percent).timeโ Duration in years.- Formula โ
(Principal ร Rate ร Time) / 100
๐น Try the Finance Game Below
This small simulator shows how your interest grows over time โ all in your browser!
๐ค C++ for Robotics
C++ is the main language used in robotics programming because it can directly control hardware (motors, sensors, and cameras). It runs fast and gives precise control over memory and timing โ which is very important for robots.
โ๏ธ What Robots Use C++
- ๐ Self-driving cars (Tesla, Waymo)
- ๐ฆฟ Industrial robots (used in factories)
- ๐ Drones and flying robots
- ๐ค Service robots (that move and talk)
๐ก Why C++ is Used
- โ Real-time control of hardware
- โ High performance for calculations
- โ Works with robotics libraries like ROS (Robot Operating System)
- โ Can combine with Python for AI + movement control
๐งฉ Example: Simple Robot Simulation
This code shows a simple robot simulation in C++. The robot moves forward, turns, and checks for obstacles.
#include <iostream>
using namespace std;
class Robot {
public:
void moveForward() {
cout << "๐ค Moving forward..." << endl;
}
void turnLeft() {
cout << "โฉ๏ธ Turning left..." << endl;
}
void detectObstacle(bool obstacle) {
if (obstacle) {
cout << "๐ง Obstacle detected! Turning left..." << endl;
turnLeft();
} else {
moveForward();
}
}
};
int main() {
Robot r1;
r1.detectObstacle(false);
r1.detectObstacle(true);
r1.detectObstacle(false);
return 0;
}
๐ฌ Output (in console):
๐ค Moving forward...
๐ง Obstacle detected! Turning left...
โฉ๏ธ Turning left...
๐ค Moving forward...
๐ How It Works
- Class Robot defines actions like moving and turning.
- detectObstacle() checks if something is blocking the way.
- The robot decides what to do โ just like a real robot brain.
๐ค C++ for Robotics
C++ is one of the main languages used in robotics programming because it can directly control hardware like motors, sensors, and cameras. It runs fast and gives real-time control โ which is very important for robots.
โ๏ธ What Robots Use C++
- ๐ Self-driving cars (Tesla, Waymo)
- ๐ฆฟ Industrial robots (factory arms)
- ๐ Drones and flying robots
- ๐ค Service robots (that move and talk)
๐ก Why C++ is Used
- โ Real-time hardware control
- โ Fast and efficient for calculations
- โ Works with ROS (Robot Operating System)
- โ Can combine with Python for AI and sensors
๐งฉ Example: Simple Robot Simulation
This example shows a simple robot simulation that moves and reacts to obstacles.
#include <iostream>
using namespace std;
class Robot {
public:
void moveForward() {
cout << "๐ค Moving forward..." << endl;
}
void turnLeft() {
cout << "โฉ๏ธ Turning left..." << endl;
}
void detectObstacle(bool obstacle) {
if (obstacle) {
cout << "๐ง Obstacle detected! Turning left..." << endl;
turnLeft();
} else {
moveForward();
}
}
};
int main() {
Robot r1;
r1.detectObstacle(false);
r1.detectObstacle(true);
r1.detectObstacle(false);
return 0;
}
๐ค C++ for Robotics โ Smart Maze Simulation
In real robotics, C++ helps robots make smart movement decisions when avoiding obstacles. Below is a browser simulation that mimics that logic โ the robot detects multiple obstacles and finds a new path every time!
๐ก The Power of C++
C++ is more than just a programming language โ itโs the foundation of modern technology. From high-speed financial systems ๐ฐ to advanced robotics ๐ค, from 3D games ๐ฎ to operating systems ๐ป, C++ is everywhere.
Learning C++ gives you the power to build software that is fast, reliable, and limitless. Every line of code you write can shape the future of technology.
What You Can Build with C++:
- ๐ High-performance apps & operating systems
- ๐ฎ 3D Games and Game Engines
- ๐ค Robotics and AI systems
- ๐น Financial trading software
- ๐ Compilers, browsers, and large-scale systems
Start Your Journey Today
Every expert once wrote their first #include <iostream>.
Keep learning, keep building โ and let C++ power your imagination.
๐ง C++ Master Quiz
Test your knowledge from all the sections youโve learned โ Basics, Game Development, Finance, Robotics, and the Power of C++!