Let's get started with C++!
Here are some notes for beginners, covering the fundamentals:
1. What is C++?
C++ is a powerful and versatile programming language. It's used for developing a wide range of
applications, including:
System software: Operating systems, device drivers
Game development: High-performance games
Application software: Desktop applications, mobile apps
Embedded systems: Software for devices like microcontrollers
High-performance computing: Scientific simulations, financial modeling
2. Basic Structure of a C++ Program:
C++
#include <iostream> // Include header for input/output operations
int main() {
std::cout << "Hello, World!" << std::endl; // Print "Hello, World!" to the console
return 0; // Indicate successful program execution
#include <iostream>: This line includes the iostream library, which provides functionalities for input
and output operations (like printing to the console).
int main() { ... }: This is the main function, where the program execution begins. Every C++
program must have a main function.
std::cout << "Hello, World!" << std::endl;: This line uses std::cout (from the iostream
library) to print "Hello, World!" to the console. std::endl inserts a newline character, moving the
cursor to the next line.
return 0;: This line indicates that the program executed successfully. Returning 0 is a
convention.
3. Variables and Data Types:
Variables are used to store data. C++ is a statically-typed language, meaning you must declare the
data type of a variable before using it.
int: Integer numbers (e.g., -10, 0, 15)
float: Floating-point numbers (e.g., 3.14, -2.5)
double: Double-precision floating-point numbers (for higher precision)
char: Single characters (e.g., 'a', 'Z', '$')
bool: Boolean values (true or false)
std::string: Sequences of characters (e.g., "Hello", "World") (Note: Requires #include
<string>)
C++
int age = 25;
float price = 99.99;
char initial = 'J';
std::string name = "John Doe";
bool isStudent = true;
4. Operators:
C++ provides various operators for performing operations on variables and values.
Arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulo)
Assignment operators: = (assignment), +=, -=, *=, /=, %= (compound assignment)
Comparison operators: == (equal to), != (not equal to), > (greater than), < (less than), >=
(greater than or equal to), <= (less than or equal to)
Logical operators: 1 && (AND), || (OR), ! (NOT) 2
1. radon-project.github.io
2. www.edrawmind.com
5. Control Flow:
Control flow statements determine the order in which code is executed.
if statement: Executes a block of code if a condition is true.
C++
if (age >= 18) {
std::cout << "You are an adult." << std::endl;
} else {
std::cout << "You are a minor." << std::endl;
for loop: Repeats a block of code a specific number of times.
C++
for (int i = 0; i < 10; i++) {
std::cout << i << " ";
std::cout << std::endl;
while loop: Repeats a block of code as long as a condition is true.
C++
int i = 0;
while (i < 5) {
std::cout << i << " ";
i++;
std::cout << std::endl;
6. Functions:
Functions are blocks of code that perform specific tasks. They help in organizing code and making it
reusable.
C++
int add(int a, int b) {
return a + b;
int main() {
int sum = add(5, 3);
std::cout << "The sum is: " << sum << std::endl;
return 0;
7. Input:
You can get input from the user using std::cin.
C++
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old." << std::endl;
8. Comments:
Comments are used to explain code. They are ignored by the compiler.
Single-line comments: // This is a single-line comment
Multi-line comments: /* This is a multi-line comment */
This is a basic introduction to C++. There's much more to learn, including more advanced concepts
like classes, objects, pointers, and templates. Start with these fundamentals, practice writing code,
and gradually explore more complex topics. Good luck!