Learn with Yasir

Share Your Feedback

Programming in C++ – Complete Beginner’s Guide with Examples, Exercises & Downloadable PDFs


Learn C++ from scratch with clear explanations, practical code examples, hands-on exercises, and downloadable PDF lessons. Covers variables, data types, operators, control flow, functions, iostream, and common pitfalls—ideal for students and self-learners.

Identifier

Keywords

Data Types


C++ Program: Understanding Data Types

Question: Write a C++ program to demonstrate different data types (int, float, double, char, bool, string) and print example values for each.

#include <iostream>
using namespace std;

int main() {
    // Integer: whole numbers
    int age = 20;
    cout << "Integer example (age): " << age << endl;

    // Floating-point: numbers with decimal
    float height = 5.9;
    double weight = 70.5;
    cout << "Float example (height): " << height << endl;
    cout << "Double example (weight): " << weight << endl;

    // Character: single letter or symbol
    char grade = 'A';
    cout << "Character example (grade): " << grade << endl;

    // Boolean: true or false
    bool isStudent = true;
    cout << "Boolean example (isStudent): " << isStudent << endl;

    // String: sequence of characters
    string name = "Ali";
    cout << "String example (name): " << name << endl;

    return 0;
}

Explanation

  1. int → Stores whole numbers like 1, 10, -5.
  2. float → Stores decimal numbers, e.g., 3.14.
  3. double → Similar to float but with more precision.
  4. char → Stores a single character like 'A' or 'x'.
  5. bool → Stores true or false.
  6. string → Stores text or words like "Hello".

Sample Output

Integer example (age): 20
Float example (height): 5.9
Double example (weight): 70.5
Character example (grade): A
Boolean example (isStudent): 1
String example (name): Ali

Note: Boolean values print as 1 (true) or 0 (false) in C++.


C++ Program: Character Data Type Example

Question: Write a C++ program to demonstrate the use of the char (character) data type and display example values.

#include <iostream>
using namespace std;

int main() {
    // Character variable
    char grade = 'A';
    char symbol = '#';
    char letter = 'Z';

    cout << "Character example 1 (grade): " << grade << endl;
    cout << "Character example 2 (symbol): " << symbol << endl;
    cout << "Character example 3 (letter): " << letter << endl;

    return 0;
}

💡 Explanation

  • The char data type is used to store a single character, such as a letter, digit, or symbol.
  • Character values are always enclosed in single quotes (‘ ‘).
  • Each character has an ASCII code (a numeric value in computer memory).

🖥️ Sample Output

Character example 1 (grade): A
Character example 2 (symbol): #
Character example 3 (letter): Z

Interger Overflow and Underflow

Here’s a beginner-friendly C++ example to explain the concept of overflow and underflow in simple terms 👇


Question: Write a C++ program to demonstrate the concept of overflow and underflow using integer data type.


C++ Program: Overflow and Underflow Example

#include <iostream>
#include <limits> // for numeric_limits
using namespace std;

int main() {
    // Find the maximum and minimum values an int can hold
    int maxValue = numeric_limits<int>::max();
    int minValue = numeric_limits<int>::min();

    cout << "Maximum value of int: " << maxValue << endl;
    cout << "Minimum value of int: " << minValue << endl;

    // Overflow: adding 1 to the maximum value
    cout << "\nAfter overflow (maxValue + 1): " << (maxValue + 1) << endl;

    // Underflow: subtracting 1 from the minimum value
    cout << "After underflow (minValue - 1): " << (minValue - 1) << endl;

    return 0;
}

💡 Explanation for Beginners

  1. numeric_limits<int>::max() gives the largest value an integer can store.
  2. numeric_limits<int>::min() gives the smallest value an integer can store.
  3. Overflow happens when a value becomes larger than the maximum limit — it wraps around to the smallest value.
  4. Underflow happens when a value becomes smaller than the minimum limit — it wraps around to the largest value.

For more details about ‘numeric_limits’, See C++ numeric_limits – Get Min/Max Values for Data Types (Beginner’s Guide)


🖥️ Sample Output

Maximum value of int: 2147483647
Minimum value of int: -2147483648

After overflow (maxValue + 1): -2147483648
After underflow (minValue - 1): 2147483647

Variables

CPP example

Questions: Write a C++ program to demonstrate variable declaration, initialization

#include <iostream>
using namespace std;

