Skip to main content

C++ Basics

C++ is a powerful, general-purpose programming language created as an extension of the C language. It adds object-oriented features to C, allowing for both procedural and object-oriented programming approaches.

Key Concepts and Definitions​

1. Your First C++ Program​

Program: A set of instructions that tells the computer what to do.

#include: A preprocessor directive that tells the compiler to include a specific file

iostream: A header file that contains functions for input/output operations.

main(): The entry point of a C++ program. Execution always begins here.

std::cout: The standard output stream used to display output.

std::endl: Inserts a newline character and flushes the output buffer.

#include <iostream>

int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}

Program Structure Diagram:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ #include <iostream> β”‚ ← Preprocessor directive
β”‚ β”‚
β”‚ int main() { β”‚ ← Main function (entry point)
β”‚ std::cout <<"Hello, World!"<<std::endl; β”‚ ← Output statement
β”‚ return 0; β”‚ ← Return statement
β”‚ } β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Output:

Hello, World!

2. Variables and Data Types​

Variable: A named storage location in memory that holds a value of a specific type.

Data Type: Defines the kind of data a variable can store and how much memory it occupies.

Data Types Table:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ C++ Basic Data Types β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Type β”‚ Example β”‚ Memory (typical) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ int β”‚ 25, -10, 0 β”‚ 4 bytes β”‚
β”‚ float β”‚ 19.99f, -3.5f β”‚ 4 bytes β”‚
β”‚ double β”‚ 3.14159265359 β”‚ 8 bytes β”‚
β”‚ char β”‚ 'A', '7', '$' β”‚ 1 byte β”‚
β”‚ bool β”‚ true, false β”‚ 1 byte β”‚
β”‚ std::string β”‚ "John", "Hello" β”‚ Variable β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
#include <iostream>

int main() {
// Integer types
int age = 25; // Whole number

// Floating-point types
float price = 19.99f; // Decimal number (single precision)
double precise_value = 3.14159265359; // Decimal number (double precision)

// Character type
char grade = 'A'; // Single character

// Boolean type
bool is_active = true; // true or false

// String (from standard library)
std::string name = "John"; // Text

// Output
std::cout << "Age: " << age << std::endl;
std::cout << "Price: $" << price << std::endl;
std::cout << "Grade: " << grade << std::endl;
std::cout << "Active: " << is_active << std::endl;
std::cout << "Name: " << name << std::endl;

return 0;
}

Output:

Age: 25
Price: $19.99
Grade: A
Active: 1
Name: John

3. Input and Output​

Input: Data provided to the program, typically from the user.

Output: Data produced by the program, typically displayed to the user.

#include <iostream>
#include <string>

int main() {
std::string name;
int age;

// Output
std::cout << "Enter your name: ";

// Input
std::getline(std::cin, name);

std::cout << "Enter your age: ";
std::cin >> age;

// Output with variables
std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;

return 0;
}

Input and Output Diagram:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ User │────▢│ C++ Program │────▢│ Console β”‚
β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β–² β”‚
β”‚ β”‚ β”‚
β”‚ β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Data flow (input/output)

Example Interaction:

Enter your name: Alice
Enter your age: 30
Hello, Alice! You are 30 years old.

4. Operators​

Operator: A symbol that tells the compiler to perform specific mathematical or logical operations.

Operators Table:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ C++ Operators β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Category β”‚ Operators β”‚ Example β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Arithmetic β”‚ +, -, *, /, % β”‚ a + b = 13 β”‚
β”‚ Comparison β”‚ ==, !=, >,<, >=, <=β”‚ a > b = true β”‚
β”‚ Logical β”‚ &&, ||, ! β”‚ x && y = false β”‚
β”‚ Assignment β”‚ =, +=, -=, *=, /= β”‚ a += 2 (a = a + 2)β”‚
β”‚ Increment β”‚ ++, -- β”‚ a++ (a = a + 1) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
#include <iostream>

int main() {
// Arithmetic operators
int a = 10, b = 3;
std::cout << "a + b = " << (a + b) << std::endl; // Addition
std::cout << "a - b = " << (a - b) << std::endl; // Subtraction
std::cout << "a * b = " << (a * b) << std::endl; // Multiplication
std::cout << "a / b = " << (a / b) << std::endl; // Division (integer)
std::cout << "a % b = " << (a % b) << std::endl; // Modulus (remainder)

// Comparison operators
std::cout << "a == b: " << (a == b) << std::endl; // Equal to
std::cout << "a != b: " << (a != b) << std::endl; // Not equal to
std::cout << "a > b: " << (a > b) << std::endl; // Greater than
std::cout << "a < b: " << (a < b) << std::endl; // Less than

// Logical operators
bool x = true, y = false;
std::cout << "x && y: " << (x && y) << std::endl; // Logical AND
std::cout << "x || y: " << (x || y) << std::endl; // Logical OR
std::cout << "!x: " << (!x) << std::endl; // Logical NOT

return 0;
}

*Output:

a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
a == b: 0
a != b: 1
a > b: 1
a < b: 0
x && y: 0
x || y: 1
!x: 0

Arrays​

Array: A collection of elements of the same type stored in contiguous memory locations.


#include <iostream>

int main() {
// Declare and initialize an array
int numbers[5] = {10, 20, 30, 40, 50};

// Access array elements
std::cout << "First element: " << numbers[0] << std::endl;
std::cout << "Third element: " << numbers[2] << std::endl;

// Modify an element
numbers[1] = 25;
std::cout << "Modified second element: " << numbers[1] << std::endl;

// Loop through array
std::cout << "All elements: ";
for (int i = 0; i < 5; i++) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;

return 0;
}