← C++ Programming and OOP: From Variables to Classes and the STL

Click “Download PDF”, then choose “Save as PDF” as the printer.

VJ Classes

Quick revision notes

C++ Programming and OOP: From Variables to Classes and the STL

Getting Started with C++

Lesson 1Your First C++ Program

  • C++ is a compiled language: the compiler translates .cpp source code into machine code before the program runs.
  • Every program starts in main; statements end with ; and blocks are wrapped in { }.
  • cout with > reads input; both come from the iostream header and live in the std namespace.
  • Compile with g++ -std=c++17 file.cpp -o name, and read compiler errors from the first one down.

Lesson 2Variables, Types and Operators

  • A variable is a named place in memory with a type; declare it, give it a starting value and choose a meaningful name.
  • Use int for whole numbers, long long for big ones, double for decimals, char for one character, bool for true or false and string for text.
  • When both operands are integers, / drops the decimal part; convert one operand to double when you need the exact quotient.
  • % gives the remainder, += and ++ update a variable, and brackets make precedence clear.

Lesson 3Decisions and Loops

  • Conditions use ==, !=, , =, and combine with &&, || and !; remember that = assigns and == compares.
  • Use if, else if and else for ranges of values, and switch with break for fixed choices.
  • Use for when the number of rounds is known, while when it is not, and do ... while when the body must run at least once.
  • Tracing a loop by hand, one round at a time, is the best way to find off-by-one mistakes.

Functions and Collections of Data

Lesson 4Functions and References

  • A function has a return type, a name, parameters and a body; declare it (a prototype) before it is called, or define it above main.
  • Pass by value gives the function a copy; pass by reference (int&) lets it change the caller’s variable.
  • Use const references for big objects that you only read, to avoid copying.
  • Default arguments and overloading let one name serve several uses; recursion needs a base case.

Lesson 5Arrays, Strings and Vectors

  • An array holds a fixed number of same-type values; indexes run from 0 to size - 1, and C++ does not check them.
  • A string knows its length and offers substr, find, + and ==; use getline for whole lines and cin.ignore after cin >>.
  • A vector grows with push_back, shrinks with pop_back, and is the default choice for a list of values.
  • Use a reference variable in a range-based for loop when you want to change the elements.

Object-Oriented Programming: Classes

Lesson 6Classes and Objects

  • A class is a blueprint and an object is one thing built from it; data members hold the state and member functions hold the behaviour.
  • Keep data private and offer public functions that check the rules; this is encapsulation.
  • Define long member functions outside the class with ClassName::, and mark functions that do not change the object as const.
  • A struct is a class whose members are public by default.

Lesson 7Constructors and Destructors

  • A constructor has the class name, no return type and runs automatically; use the member initialiser list to set the members.
  • Writing any constructor removes the automatic default constructor.
  • A destructor (~ClassName) runs when the object’s life ends, in reverse order of creation, and releases resources (RAII).
  • Passing by value copies an object through the copy constructor; pass big objects by const reference instead.

Inheritance, Polymorphism and the STL

Lesson 8Inheritance

  • Inheritance (class Derived : public Base) lets a derived class reuse and extend a base class; use it for “is a” relationships.
  • Derived classes can use public and protected base members, never private ones.
  • Construction runs base first, then derived; destruction runs in the exact reverse order.
  • If the relationship is “has a”, use composition (a data member) instead of inheritance.

Lesson 9Polymorphism and Virtual Functions

  • A virtual function is chosen by the real type of the object at run time; an ordinary function is chosen by the declared type at compile time.
  • Mark overriding functions with override so the compiler catches signature mistakes.
  • = 0 makes a function pure virtual and the class abstract: it cannot be instantiated, but it defines an interface.
  • Give every polymorphic base class a virtual destructor, and use references or pointers (never copies) to avoid slicing.

Lesson 10A Tour of the STL and Where to Go Next

  • The STL gives you containers (vector, map, set), algorithms (sort, find, count) and iterators (begin() and end()) that connect them.
  • Use sort with a lambda to order your own objects, and break ties explicitly.
  • A map keeps keys sorted, and m[key] creates a missing key, which makes m[key]++ a counting tool.
  • Prefer STL tools to hand-written ones, and keep practising with small projects.

Important terms

arguments · array · assignment · base case · both · by value · C++17 · class · comments · compiler · conditional operator · conditions · const reference · constructor · data members · declaration · declare · default argument · default constructor · definition · destructor · dot operator · encapsulation · escape sequences · falls through · function · g++ · index · inheritance · initialise · know · linker · local · logical operators · loop · machine code · member functions · namespace · object · object-oriented programming · Object-oriented programming (OOP · overflows · Overloading · parameters · Polymorphism · precedence · preprocessor · prototype · range-based for loop · recursion · reference · relational operators · return · scope · scope resolution operator · short-circuit evaluation · source code · type · variable · vector

© 2026 VJ Classes · vjclasses.in