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.
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;
}
'A' or 'x'.true or false."Hello".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) or0(false) in C++.
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;
}
char data type is used to store a single character, such as a letter, digit, or symbol.Character example 1 (grade): A
Character example 2 (symbol): #
Character example 3 (letter): Z
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.
#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;
}
numeric_limits<int>::max() gives the largest value an integer can store.numeric_limits<int>::min() gives the smallest value an integer can store.For more details about ‘numeric_limits’, See C++ numeric_limits – Get Min/Max Values for Data Types (Beginner’s Guide)
Maximum value of int: 2147483647
Minimum value of int: -2147483648
After overflow (maxValue + 1): -2147483648
After underflow (minValue - 1): 2147483647
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:
int age;).int age = 20;).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;
}
Examples:
25 → integer literal7800000000L → long integer literal5.9f → float literal70.5 → double literal'A' → character literal"Ali" → string literaltrue → boolean literal\n → escape sequence literalL for long and f for float to specify the type explicitly.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;
}
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;
}
#define:
#define creates preprocessor constants that cannot be changed during program execution.Examples in code:
#define PI 3.14159 → used for numeric constant#define GREETING "Hello, World!" → used for string constant#define does not have a data type, unlike const.const:
const int DAYS_IN_WEEK = 7;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++
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;
}
Operators used in the example:
+ Addition- Subtraction* Multiplication/ Division% Modulus (remainder)++ Increment-- Decrement15 / 4 = 3.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;
}
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 resultQuestion: 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;
}
Prefix Increment (++a):
++a → first increase a by 1, then use the new value.Postfix Increment (a++):
a++ → use the value of a, then increase it by 1.Output Understanding:
Initial values: a = 5, b = 5
After prefix increment (++a): a = 6, result = 6
After postfix increment (b++): b = 6, result = 5
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;
}
Higher precedence operators are evaluated first.
* and / have higher precedence than + and -.() can be used to override the default precedence.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