0% found this document useful (0 votes)
19 views15 pages

C++ Full Course

this is a c++ full course

Uploaded by

obinnaugwuishiwu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

C++ Full Course

this is a c++ full course

Uploaded by

obinnaugwuishiwu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Table of Contents

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++

History and Features of C++

C++ is a general-purpose programming language created by Bjarne Stroustrup as an


extension of the C programming language. It supports procedural programming, object-
oriented programming, and generic programming. Key features include:

 Strongly typed language


 Support for both low-level and high-level programming
 Extensive standard library

Setting Up Development Environment

To start programming in C++, you need:

 A text editor (VS Code, Sublime Text, etc.)


 A compiler (GCC, Clang, MSVC)
 An IDE (optional but recommended, like Visual Studio or Code::Blocks)

Writing and Compiling Your First C++ Program

Here's a simple "Hello, World!" program:

cpp
Copy code
#include <iostream> // Preprocessor directive

int main() {
std::cout << "Hello, World!" << std::endl; // Output statement
return 0; // Return statement
}

To compile and run:


bash
Copy code
g++ -o hello hello.cpp
./hello

2. Basic Concepts

Syntax and Structure of a C++ Program

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;
}

Data Types and Variables

C++ supports various data types:

 Basic: int, char, float, double, bool


 Derived: arrays, pointers, references
 User-defined: struct, class, enum

cpp
Copy code
int age = 25;
char initial = 'A';
float height = 5.9;
double pi = 3.14159;
bool isAdult = true;

Constants and Literals

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);

Break and Continue Statements

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

Function Declaration and Definition


cpp
Copy code
// Declaration
int add(int a, int b);

// Definition
int add(int a, int b) {
return a + b;
}

// Calling the function


int sum = add(5, 3);

Parameter Passing

 Pass by Value: copies the value


 Pass by Reference: uses the original variable

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

Multiple functions with the same name but different parameters.

cpp
Copy code
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

Recursion

A function that calls itself.

cpp
Copy code
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}

5. Object-Oriented Programming (OOP)

Classes and Objects

A class is a blueprint for objects.

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();

Constructors and Destructors

Constructors initialize objects. Destructors clean up.

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;
}
};

Car car1("Toyota", "Corolla", 2020);

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

Shared by all objects of a class.

cpp
Copy code
class Car {
public:
static int count;
Car() { count++; }
};

int Car::count = 0;

Car car1, car2;


cout << Car::count; // Output: 2

Friend Functions

Can access private members of a class.

cpp
Copy code
class Car {
private:
string brand;
public:
friend void showBrand(Car &c);
};

void showBrand(Car &c) {


cout << c.brand;
}

This Pointer

Refers to the invoking object.

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();

6. Advanced OOP Concepts

Inheritance

Deriving a class from another class.

cpp
Copy code
class Vehicle {
public:
string brand = "Ford";
};

class Car : public Vehicle {


public:
string model = "Mustang";
};

Polymorphism

Allows methods to do different things based on the object it is acting upon.

cpp
Copy code
class Animal {
public:
virtual void sound() {
cout << "Some sound" << endl;
}
};

class Dog : public Animal {


public:
void sound() override {
cout << "Bark" << endl;
}
};

Virtual Functions
Allow derived classes to override methods.

cpp
Copy code
class Base {
public:
virtual void display() {
cout << "Base class" << endl;
}
};

class Derived : public Base {


public:
void display() override {
cout << "Derived class" << endl;
}
};

Abstract Classes

Contain at least one pure virtual function.

cpp
Copy code
class Shape {
public:
virtual void draw() = 0; // pure virtual function
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};

Operator Overloading

Overloading operators for user-defined types.

cpp
Copy code
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i) {}

Complex operator + (const Complex &obj) {


return Complex(real + obj.real, imag + obj.imag);
}
};

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

Special case for template.

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

Try, Catch, and Throw Blocks

cpp
Copy code
try {
// code that may throw exception
throw "An error occurred";
} catch (const char* msg) {
cout << msg << endl;
}

Standard Exceptions

Using 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

Using file streams.

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();

Reading from and Writing to Files

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

Modes for opening files.

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

10. Standard Template Library (STL)

Introduction to STL

STL provides ready-to-use classes and functions.

Containers

Examples of STL containers.

cpp
Copy code
#include <vector>
#include <list>
#include <map>
#include <set>

std::vector<int> vec = {1, 2, 3, 4, 5};


std::list<int> lst = {1, 2, 3, 4, 5};
std::map<int, std::string> mp = {{1, "one"}, {2, "two"}};
std::set<int> st = {1, 2, 3, 4, 5};

Iterators

Iterators for traversing containers.


cpp
Copy code
std::vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << " ";
}

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

Objects that can be called as functions.

cpp
Copy code
struct Add {
int operator()(int a, int b) {
return a + b;
}
};

Add add;
std::cout << add(3, 4);

11. Advanced Topics

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

Automate memory management.

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

Inline anonymous functions.

cpp
Copy code
auto add = [](int a, int b) {
return a + b;
};

std::cout << add(3, 4);

Move Semantics and Rvalue References

Efficient resource transfer.

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; }
};

12. Best Practices and Design Patterns

Code Style and Documentation

Follow consistent code style guidelines and document code.

Common Design Patterns in C++

 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;
}
};

Singleton* Singleton::instance = nullptr;

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!

You might also like