C++ Full Course
C++ Full Course
C++ Full Course
1. Introduction to C++
o History and Features of C++
o Setting Up Development Environment
o Writing and Compiling Your First C++ Program
2. Basic Concepts
o Syntax and Structure of a C++ Program
o Data Types and Variables
o Constants and Literals
o Operators in C++
3. Control Structures
o Conditional Statements: if, else-if, else, switch
o Loops: for, while, do-while
o Break and Continue Statements
4. Functions
o Function Declaration and Definition
o Parameter Passing: Pass by Value, Pass by Reference
o Inline Functions
o Function Overloading
o Recursion
5. Object-Oriented Programming (OOP)
o Classes and Objects
o Constructors and Destructors
o Access Specifiers: public, private, protected
o Member Functions
o Static Members
o Friend Functions
o This Pointer
6. Advanced OOP Concepts
o Inheritance
o Polymorphism
o Virtual Functions
o Abstract Classes
o Operator Overloading
7. Templates
o Function Templates
o Class Templates
o Template Specialization
8. Exception Handling
o Try, Catch, and Throw Blocks
o Standard Exceptions
o User-Defined Exceptions
9. File I/O
o File Handling
o Reading from and Writing to Files
o File Modes
10. Standard Template Library (STL)
o Introduction to STL
o Containers: Vector, List, Map, Set, etc.
o Iterators
o Algorithms
o Function Objects
11. Advanced Topics
o Multithreading
o Smart Pointers
o Lambda Expressions
o Move Semantics and Rvalue References
12. Best Practices and Design Patterns
o Code Style and Documentation
o Common Design Patterns in C++
1. Introduction to C++
cpp
Copy code
#include <iostream> // Preprocessor directive
int main() {
std::cout << "Hello, World!" << std::endl; // Output statement
return 0; // Return statement
}
2. Basic Concepts
A C++ program consists of functions and classes. The main function is the entry point.
cpp
Copy code
#include <iostream>
int main() {
// code goes here
return 0;
}
cpp
Copy code
int age = 25;
char initial = 'A';
float height = 5.9;
double pi = 3.14159;
bool isAdult = true;
Constants are fixed values that do not change during the program execution. Use const
keyword or #define.
cpp
Copy code
const int max_age = 100;
#define PI 3.14159
Operators in C++
Operators are symbols that perform operations on variables and values. Types include:
Arithmetic: +, -, *, /, %
Relational: ==, !=, >, <, >=, <=
Logical: &&, ||, !
Bitwise: &, |, ^, ~, <<, >>
Assignment: =, +=, -=, *=, /=, %=
Miscellaneous: sizeof, ?:, ,, .
3. Control Structures
Conditional Statements
cpp
Copy code
if (condition) {
// code
} else if (condition) {
// code
} else {
// code
}
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Loops
cpp
Copy code
for (int i = 0; i < 10; i++) {
// code
}
while (condition) {
// code
}
do {
// code
} while (condition);
cpp
Copy code
for (int i = 0; i < 10; i++) {
if (i == 5) break; // exit loop
if (i % 2 == 0) continue; // skip even numbers
}
4. Functions
// Definition
int add(int a, int b) {
return a + b;
}
Parameter Passing
cpp
Copy code
void increment(int &value) {
value++;
}
int num = 5;
increment(num); // num is now 6
Inline Functions
Functions defined with inline keyword are expanded in line when invoked.
cpp
Copy code
inline int square(int x) {
return x * x;
}
Function Overloading
cpp
Copy code
int add(int a, int b) {
return a + b;
}
Recursion
cpp
Copy code
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
cpp
Copy code
class Car {
public:
string brand;
string model;
int year;
void displayInfo() {
cout << brand << " " << model << " " << year << endl;
}
};
// Creating an object
Car car1;
car1.brand = "Toyota";
car1.model = "Corolla";
car1.year = 2020;
car1.displayInfo();
cpp
Copy code
class Car {
public:
string brand;
string model;
int year;
// Constructor
Car(string b, string m, int y) : brand(b), model(m), year(y) {}
// Destructor
~Car() {
cout << "Car object destroyed" << endl;
}
};
Access Specifiers
Control access to class members.
cpp
Copy code
class Car {
private:
string brand;
public:
void setBrand(string b) {
brand = b;
}
string getBrand() {
return brand;
}
};
Static Members
cpp
Copy code
class Car {
public:
static int count;
Car() { count++; }
};
int Car::count = 0;
Friend Functions
cpp
Copy code
class Car {
private:
string brand;
public:
friend void showBrand(Car &c);
};
This Pointer
cpp
Copy code
class Car {
public:
Car* setBrand(string b) {
brand = b;
return this;
}
void display() {
cout << brand << endl;
}
private:
string brand;
};
Car car1;
car1.setBrand("Toyota")->display();
Inheritance
cpp
Copy code
class Vehicle {
public:
string brand = "Ford";
};
Polymorphism
cpp
Copy code
class Animal {
public:
virtual void sound() {
cout << "Some sound" << endl;
}
};
Virtual Functions
Allow derived classes to override methods.
cpp
Copy code
class Base {
public:
virtual void display() {
cout << "Base class" << endl;
}
};
Abstract Classes
cpp
Copy code
class Shape {
public:
virtual void draw() = 0; // pure virtual function
};
Operator Overloading
cpp
Copy code
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
7. Templates
Function Templates
Generic functions.
cpp
Copy code
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << add<int>(3, 4) << endl;
cout << add<double>(3.5, 4.5) << endl;
}
Class Templates
Generic classes.
cpp
Copy code
template <class T>
class Pair {
private:
T first, second;
public:
Pair(T a, T b) : first(a), second(b) {}
T getFirst() { return first; }
T getSecond() { return second; }
};
int main() {
Pair<int> obj(1, 2);
cout << obj.getFirst() << " " << obj.getSecond();
}
Template Specialization
cpp
Copy code
template <>
class Pair <int> {
private:
int first, second;
public:
Pair(int a, int b) : first(a), second(b) {}
int getFirst() { return first; }
int getSecond() { return second; }
};
8. Exception Handling
cpp
Copy code
try {
// code that may throw exception
throw "An error occurred";
} catch (const char* msg) {
cout << msg << endl;
}
Standard Exceptions
cpp
Copy code
#include <stdexcept>
try {
throw std::out_of_range("Out of range error");
} catch (const std::out_of_range &e) {
cout << e.what() << endl;
}
User-Defined Exceptions
Custom exceptions.
cpp
Copy code
class MyException : public std::exception {
public:
const char* what() const throw() {
return "Custom exception occurred";
}
};
try {
throw MyException();
} catch (const MyException &e) {
cout << e.what() << endl;
}
9. File I/O
File Handling
cpp
Copy code
#include <fstream>
std::ofstream outFile("example.txt");
outFile << "Writing to a file." << std::endl;
outFile.close();
std::ifstream inFile("example.txt");
std::string line;
while (std::getline(inFile, line)) {
std::cout << line << std::endl;
}
inFile.close();
cpp
Copy code
std::ofstream outFile("example.txt", std::ios::app); // append mode
outFile << "Adding more text." << std::endl;
outFile.close();
std::ifstream inFile("example.txt");
std::string line;
while (std::getline(inFile, line)) {
std::cout << line << std::endl;
}
inFile.close();
File Modes
cpp
Copy code
std::ofstream outFile("example.txt", std::ios::out | std::ios::trunc); //
write mode
std::ifstream inFile("example.txt", std::ios::in); // read mode
std::fstream file("example.txt", std::ios::in | std::ios::out); //
read/write mode
Introduction to STL
Containers
cpp
Copy code
#include <vector>
#include <list>
#include <map>
#include <set>
Iterators
Algorithms
Common algorithms.
cpp
Copy code
#include <algorithm>
std::sort(vec.begin(), vec.end());
std::reverse(vec.begin(), vec.end());
std::find(vec.begin(), vec.end(), 3);
Function Objects
cpp
Copy code
struct Add {
int operator()(int a, int b) {
return a + b;
}
};
Add add;
std::cout << add(3, 4);
Multithreading
cpp
Copy code
#include <thread>
void threadFunction() {
std::cout << "Thread is running" << std::endl;
}
std::thread t1(threadFunction);
t1.join(); // Wait for thread to finish
Smart Pointers
cpp
Copy code
#include <memory>
std::unique_ptr<int> ptr1 = std::make_unique<int>(10);
std::shared_ptr<int> ptr2 = std::make_shared<int>(20);
Lambda Expressions
cpp
Copy code
auto add = [](int a, int b) {
return a + b;
};
cpp
Copy code
class Moveable {
public:
int* data;
Moveable(int value) : data(new int(value)) {}
Moveable(Moveable&& other) noexcept : data(other.data) {
other.data = nullptr;
}
~Moveable() { delete data; }
};
Singleton
Observer
Factory
Strategy
cpp
Copy code
// Singleton Example
class Singleton {
private:
static Singleton* instance;
Singleton() {}
public:
static Singleton* getInstance() {
if (!instance) {
instance = new Singleton();
}
return instance;
}
};
This course outline and the detailed explanations provided will give you a solid foundation in
C++. If you need more specific details or further explanations on any topic, feel free to ask!