Oop

By the end of this lesson you'll be able to design your own C++ classes — bundling data with the functions that act on it, protecting that data behind a clean interface, and bringing objects to life with constructors. This is the paradigm that organises every large C++ program you'll ever read.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free C++ course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A class is a cookie cutter ; an object is a cookie you stamp out with it. The cutter defines the shape (what data and behaviour every cookie has), but each cookie is its own thing with its own decorations (its own data). You can make a hundred cookies from one cutter, and changing the icing on one doesn't touch the others. The private part of a class is like the secret recipe sealed inside — people can eat the cookie (use its methods) without ever seeing, or messing up, the recipe.

1. Classes, Objects & Members

A class bundles together data members (the variables each object stores) and member functions , also called methods (the functions that act on that data). An object is one concrete instance of the class. You reach into an object with the . (dot) operator: myDog.bark() . Read this worked example, run it, and notice that each object keeps its own copy of the data.

Your turn. The Book class below is almost complete — fill in the two blanks marked ___ using the hints, then run it.

2. Access Specifiers & Encapsulation

Encapsulation means hiding an object's data and only letting the outside world touch it through safe, controlled methods. You enforce it with access specifiers : private: (only the class itself can see it), public: (anyone with an object can use it), and protected: (the class and any classes that inherit from it — you'll meet inheritance next lesson). The pattern is: keep data private , expose public methods . A method that only reads data is a getter ; one that changes it (with validation) is a setter .

Now you try. Complete the constructor's initializer list and add the missing const to the getter:

3. Constructors, this & Destructors

A constructor is a special method that runs automatically when an object is created — its job is to put the object in a valid starting state. A class can have several: a default constructor (no arguments) and one or more parameterized constructors . The cleanest way to set members is the initializer list — the : member(value) part written before the {' body. Inside a method, this is a pointer to the current object, handy when a parameter shares a member's name ( this->name = name; ). The destructor (named ~ClassName ) runs automatically when the object is destroyed — the place to release any resources it held.

In C++ a struct and a class are almost identical — both can have data, methods, and constructors. The only real difference is the default access level:

By convention, reach for struct when you just need a plain bundle of data with no hidden rules (like a 2D Point ), and class when you want encapsulation. Picking the right one signals your intent to the next reader.

This program uses everything from the lesson at once — a plain struct Point , an encapsulated Inventory class with a constructor, a private vector , and public methods that control how it changes. Read it line by line; you understand every part now.

No blanks this time — just a brief and an outline to keep you on track. Build the class yourself, run it, and check your output against the example in the comments. This is exactly the kind of small, self-contained class real programs are built from.

Practice quiz

What is the difference between a class and an object?

  • They are the same thing
  • An object is a blueprint; a class is an instance
  • A class is a blueprint; an object is one instance built from it
  • A class can only make one object

Answer: A class is a blueprint; an object is one instance built from it. A class is the blueprint; an object is one concrete instance of it.

What is the ONLY real difference between struct and class in C++?

  • Default access: struct is public, class is private
  • struct cannot have methods
  • class cannot have constructors
  • struct is faster

Answer: Default access: struct is public, class is private. struct members are public by default; class members are private by default.

Which operator reaches into an object to use a member or method?

  • -> arrow
  • :: scope
  • @
  • . (dot)

Answer: . (dot). You access an object's members with the . (dot) operator, e.g. myDog.bark().

When does a constructor run?

  • When the program ends
  • Automatically when an object is created
  • Only when you call it by name
  • Never automatically

Answer: Automatically when an object is created. A constructor runs automatically when an object is created, putting it in a valid starting state.

What does the ': owner(name), balance(opening)' part of a constructor represent?

  • A member initializer list
  • A function call
  • A comment
  • A return statement

Answer: A member initializer list. That is the member initializer list, which initializes members directly as the object is built.

Why make data members private?

  • It makes them faster
  • It is required for all members
  • Encapsulation — only controlled methods can change them, keeping state valid
  • So they print automatically

Answer: Encapsulation — only controlled methods can change them, keeping state valid. Private data is the core of encapsulation: the object can never reach an invalid state from outside.

What is 'this' inside a member function?

  • A copy of the object
  • A pointer to the current object
  • The class name
  • A reserved error value

Answer: A pointer to the current object. this is a pointer to the current object; this->name disambiguates a member from a same-named parameter.

When does a destructor (~ClassName) run for a local object?

  • When the program starts
  • Only if you call it manually
  • Halfway through main
  • When the object goes out of scope

Answer: When the object goes out of scope. A local object's destructor runs automatically when it goes out of scope, in reverse order of creation.

What does marking a getter 'const', e.g. 'double getBalance() const', promise?

  • It runs faster
  • The method won't modify the object
  • It returns a constant
  • It can only be called once

Answer: The method won't modify the object. A const member function promises not to change the object, so it can be called on const objects too.

What does adding 'public:' before members mean?

  • Only the class can use them
  • They become constants
  • Anyone with an object can use them
  • They are hidden

Answer: Anyone with an object can use them. public members can be accessed by anyone holding an object; private members cannot.