int main() {
    // 1. What is a variable?
    // A variable is a name that stores a value in memory. 
    // You can change the value of a variable during program execution.

    // 2. Variable declaration and initialization
    int age = 20;          // 'int' is the type, 'age' is the variable name, 20 is the initial value
    float height = 5.9;    // float stores numbers with decimals
    char grade = 'A';      // char stores a single character
    bool isStudent = true; // bool stores true or false
    string name = "Ali";   // string stores a sequence of characters

    // 3. Printing variable values
    cout << "Age: " << age << endl;
    cout << "Height: " << height << endl;
    cout << "Grade: " << grade << endl;
    cout << "Is student? " << isStudent << endl;
    cout << "Name: " << name << endl;

    // 4. Changing variable values
    age = 21;   // Value of 'age' is updated
    name = "Ahmed"; // Value of 'name' is updated

    cout << "\nAfter updating variables:" << endl;
    cout << "Age: " << age << endl;
    cout << "Name: " << name << endl;

    /* 5. Rules for declaring variables:
        - Must start with a letter or underscore (_)
        - Can contain letters, digits, and underscores
        - Cannot start with a digit
        - Cannot use C++ keywords (like int, float, return, etc.)
        - Should have meaningful names
    */

    return 0;
}

Explanation:

  • Variables store data.
  • Declaration is telling the program the type of variable (e.g., int age;).
  • Initialization is giving it a value for the first time (e.g., int age = 20;).
  • You can update the value anytime.
  • Follow the naming rules to avoid errors.

Literals

CPP example

Question: Write a C++ program to demonstrate the concept of literals, including long literals.

#include <iostream>
using namespace std;

int main() {
    // 1. Integer literal
    int age = 25;  
    cout << "Integer literal (age): " << age << endl;

    // 2. Long integer literal
    long population = 7800000000L; // 'L' specifies a long literal
    cout << "Long literal (population): " << population << endl;

    // 3. Floating-point literal
    float height = 5.9f;   // 'f' specifies a float literal
    double weight = 70.5;  // double literal by default
    cout << "Float literal (height): " << height << endl;
    cout << "Double literal (weight): " << weight << endl;

    // 4. Character literal
    char grade = 'A';  
    cout << "Character literal (grade): " << grade << endl;

    // 5. Boolean literal
    bool isStudent = true;  
    cout << "Boolean literal (isStudent): " << isStudent << endl;

    // 6. String literal
    string name = "Ali";  
    cout << "String literal (name): " << name << endl;

    // 7. Escape sequence literal
    cout << "This is a new line\nand this is the next line using a literal." << endl;

    return 0;
}

Explanation for Beginners:

  1. Literals are fixed values written directly in code.
  2. Examples:

    • 25 → integer literal
    • 7800000000L → long integer literal
    • 5.9f → float literal
    • 70.5 → double literal
    • 'A' → character literal
    • "Ali" → string literal
    • true → boolean literal
    • \n → escape sequence literal
  3. Literals cannot be changed during program execution.
  4. Use suffixes like L for long and f for float to specify the type explicitly.

Constants

Here’s a beginner-friendly C++ example to explain the concept of const and #define in C++:


Question: Write a C++ program to demonstrate the use of const and #define.

#include <iostream>
using namespace std;

// 1. Using #define to define a constant value
#define PI 3.14159

int main() {
    // 2. Using const to declare a constant variable
    const int DAYS_IN_WEEK = 7;

    // Trying to change a const variable will cause an error
    // DAYS_IN_WEEK = 8; // ❌ Uncommenting this line will give an error

    cout << "Value of PI using #define: " << PI << endl;
    cout << "Days in a week using const: " << DAYS_IN_WEEK << endl;

    // Example usage in calculation
    float radius = 5.0;
    float area = PI * radius * radius; // Using #define constant
    cout << "Area of circle with radius " << radius << " is: " << area << endl;

    return 0;
}

CPP Example


Question: Write a C++ program to demonstrate the use of #define for constants.

#include <iostream>
using namespace std;

// Using #define to create a constant
#define PI 3.14159
#define GREETING "Hello, World!"

int main() {
    // Using the defined constant
    float radius = 5.0;
    float area = PI * radius * radius; // Using #define constant

    cout << GREETING << endl; // Using #define string literal
    cout << "Radius: " << radius << endl;
    cout << "Area of circle: " << area << endl;

    return 0;
}

Explanation

  1. #define:

    • #define creates preprocessor constants that cannot be changed during program execution.
    • The compiler replaces the name with the value before compilation.
    • Examples in code:

      • #define PI 3.14159 → used for numeric constant
      • #define GREETING "Hello, World!" → used for string constant
      • Key Tip: #define does not have a data type, unlike const.
  2. const:

    • Declares a read-only variable whose value cannot be changed during program execution.
    • Example: const int DAYS_IN_WEEK = 7;
  3. Key Difference:

    • #define is handled by the preprocessor, not the compiler.
    • const is handled by the compiler, so it has a type and can be used like a normal variable.

for more details, see Difference Between Preprocessor and Compiler in C++

Operators

C++ Example: Arithmetic Operators


Question: Write a C++ program to demonstrate the use of arithmetic operators in C++.

#include <iostream>
using namespace std;

