All C++ NOTES

 

Comprehensive C++ Notes


1. Introduction to C++

  • C++ is a general-purpose programming language created by Bjarne Stroustrup in 1979 as an extension of C.

  • It supports procedural, object-oriented, and generic programming paradigms.

  • Commonly used for system/software development, game programming, embedded systems, and more.


2. Basic Syntax

cpp
#include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }
  • #include <iostream>: Includes input/output stream library.

  • using namespace std;: Avoids prefixing std:: before cout, cin, etc.

  • int main(): Entry point of C++ program.

  • cout: Standard output stream.

  • endl: End line (newline) character.


3. Data Types

TypeDescriptionSize (typical)
intInteger4 bytes
floatFloating point number4 bytes
doubleDouble precision float8 bytes
charSingle character1 byte
boolBoolean (true or false)1 byte
voidRepresents no valueN/A

4. Variables and Constants

cpp
int age = 25; // variable const float PI = 3.14159; // constant (cannot be changed)
  • Variables hold data, constants are fixed values.

  • Use const keyword for constants.


5. Operators

  • Arithmetic: +, -, *, /, %

  • Assignment: =, +=, -=, *=, /=

  • Comparison: ==, !=, >, <, >=, <=

  • Logical: && (AND), || (OR), ! (NOT)

  • Increment/Decrement: ++, --


6. Control Structures

6.1 Conditional Statements

cpp
if (condition) { // code if true } else if (another_condition) { // code if another_condition true } else { // code if all false }

6.2 Switch Statement

cpp
switch (variable) { case 1: // code break; case 2: // code break; default: // code }

6.3 Loops

  • For loop:

cpp
for (int i = 0; i < 10; i++) { cout << i << endl; }
  • While loop:

cpp
int i = 0; while (i < 10) { cout << i << endl; i++; }
  • Do-while loop:

cpp
int i = 0; do { cout << i << endl; i++; } while (i < 10);

7. Functions

cpp
int add(int a, int b) { return a + b; } int main() { int sum = add(5, 3); cout << sum; }
  • Functions encapsulate reusable code.

  • Can have return types, parameters, and default values.


8. Arrays and Strings

8.1 Arrays

cpp
int numbers[5] = {1, 2, 3, 4, 5}; cout << numbers[0]; // Output: 1
  • Fixed size, stores multiple elements of same type.

8.2 Strings

  • Using C-style:

cpp
char name[] = "Alice";
  • Using C++ string class:

cpp
#include <string> string name = "Alice"; cout << name.length();

9. Pointers

cpp
int var = 10; int *ptr = &var; cout << *ptr; // Output: 10 (value pointed to)
  • Pointer stores memory address.

  • Use & to get address, * to dereference.


10. References

cpp
int a = 5; int &ref = a; ref = 10; // a is now 10
  • Reference is an alias for a variable.

  • Must be initialized when declared.


11. Structures (struct)

cpp
struct Person { string name; int age; }; Person p1; p1.name = "John"; p1.age = 30;
  • Group related variables under one name.


12. Object-Oriented Programming (OOP)

12.1 Classes and Objects

cpp
class Car { public: string brand; int year; void honk() { cout << "Beep beep!" << endl; } }; int main() { Car myCar; myCar.brand = "Toyota"; myCar.year = 2020; myCar.honk(); }
  • Class: blueprint for objects.

  • Object: instance of a class.

12.2 Access Modifiers

ModifierAccess Level
publicAccessible anywhere
privateAccessible only inside the class
protectedAccessible in class and subclasses

12.3 Constructor and Destructor

cpp
class Car { public: Car() { cout << "Car created\n"; } ~Car() { cout << "Car destroyed\n"; } };

12.4 Inheritance

cpp
class Vehicle { public: void move() { cout << "Moving\n"; } }; class Car : public Vehicle { public: void honk() { cout << "Honk honk\n"; } };

13. Templates (Generic Programming)

cpp
template <typename T> T add(T a, T b) { return a + b; }
  • Allows functions/classes to work with any data type.


14. Exception Handling

cpp
try { int x = 5 / 0; } catch (exception &e) { cout << "Error: " << e.what(); }
  • Use try, catch, and throw for error handling.


15. Standard Template Library (STL)

  • Containers: vector, list, map, set, etc.

  • Algorithms: sort, search, etc.

  • Iterators: used to traverse containers.

Example with vector:

cpp
#include <vector> #include <algorithm> vector<int> nums = {5, 3, 9, 1}; sort(nums.begin(), nums.end());

16. Input and Output

  • Input: cin

  • Output: cout

Example:

cpp
int age; cout << "Enter your age: "; cin >> age; cout << "Your age is " << age << endl;

17. File I/O

cpp
#include <fstream> ofstream outfile("file.txt"); outfile << "Hello File"; outfile.close(); ifstream infile("file.txt"); string content; getline(infile, content); infile.close(); cout << content;

18. Useful Keywords

KeywordPurpose
staticShared variable/function, persists
constConstant value
volatileVariable can be changed unexpectedly
mutableAllows modification in const object
friendAccess private members
namespaceOrganizes code into logical groups

19. Common Best Practices

  • Use meaningful variable names

  • Keep functions small and focused

  • Comment your code for clarity

  • Avoid using using namespace std; in headers

  • Use nullptr instead of NULL

  • Prefer std::vector over raw arrays for safety

Comments