Skip to main content
Module 4 · Lesson 1 of 29/26 in the course~14 min
Module lessons (1/2)

Classes and Objects

Lesson preview5 hands-on exercises readyCourse C++ · Module 4 — Object Oriented Programming

The syntax class, data members and member functions. Access specifiers public and private for encapsulation.

What you'll learn in this lesson

  • Design user-defined types with class and struct (always ending with ;).
  • Protect internal invariants with private: by exposing checked methods public:.
  • Declare read-only methods as const to also work with const T& parameters.

Overview

Object Oriented Programming in C++ adds no hidden costs: a class without virtual functions occupies exactly the same space in bytes as a struct in C, but packs data (membri) and behavior (metodi) in the same place.

Inside a class, all members are private by default (as opposed to struct, where the default is public):

Sections covered in this lesson

  1. 11. Syntax of class, private: and public:
  2. 22. class vs struct: What Really Changes?
  3. 33. Methods const (Const-Correctness)
  4. 44. The Pointer this
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 4 — Object Oriented Programming

Unlock "Classes and Objects" and the full Course C++ curriculum

The syntax class, data members and member functions. Access specifiers public and private for encapsulation.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course C++ plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.