UTIES Jamshedpur

C++
Complete Notes

From pointers to polymorphism, from STL to smart pointers โ€” master the language of performance.

๐ŸŽฌ Learn alongside! These notes accompany the C++ playlist on @utiesjsr YouTube channel. Code along, practice, and build muscle memory.
Introduction

What is C++?

โš™๏ธ C++ - The Language of Performance

C++ (pronounced "C plus plus") is a general-purpose, compiled, object-oriented, low-level programming language created by Bjarne Stroustrup in 1985 as an extension of the C language. It gives you direct memory control, high performance, and zero-cost abstractions.

๐ŸŽ๏ธ Blazing Fast

No garbage collection, manual memory management โ†’ used in game engines, operating systems, and trading systems.

๐ŸŽฎ Industry Standard

Unreal Engine, Photoshop, Chrome, databases, embedded systems โ€” all built with C++.

โšก Compilation Process

๐Ÿ“„ .cpp (Source)
โ†’
๐Ÿ”จ Preprocessor
โ†’
โš™๏ธ Compiler
โ†’
๐Ÿ”— Linker
โ†’
๐Ÿ–ฅ๏ธ Executable (.exe/out)

Preprocessor handles #include, #define. Compiler generates object files. Linker combines them into one binary.

Setup

Setup & Compilation

๐Ÿ–ฅ๏ธ Compilers & IDEs

  • GCC/G++ (Linux/Mac/WSL) โ€” g++ main.cpp -o output
  • MSVC (Visual Studio) โ€” Windows native
  • Clang โ€” Fast, modern
  • IDE Options: VS Code (with C++ extension), CLion, Code::Blocks, Dev-C++
// Compile and run (terminal) g++ hello.cpp -o hello ./hello // With warnings and standard g++ -Wall -std=c++17 main.cpp -o main
Chapter 02

Syntax & Variables

Hello World + Basics

#include <iostream> using namespace std; int main() { cout << "Hello, C++!" << endl; // Variables int age = 20; double pi = 3.14159; char grade = 'A'; bool isFun = true; string name = "Akarsh"; // std::string return 0; }
๐Ÿ“–
Naming: camelCase for variables, PascalCase for classes, UPPER_CASE for constants. C++ is case-sensitive.
Chapter 03

Data Types

Primitive & Derived

TypeSizeRange
int4 bytes-2e9 to 2e9
float4 bytes7 decimal digits
double8 bytes15 decimal digits
char1 byte-128 to 127
bool1 bytetrue/false
voidNo value
int a = 42; unsigned int u = 100; // only positive long long big = 1234567890123LL; auto x = 5; // type deduced as int
Chapter 04

Operators

// Arithmetic int sum = 10 + 3, product = 10 * 3; int remainder = 10 % 3; // 1 // Increment/Decrement int i = 5; int a = ++i; // pre: i=6, a=6 int b = i++; // post: b=6, i=7 // Relational & Logical bool isGreater = (a > b); bool result = (a > 0) && (b < 10); // Ternary int max = (x > y) ? x : y;
Chapter 05

Control Flow

int score = 85; if (score >= 90) { cout << "A"; } else if (score >= 75) { cout << "B"; } else { cout << "C"; } // Switch switch (day) { case 1: cout << "Mon"; break; case 2: cout << "Tue"; break; default: cout << "Invalid"; }
Chapter 06

Loops

// for loop for (int i = 0; i < 5; i++) { cout << i << " "; } // while int count = 0; while (count < 3) { count++; } // range-based for (C++11) int arr[] = {1, 2, 3}; for (int x : arr) { cout << x; }

๐Ÿ“ Loop Practice

Q1
Print all prime numbers between 1 and 100.
Q2
Reverse a number entered by user.
1234554321
Q3
Print Floyd's Triangle for N rows.
Chapter 07

Arrays & Strings

// C-style array int nums[5] = {1,2,3,4,5}; int matrix[2][3] = {{1,2,3},{4,5,6}}; // std::string (C++ style) #include <string> string str = "Hello"; str += " World"; int len = str.length(); char ch = str[0]; // 'H'
Chapter 08

