C++
C++
Beginner Level
Introduction to C++
Getting Started
Download and Install a Compiler: Examples include GCC (MinGW for Windows) or Microsoft
Visual Studio.
Use an IDE: Popular IDEs for C++ are Code::Blocks, CLion, Visual Studio, or VS Code.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Explanation:
Basic Syntax
Variables
Data Types
Common types include int, float, double, char, bool, string (from <string>
library).
Input/Output
int number;
cout << "Enter a number: ";
cin >> number;
cout << "You entered: " << number << endl;
Control Structures
If-Else Statements:
Loops:
o For Loop:
o
o While Loop:
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
}
Intermediate Level
Functions
Syntax
int main() {
int result = add(5, 3);
cout << "The sum is: " << result << endl;
return 0;
}
Function Overloading
Allows multiple functions with the same name but different parameters.
class Car {
public:
string brand;
int year;
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car myCar;
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.displayInfo();
return 0;
}
Inheritance
class Vehicle {
public:
string brand;
void honk() {
cout << "Beep beep!" << endl;
}
};
Advanced Level
Pointers
int num = 10;
int *ptr = #
cout << "Value: " << *ptr << endl;
Templates
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << add<int>(3, 5) << endl;
cout << add<float>(3.2, 5.1) << endl;
return 0;
}
File Handling
#include <fstream>
int main() {
ofstream file("example.txt");
file << "Hello, File!";
file.close();
ifstream readFile("example.txt");
string content;
while (getline(readFile, content)) {
cout << content << endl;
}
readFile.close();
return 0;
}
Expert Level
Advanced OOP Concepts
Polymorphism
Virtual Functions:
class Base {
public:
virtual void show() {
cout << "Base class" << endl;
}
};
int main() {
Base *ptr;
Derived d;
ptr = &d;
ptr->show();
return 0;
}
class Abstract {
public:
virtual void display() = 0; // Pure virtual function
};
int main() {
Concrete obj;
obj.display();
return 0;
}
Multithreading
#include <iostream>
#include <thread>
void printHello() {
cout << "Hello from thread" << endl;
}
int main() {
thread t1(printHello);
t1.join();
return 0;
}
Vectors:
#include <vector>
int main() {
vector<int> v = {1, 2, 3};
v.push_back(4);
for (int i : v) {
cout << i << " ";
}
return 0;
}
Maps:
#include <map>
int main() {
map<string, int> m;
m["Alice"] = 30;
m["Bob"] = 25;
for (auto &pair : m) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}