Learn C++ in simple steps. Master the fundamentals, write efficient programs, and build powerful software with one of the world's most popular programming languages.
Start learning step by step
Check your understanding
Prepare for real interviews
Test your skills
C++ is a powerful, high-performance programming language used for system programming, game development, and competitive programming.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++!";
return 0;
}
Variables store data. C++ has several data types like int, float, double, char, and string.
#include <iostream>
using namespace std;
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
string name = "Zohil";
cout << "Name: " << name << endl;
cout << "Age: " << age << ", Height: " << height << endl;
cout << "Grade: " << grade << endl;
return 0;
}
C++ has arithmetic, relational, logical, and assignment operators.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
// Arithmetic
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
// Relational
cout << "a > b? " << (a > b) << endl;
// Logical
cout << "(a > 0 && b > 0)? " << (a > 0 && b > 0) << endl;
return 0;
}
Use cin to take input and cout to display output.
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Hello " << name << ", you are " << age << " years old." << endl;
return 0;
}
Use if, else if, and else to make decisions in your code.
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter marks: ";
cin >> marks;
if(marks >= 90) {
cout << "Grade A" << endl;
} else if(marks >= 75) {
cout << "Grade B" << endl;
} else if(marks >= 50) {
cout << "Grade C" << endl;
} else {
cout << "Fail" << endl;
}
return 0;
}
Loops allow repeating code multiple times.
#include <iostream>
using namespace std;
int main() {
cout << "For loop: " << endl;
for(int i=1; i<=5; i++) {
cout << i << " ";
}
cout << endl;
cout << "While loop: " << endl;
int j = 1;
while(j <= 5) {
cout << j << " ";
j++;
}
cout << endl;
cout << "Do-while loop: " << endl;
int k = 1;
do {
cout << k << " ";
k++;
} while(k <= 5);
return 0;
}
Functions allow you to reuse code and organize programs.
#include <iostream>
using namespace std;
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
cout << "5 + 3 = " << result << endl;
return 0;
}
Arrays store multiple values of the same type in a single variable.
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
cout << "Array elements: ";
for(int i=0; i<5; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
C++ string class allows working with text easily.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Zohil";
cout << "Name: " << name << endl;
cout << "Length: " << name.length() << endl;
cout << "Uppercase first char: " << char(toupper(name[0])) << endl;
return 0;
}
The ternary operator is a shortcut for if-else.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter age: ";
cin >> age;
string result = (age >= 18) ? "Adult" : "Minor";
cout << "You are: " << result << endl;
return 0;
}
Pointers store the memory address of variables and allow direct memory manipulation.
#include <iostream>
using namespace std;
int main() {
int num = 42;
int* ptr = # // pointer to num
cout << "Value: " << num << endl;
cout << "Address: " << ptr << endl;
cout << "Value via pointer: " << *ptr << endl;
return 0;
}
References are aliases for variables, often used for function arguments to avoid copying.
#include <iostream>
using namespace std;
void addOne(int &ref) {
ref += 1; // modifies original variable
}
int main() {
int num = 10;
addOne(num);
cout << "After addOne: " << num << endl; // 11
return 0;
}
Use new and delete to allocate memory at runtime.
#include <iostream>
using namespace std;
int main() {
int* ptr = new int; // dynamically allocate integer
*ptr = 100;
cout << "Value: " << *ptr << endl;
delete ptr; // free memory
int n;
cout << "Enter array size: ";
cin >> n;
int* arr = new int[n]; // dynamic array
for(int i=0; i<n; i++) arr[i] = i+1;
for(int i=0; i<n; i++) cout << arr[i] << " ";
cout << endl;
delete[] arr; // free array memory
return 0;
}
Store data in rows and columns using 2D arrays.
#include <iostream>
using namespace std;
int main() {
int matrix[2][3] = {{1,2,3},{4,5,6}};
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
C++ supports string class and C-style character arrays.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Zohil";
char greeting[] = "Hello";
cout << "String: " << name << endl;
cout << "Char array: " << greeting << endl;
cout << "Length of string: " << name.length() << endl;
cout << "Length of char array: " << sizeof(greeting)/sizeof(greeting[0]) << endl;
return 0;
}
Classes are blueprints for objects. Objects are instances of classes.
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
};
int main() {
Student s1;
s1.name = "Zohil";
s1.age = 20;
cout << "Name: " << s1.name << ", Age: " << s1.age << endl;
return 0;
}
Functions inside a class are called member functions and can operate on class data.
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1;
s1.name = "Zohil";
s1.age = 20;
s1.display();
return 0;
}
Constructors are special functions called when an object is created to initialize data.
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
// Constructor
Student(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1("Zohil", 20);
s1.display();
return 0;
}
Destructors are special functions called when an object is destroyed to release resources.
#include <iostream>
using namespace std;
class Student {
public:
string name;
Student(string n) { name = n; cout << "Constructor called for " << name << endl; }
~Student() { cout << "Destructor called for " << name << endl; }
};
int main() {
Student s1("Zohil");
return 0;
}
Access specifiers control access to class members: public, private, and protected.
#include <iostream>
using namespace std;
class Student {
private:
string name;
public:
void setName(string n) { name = n; }
void display() { cout << "Name: " << name << endl; }
};
int main() {
Student s1;
s1.setName("Zohil");
s1.display();
return 0;
}
Inheritance allows a class to acquire properties and methods of another class.
#include <iostream>
using namespace std;
// Base class
class Person {
public:
string name;
void greet() { cout << "Hello, " << name << endl; }
};
// Derived class
class Student : public Person {
public:
int rollNo;
};
int main() {
Student s;
s.name = "Zohil";
s.rollNo = 101;
s.greet();
cout << "Roll No: " << s.rollNo << endl;
return 0;
}
Polymorphism allows methods with the same name to behave differently based on parameters.
#include <iostream>
using namespace std;
class Math {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
};
int main() {
Math m;
cout << "Int sum: " << m.add(5, 3) << endl;
cout << "Double sum: " << m.add(5.5, 3.2) << endl;
return 0;
}
Operator overloading allows customizing the behavior of operators for user-defined types.
#include <iostream>
using namespace std;
class Point {
public:
int x, y;
Point(int a, int b) { x = a; y = b; }
// Overload + operator
Point operator+ (Point p) {
return Point(x + p.x, y + p.y);
}
};
int main() {
Point p1(1,2), p2(3,4);
Point p3 = p1 + p2;
cout << "p3: (" << p3.x << "," << p3.y << ")" << endl;
return 0;
}
Encapsulation hides internal data of a class and allows controlled access using getters and setters.
#include <iostream>
using namespace std;
class Student {
private:
int age;
public:
void setAge(int a) {
if(a >= 0) age = a;
else cout << "Invalid age!" << endl;
}
int getAge() { return age; }
};
int main() {
Student s;
s.setAge(20);
cout << "Age: " << s.getAge() << endl;
return 0;
}
Static members belong to the class, not objects, and share the same value across all objects.
#include <iostream>
using namespace std;
class Student {
public:
static int count;
Student() { count++; }
};
int Student::count = 0;
int main() {
Student s1, s2;
cout << "Total students: " << Student::count << endl;
return 0;
}
C++ allows reading from and writing to files using fstream, ifstream, and ofstream.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Writing to a file
ofstream outFile("example.txt");
outFile << "Hello C++ File!" << endl;
outFile.close();
// Reading from a file
ifstream inFile("example.txt");
string line;
while(getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
return 0;
}
Templates allow writing generic functions that work with any data type.
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << "Int sum: " << add(5, 3) << endl;
cout << "Double sum: " << add(5.5, 3.2) << endl;
return 0;
}
Exceptions allow handling runtime errors gracefully using try, catch, and throw.
#include <iostream>
using namespace std;
int main() {
int a = 10, b;
cout << "Enter divisor: ";
cin >> b;
try {
if(b == 0) throw "Division by zero!";
cout << "Result: " << a/b << endl;
} catch(const char* e) {
cout << "Error: " << e << endl;
}
return 0;
}
The Standard Template Library (STL) provides ready-to-use containers like vector, list, map, etc.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {1, 2, 3, 4, 5};
cout << "Vector elements: ";
for(int n : nums) {
cout << n << " ";
}
cout << endl;
cout << "Size: " << nums.size() << endl;
return 0;
}
Vectors are dynamic arrays in C++ STL that can grow or shrink at runtime.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums;
// Add elements
nums.push_back(10);
nums.push_back(20);
nums.push_back(30);
// Access elements
cout << "First element: " << nums[0] << endl;
// Remove last element
nums.pop_back();
cout << "Vector elements: ";
for(int n : nums) cout << n << " ";
cout << endl;
return 0;
}
Maps store key-value pairs with unique keys and provide fast lookup.
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> scores;
scores["Zohil"] = 95;
scores["Ali"] = 88;
cout << "Zohil's score: " << scores["Zohil"] << endl;
for(auto &pair : scores) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
Sets store unique elements in sorted order.
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> numbers = {5, 1, 3, 3, 2};
cout << "Set elements: ";
for(int n : numbers) cout << n << " ";
cout << endl;
numbers.insert(4);
numbers.erase(1);
cout << "Updated set: ";
for(int n : numbers) cout << n << " ";
cout << endl;
return 0;
}
Queue is a First-In-First-Out (FIFO) container in STL.
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
cout << "Front: " << q.front() << ", Back: " << q.back() << endl;
q.pop(); // remove front
cout << "After pop, Front: " << q.front() << endl;
return 0;
}
Stack is a Last-In-First-Out (LIFO) container in STL.
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
cout << "Top element: " << s.top() << endl;
s.pop(); // remove top
cout << "After pop, Top: " << s.top() << endl;
return 0;
}
Combine everything you learned to build a simple project.
// Features:
// - Add, Display, Update, Delete Students
// - Store Name, Age, Roll No
// - Use Classes, Vectors, Functions, File I/O
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
int rollNo;
};
int main() {
vector<Student> students;
// Example: add a student
Student s1;
s1.name = "Zohil";
s1.age = 20;
s1.rollNo = 101;
students.push_back(s1);
// Display all students
for(auto &s : students) {
cout << s.name << ", Age: " << s.age << ", Roll No: " << s.rollNo << endl;
}
return 0;
}
1. C++ is an extension of which programming language?
2. Which of the following is used to define a class in C++?
3. Which function is the entry point of a C++ program?
4. Which operator is used for scope resolution in C++?
5. Which of the following is a correct comment in C++?
6. Which of these is the correct way to declare a pointer in C++?
7. Which access specifier makes class members accessible only within the class?
8. Which of the following is used to dynamically allocate memory in C++?
9. What is the correct way to define a constructor in C++?
10. Which operator is used for bitwise AND in C++?
11. Which of the following is a loop that executes at least once?
12. Which of the following is not a C++ access specifier?
13. Which of the following is used to include a library in C++?
14. Which keyword is used to define a constant in C++?