Cplusplus

C++ is a powerful and widely-used programming language known for its efficiency and low-level control. It offers a balance between high-level abstractions and low-level memory manipulation, making it suitable for system programming, game development, and performance-critical applications. C++ supports both procedural and object-oriented programming paradigms, giving developers flexibility in code organization and design. It provides direct memory access, allowing fine-grained control over system resources.

Module 1: Introduction to C++

Introduction to C++

C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language. It has imperative, object-oriented and generic programming features.

#include <iostream>

int main() {
	std::cout << 'Hello, World!';
	return 0;
}

Data Types and Variables

C++ has several types of variables, which can be a built-in type or a user-defined type. Each variable holds a value of a specific type.

#include <iostream>

int main() {
	int myNum = 5;
	double myFloatNum = 5.99;
	char myLetter = 'D';
	std::string myText = 'Hello';
	bool myBoolean = true;
	std::cout << myNum;
	return 0;
}

Operators

Operators are used to perform operations on variables and values.

#include <iostream>

int main() {
	int x = 10;
	int y = 4;
	std::cout << x + y;
	return 0;
}

Module 2: Control Flow and Functions in C++

If..else Statements

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

#include <iostream>

int main() {
	int time = 22;
	if (time < 10) {
		std::cout << 'Good morning.';
	} else if (time < 20) {
		std::cout << 'Good day.';
	} else {
		std::cout << 'Good evening.';
	}
	return 0;
}

Loops

C++ has several structures to control the flow of your program, including while loops, do...while loops, and for loops.

#include <iostream>

int main() {
	for (int i = 0; i < 5; i++) {
		std::cout << i;
	}
	return 0;
}

Functions

A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.

#include <iostream>

void myFunction() {
	std::cout << 'I just got executed!';
}

int main() {
	myFunction();
	return 0;
}

Module 3: Object-Oriented Programming in C++

Classes and Objects

A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a 'blueprint' for creating objects.

#include <iostream>

class MyClass {
	public: 
	int myNum;
	std::string myString;
};

int main() {
	MyClass myObj;
	myObj.myNum = 15;
	myObj.myString = 'Hello';
	std::cout << myObj.myNum;
	std::cout << myObj.myString;
	return 0;
}

Inheritance

Inheritance is one of the key features of Object Oriented Programming. Child class will inherit all the public and protected properties and methods from parent class.

#include <iostream>

class Vehicle {
	public: 
	std::string brand = 'Ford';
	void honk() {
		std::cout << 'Tuut, tuut! \n';
	}
};

class Car: public Vehicle {
	public: 
	std::string model = 'Mustang';
};

int main() {
	Car myCar;
	myCar.honk();
	std::cout << myCar.brand + ' ' + myCar.model;
	return 0;
}

Polymorphism

Polymorphism allows us to perform a single action in different ways.

#include <iostream>

class Animal {
	public: 
	virtual void animalSound() {
		std::cout << 'The animal makes a sound \n';
	}
};

class Pig : public Animal {
	public: 
	void animalSound() {
		std::cout << 'The pig says: wee wee \n';
	}
};

class Dog : public Animal {
	public: 
	void animalSound() {
		std::cout << 'The dog says: bow wow \n';
	}
};

int main() {
	Animal myAnimal;
	Pig myPig;
	Dog myDog;
	myAnimal.animalSound();
	myPig.animalSound();
	myDog.animalSound();
	return 0;
}

Module 4: Advanced Concepts in C++

Pointers

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.

#include <iostream>

int main() {
	int var = 20;
	int *p;
	p = &var;
	std::cout << 'Value of var variable: ' << var << '\n';
	std::cout << 'Address stored in p variable: ' << p << '\n';
	std::cout << 'Value of *p variable: ' << *p << '\n';
	return 0;
}

References

A reference variable is an alias, that is, another name for an already existing variable.

#include <iostream>

void swap(int& x, int& y) {
	int temp;
	temp = x;
	x = y;
	y = temp;
}

int main() {
	int a = 10;
	int b = 20;
	swap(a, b);
	std::cout << 'a = ' << a << '\n';
	std::cout << 'b = ' << b << '\n';
	return 0;
}

Templates

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

#include <iostream>
using namespace std;

template <typename T>
T max(T x, T y)
{
	return (x > y)? x : y;
}

int main() {
	cout << max(3, 7) << '\n';
	cout << max(3.0, 7.0) << '\n';
	cout << max('a', 's') << '\n';
	return 0;
}

Exception Handling

Exception handling in C++ involve three keywords: try, catch, throw.

#include <iostream>
using namespace std;

int main() {
	try {
		int age = 15;
		if (age >= 18) {
			cout << 'Access granted - you are old enough.';
		} else {
			throw (age);
		}
	} catch (int myNum) {
		cout << 'Access denied - You must be at least 18 years old.\n';
		cout << 'Age is: ' << myNum;
	}
	return 0;
}