Difficulty: Introductory to Intermediate | Prerequisites: Basic C++ syntax (variables, functions, control flow). If you are not comfortable writing simple functions and using basic data types in C++, review those first.
Object-oriented programming is the dominant paradigm for structuring non-trivial C++ programmes, and most upper-level CS courses assume you can read and write classes fluently. This topic sits at the boundary between "learning the language" and "designing software": you already know how to write functions, and now you are learning how to bundle data and behaviour into self-contained units. The payoff is code that is easier to reason about, test, and extend, which is why OOP surfaces in nearly every software engineering, data structures, and systems course that follows. If you have not yet worked with functions and basic data types in C++, cover those first.
OOP in C++ revolves around classes (blueprints) and objects (instances). You keep data private, expose it through public functions, and use constructors and destructors to manage an object's lifecycle. Structs and enums round out the type system for simpler cases.
Object-Oriented Programming (OOP)
A programming paradigm that organises code around objects, each combining data (attributes) and the functions (methods) that operate on that data.
In simple terms: instead of writing loose functions that pass data around, you bundle everything an entity needs into one package.
Encapsulation
The principle of wrapping data and the functions that manipulate it into a single unit (a class), while restricting direct access to some of the unit's internals.
Think of it as a capsule: the contents are sealed inside, and you interact through a defined interface.
Data hiding
The practice of making data members private so they cannot be read or changed directly from outside the class. Access goes through public member functions instead.
In simple terms: the class decides what outsiders are allowed to see or change.
Abstract Data Type (ADT)
A programmer-defined type that specifies what operations can be performed and what values the data can hold, without exposing how those operations are implemented.
Think of it as a contract: "here is what you can do with this type" with no promises about how it works under the bonnet.
Class
A blueprint or template that defines the attributes and methods an object will have. Declaring a class does not create an object.
In simple terms: a class is the cookie cutter, not the cookie.
Object
A specific instance of a class, with its own set of attribute values. Created by declaring a variable of the class type.
Think of it as one cookie stamped out from the cutter.
Access specifier
A keyword (private, public, or protected) that controls which parts of a programme can see or use a class member.
Member variable (attribute / property)
A variable declared inside a class that stores part of an object's state. Usually marked private.
Member function (method)
A function declared inside a class that defines a behaviour or operation the object can perform.
Accessor (getter)
A public member function that returns the value of a private member variable without changing it.
Mutator (setter)
A public member function that changes the value of a private member variable, often with validation.
Constructor
A special member function with the same name as the class, called automatically when an object is created. Initialises the object's data.
Destructor
A special member function indicated by a tilde (~) before the class name, called automatically when an object is destroyed. Handles cleanup such as releasing memory.
Inline function
A member function defined inside the class declaration. The compiler may embed its code directly at the call site rather than performing a normal function call.
Scope resolution operator (::)
The operator used to define a member function outside its class declaration, specifying which class the function belongs to.
Struct (structure)
Similar to a class in C++, but with all members public by default. Typically used for simple data groupings (plain-old-data, or POD types).
Enum (enumerated type)
A type that defines a set of named integral constants, such as enum Season {Spring, Summer, Fall, Winter};. Values map to integers starting from zero unless you say otherwise.
OOP centres on three ideas: encapsulation (bundling data and functions), data hiding (restricting direct access), and abstraction (exposing what, not how).
Together these give you modular, reusable, maintainable code. Each object is a self-contained unit you can reason about independently.
An ADT defines the interface (what you can do) without locking in the implementation (how it works). In C++ the class is the standard vehicle for an ADT.
Because the implementation is hidden, you can change internals freely without breaking code that uses the type. This is the foundation of modularity in large codebases.
A class declares the attributes (data) and methods (functions) that every object of that type will have. It is a template, nothing more.
An object is a living instance with its own copy of those attributes. You create one by declaring a variable of the class type: Circle myCircle;
You interact with an object through the dot operator: myCircle.setRadius(5);
private: accessible only inside the class. This is where data members live by default in a class.
public: accessible from anywhere. Public member functions form the class's interface.
protected: accessible inside the class and its derived classes (relevant once you reach inheritance).
The standard pattern is to keep data private and provide public accessors (getters) and mutators (setters). This lets you validate input, log changes, or swap internal representation without touching the code that uses the object.
Member variables store state. They are usually private.
Member functions define behaviour. Two important categories:
Accessors (getters): return a value without modifying the object.
Mutators (setters): change a value, often with validation logic.
In everyday C++ vocabulary, member variables are also called attributes or properties, and member functions are called methods.
A constructor shares its name with the class, takes no return type, and runs automatically when an object is created. Its job is to put the object into a valid initial state.
You can overload constructors: a default constructor (no parameters) and one or more parameterised constructors (with arguments).
A destructor is prefixed with a tilde (~ClassName), runs when the object goes out of scope or is deleted, and handles cleanup (releasing memory, closing files). A class has exactly one destructor; it cannot be overloaded.
A member function defined inside the class body is implicitly inline: the compiler may substitute the function call with the function's code directly, avoiding call overhead.
When you define a member function outside the class, you qualify it with the class name and :: (the scope resolution operator): void Circle::setRadius(double r) { ... }
Separating declaration (in the header) from definition (in the .cpp file) is standard practice for larger classes.
In C++, struct and class are almost identical. The only language-level difference is default access: struct members are public by default, class members are private.
Convention: use a struct for simple data bundles (plain-old-data, POD) with no internal logic. Use a class when you need encapsulation, data hiding, and methods.
An enum defines a fixed set of named constants: enum Season {Spring, Summer, Fall, Winter};
Internally the names map to integers starting at 0 (Spring = 0, Summer = 1, and so on), unless you assign values explicitly.
Outputting an enum variable with cout prints the integer, not the name, unless you provide a conversion.
Use enums wherever a variable should only take one of a closed set of values. They make code self-documenting and catch invalid states at compile time.
Students often think declaring a class creates an object. It does not. A class is only a blueprint; you must declare a variable of that type to instantiate an object.
Students sometimes assume a struct cannot have member functions in C++. It can. The only difference between struct and class is the default access level (public vs private).
Students frequently confuse the constructor with a regular member function that happens to share the class name. Constructors have no return type, not even void, and are called automatically at object creation.
Students sometimes believe "encapsulation" and "data hiding" are the same thing. Encapsulation is the broader idea of bundling data and behaviour together. Data hiding is one technique used to enforce encapsulation, by making members private.
⚠️ Know the three access specifiers (private, public, protected) and when each is appropriate. Exam questions love asking what happens when you try to access a private member from outside the class.
⚠️ Be able to write a complete class from scratch: private data, public constructor(s), accessor and mutator functions, and a destructor.
⚠️ Understand the difference between defining a member function inside the class (inline) vs outside with the scope resolution operator (::). You may be asked to convert between the two forms.
⚠️ The struct vs class distinction (default access level) is a common short-answer or true/false question.
⚠️ Enum integer mapping: know that Spring in enum Season {Spring, Summer, Fall, Winter}; has value 0, and that cout << Spring; prints 0, not "Spring".
True or False: A class declaration creates an object. (False. It only defines the blueprint.)
Fill in the blank: In C++, the default access level for class members is ______. (private)
True or False: A struct in C++ cannot contain member functions. (False. It can; the only difference from a class is default access.)
Fill in the blank: A function that returns the value of a private member variable without changing it is called an ______ (or getter). (accessor)
True or False: A class can have more than one destructor. (False. A class has exactly one destructor.)
Q: What are the three core principles of OOP as covered in this material?
A: Encapsulation, data hiding, and bundling of data and functions.
Q: Explain the difference between an Abstract Data Type and a class in C++.
A: An ADT is a conceptual specification of what a type does (its operations and valid values) without describing how. A class is the C++ implementation of that concept, providing both the interface and the concrete code behind it.
Q: Given a class called Rectangle with private members width and height, write the signature for an accessor and a mutator for width.
A: Accessor: double getWidth() const; Mutator: void setWidth(double w);
Q: What is the difference between a default constructor and a parameterised constructor?
A: A default constructor takes no arguments and initialises data to default values. A parameterised constructor takes one or more arguments and uses them to set the object's initial state.
Q: Why does cout << Spring; print 0 when Spring is the first value in enum Season {Spring, Summer, Fall, Winter};?
A: Enum values map to integers starting from 0 by default. Spring is the first enumerator, so its underlying value is 0. cout prints the integer, not the name.
Q: You define void Circle::setRadius(double r) outside the class body. What is the role of :: here?
A: The scope resolution operator :: tells the compiler that setRadius belongs to the Circle class, rather than being a free function.
Q: What happens if you try to access a private member variable directly from main()?
A: The compiler produces an error. Private members are only accessible from within the class's own member functions.
This material connects directly to inheritance and polymorphism, which build on classes, access specifiers (especially protected), and constructors to let you create hierarchies of related types. If you move on to data structures (linked lists, stacks, queues, trees), you will implement each one as a class using exactly the patterns covered here. Operator overloading, templates, and the Standard Template Library (STL) all assume you are comfortable with class design, constructors, and the distinction between interface and implementation.
OOP, object-oriented programming, C++ classes, C++ objects, encapsulation, data hiding, abstract data type, ADT, access specifiers, private public protected, member variables, member functions, attributes, properties, methods, accessor, getter, mutator, setter, constructor, default constructor, parameterised constructor, destructor, tilde destructor, inline function, scope resolution operator, ::, struct vs class, POD, plain-old-data, enum, enumerated type, enumeration, class blueprint, object instance, CS101, C++ class design, OOP principles C++