Functions

// Function declaration & definition int add(int a, int b) { return a + b; } // Overloading double add(double a, double b) { return a + b; } // Default arguments void greet(string name = "Guest") { cout << "Hello " << name; } // Inline function (suggestion) inline int square(int x) { return x * x; }
Chapter 09

Pointers & References

int x = 10; int* ptr = &x; // pointer stores address *ptr = 20; // dereference โ†’ x becomes 20 // Null pointer int* p = nullptr; // C++11 // Reference (alias) int& ref = x; // ref is an alias for x ref = 30; // x becomes 30 // Dynamic memory int* arr = new int[10]; delete[] arr; // manual cleanup
โš ๏ธ
Always delete dynamically allocated memory to avoid memory leaks!
Chapter 10

OOP: Classes

class Student { private: string name; int age; public: // Constructor Student(string n, int a) : name(n), age(a) {} // Getter string getName() { return name; } // Destructor ~Student() { cout << "Destroyed" << endl; } }; Student s("Akarsh", 20);
Chapter 11

Inheritance

class Animal { public: void eat() { cout << "Eating..." << endl; } }; class Dog : public Animal { public: void bark() { cout << "Barking!" << endl; } }; Dog d; d.eat(); d.bark();
๐Ÿ”ท
Access specifiers: public (everyone), protected (derived classes), private (only class itself).
Chapter 12

Polymorphism

class Shape { public: virtual void draw() { cout << "Shape" << endl; } // virtual destructor important! virtual ~Shape() {} }; class Circle : public Shape { public: void draw() override { cout << "Circle" << endl; } }; Shape* s = new Circle(); s->draw(); // Circle (runtime polymorphism) delete s;
Chapter 13

Templates

// Function template template <typename T> T max(T a, T b) { return (a > b) ? a : b; } // Class template template <class T> class Box { T data; public: void set(T val) { data = val; } T get() { return data; } }; Box<int> intBox;
Chapter 14

STL (Standard Template Library)

Containers & Algorithms

#include <vector> #include <algorithm> #include <map> #include <set> vector<int> v = {3,1,4,1,5}; sort(v.begin(), v.end()); // {1,1,3,4,5} v.push_back(9); map<string, int> mp; mp["age"] = 20; set<int> s = {1,2,2,3}; // {1,2,3}
ContainerDescription
vectorDynamic array, fast random access
listDoubly linked list
mapKey-value pairs (BST)
unordered_mapHash table
stack/queueLIFO/FIFO adaptors
Chapter 15

Exception Handling

try { int divisor = 0; if (divisor == 0) { throw "Division by zero!"; } } catch (const char* msg) { cerr << "Error: " << msg << endl; } catch (...) { cerr << "Unknown exception" << endl; }
Chapter 16

Advanced Topics

Smart Pointers (C++11)

#include <memory> unique_ptr<int> uptr = make_unique<int>(42); shared_ptr<int> sptr = make_shared<int>(100); weak_ptr<int> wptr = sptr;

Lambda Expressions

auto add = [](int a, int b) { return a + b; }; cout << add(3, 5); // 8 vector<int> nums = {1,2,3}; for_each(nums.begin(), nums.end(), [](int n) { cout << n*2 << " "; });

Move Semantics & Rvalue References

class MyVec { int* data; public: MyVec(MyVec&& other) noexcept : data(other.data) { other.data = nullptr; // steal resource } };

๐ŸŽฏ Advanced Challenges

Challenge 1
Implement a thread-safe singleton using std::call_once.
Challenge 2
Write a custom allocator for std::vector.
Challenge 3
Implement a compile-time Fibonacci using templates (template metaprogramming).

Master the fundamentals

"C++ gives you the power to build anything โ€” from blazing fast games to operating systems. We'll explore all these concepts in the UTIES C++ series. Stay curious, keep coding. Much love! ๐Ÿ’™"

UTIES Coding School