int main() {
    int a = 15;
    int b = 4;

    cout << "Values: a = " << a << ", b = " << b << endl;

    // 1. Addition (+)
    cout << "a + b = " << a + b << endl;

    // 2. Subtraction (-)
    cout << "a - b = " << a - b << endl;

    // 3. Multiplication (*)
    cout << "a * b = " << a * b << endl;

    // 4. Division (/)
    cout << "a / b = " << a / b << " (integer division)" << endl;

    // 5. Modulus (%) - remainder of division
    cout << "a % b = " << a % b << endl;

    // 6. Increment (++) - increases value by 1
    a++;
    cout << "After increment, a = " << a << endl;

    // 7. Decrement (--) - decreases value by 1
    b--;
    cout << "After decrement, b = " << b << endl;

    return 0;
}

Explanation for Beginners:

  1. Arithmetic operators are used to perform mathematical operations on numbers.
  2. Operators used in the example:

    • + Addition
    • - Subtraction
    • * Multiplication
    • / Division
    • % Modulus (remainder)
    • ++ Increment
    • -- Decrement
  3. Integer division truncates the decimal part. Example: 15 / 4 = 3.

C++ Example: Assignment Operator and Arithmetic Assignment Operators


Question: Write a C++ program to demonstrate the use of assignment operators in C++.

#include <iostream>
using namespace std;

int main() {
    int a = 10; // Simple assignment (=)
    int b = 5;

    cout << "Initial values: a = " << a << ", b = " << b << endl;

    // 1. Addition assignment (+=)
    a += b; // equivalent to a = a + b
    cout << "After a += b, a = " << a << endl;

    // 2. Subtraction assignment (-=)
    a -= b; // equivalent to a = a - b
    cout << "After a -= b, a = " << a << endl;

    // 3. Multiplication assignment (*=)
    a *= b; // equivalent to a = a * b
    cout << "After a *= b, a = " << a << endl;

    // 4. Division assignment (/=)
    a /= b; // equivalent to a = a / b
    cout << "After a /= b, a = " << a << endl;

    // 5. Modulus assignment (%=)
    a %= b; // equivalent to a = a % b
    cout << "After a %= b, a = " << a << endl;

    return 0;
}

Explanation for Beginners:

  1. Assignment operators are used to store values in variables and optionally perform an operation at the same time.
  2. Examples:

    • = → simple assignment
    • += → add right value to left variable and assign result
    • -= → subtract right value from left variable and assign result
    • *= → multiply left variable by right value and assign result
    • /= → divide left variable by right value and assign result
    • %= → find remainder and assign result

C++ Example: Prefix and Postfix Increment Operators


Question: Write a C++ program to demonstrate the difference between prefix (++a) and postfix (a++) increment operators.

#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int b = 5;

    cout << "Initial values: a = " << a << ", b = " << b << endl;

    // 1. Prefix increment: ++a
    // The value is incremented first, then used
    int prefixResult = ++a;
    cout << "After prefix increment (++a): a = " << a << ", result = " << prefixResult << endl;

    // 2. Postfix increment: b++
    // The value is used first, then incremented
    int postfixResult = b++;
    cout << "After postfix increment (b++): b = " << b << ", result = " << postfixResult << endl;

    return 0;
}

Explanation:

  1. Prefix Increment (++a):

    • Increments the value of the variable before using it in an expression.
    • Example: ++a → first increase a by 1, then use the new value.
  2. Postfix Increment (a++):

    • Uses the current value of the variable in an expression first, then increments it.
    • Example: a++ → use the value of a, then increase it by 1.
  3. Output Understanding:

    Initial values: a = 5, b = 5
    After prefix increment (++a): a = 6, result = 6
    After postfix increment (b++): b = 6, result = 5
    

C++ Example: Operator Prcedence

Question: Write a C++ program to demonstrate the concept of operator precedence in C++.

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5, c = 2;

    // Without parentheses
    int result1 = a + b * c; 
    // Multiplication (*) has higher precedence than addition (+)
    cout << "Result without parentheses (a + b * c): " << result1 << endl;

    // With parentheses
    int result2 = (a + b) * c; 
    // Parentheses change the order of evaluation
    cout << "Result with parentheses ((a + b) * c): " << result2 << endl;

    // Combining multiple operators
    int result3 = a + b - c * 2 / 2;
    // Operator precedence: *, / first, then +, -
    cout << "Result of a + b - c * 2 / 2: " << result3 << endl;

    return 0;
}

Explanation:

  1. Operator Precedence determines the order in which operators are evaluated in an expression.
  2. Higher precedence operators are evaluated first.

    • Example: * and / have higher precedence than + and -.
  3. Parentheses () can be used to override the default precedence.
  4. Example:

    • a + b * c → multiplication happens first: 5 * 2 = 10, then a + 10 = 20
    • (a + b) * c → parentheses first: 10 + 5 = 15, then 15 * 2 = 30