C++ Object-Oriented Programming โ€” Classes, Objects & Encapsulation Masterclass

โšก Modern C++ (C++17 / C++20 / C++23) ๐ŸŸข Lesson 9 ๐Ÿ“‚ Phase 09: Object-Oriented Programming ๐Ÿ“… 2026 Master Edition
๐Ÿ“Œ Covered in this in-depth guide: Classes vs Objects ยท Access Specifiers ยท Encapsulation ยท Member Initializer Lists ยท const Member Functions ยท static Members ยท 4 Pillars of OOP

Welcome to Phase 9 (Chapter 9): C++ Object-Oriented Programming โ€” Classes, Objects & Encapsulation Masterclass! Object-Oriented Programming (OOP) bundles data members and member functions into cohesive class blueprints. In this guide, you will master access specifiers (public, private, protected), const member functions, static members, and the 4 pillars of OOP.

1The 4 Core Pillars of OOP
PillarDefinitionC++ Implementation
1. EncapsulationBundling data and methods into a single class unit, hiding internal state.private data members + public getter/setter methods.
2. AbstractionHiding complex implementation details and showing only high-level interface.Abstract classes with pure virtual functions (virtual void f() = 0;).
3. InheritanceDeriving new child classes from existing parent base classes to reuse code.class Derived : public Base { ... };
4. PolymorphismAbility to process objects differently based on their runtime data type.Virtual functions (virtual), function overriding & dynamic dispatch.
2Complete C++ Student Class Implementation
C++ โ€” Student Class with Encapsulation & const Membersโ–ถ Run Code in C++ Compiler
#include <iostream>
#include <string>

class Student {
private:
    std::string name;
    int age;
    double marks;
    static inline int totalStudents{0}; // C++17 inline static member

public:
    // Constructor with Member Initializer List
    Student(std::string studentName, int studentAge, double studentMarks)
        : name(studentName), age(studentAge), marks(studentMarks) {
        totalStudents++;
    }

    // Destructor
    ~Student() { totalStudents--; }

    // Const member function (guarantees no modification of data members)
    void displayDetails() const {
        std::cout << "Student: " << name << " | Age: " << age
                  << " | Marks: " << marks << "\n";
    }

    // Static member function
    static int getTotalStudents() { return totalStudents; }
};

int main() {
    Student s1("Ravi Kumar", 20, 87.5);
    Student s2("Anitha Roy", 21, 92.0);

    s1.displayDetails();
    s2.displayDetails();

    std::cout << "Total Active Students: " << Student::getTotalStudents() << "\n";
    return 0;
}
3Technical FAQs

Q1: Why mark member functions as const (e.g. void display() const)?

Marking a method const promises that it will not modify any data members. Required when operating on const class objects or const T& references!

Q2: What is the this pointer in C++?

An implicit pointer parameter passed to all non-static member functions that points to the invoking class instance object.

Q3: What is the default access specifier in a C++ class vs struct?

Class members default to private. Struct members default to public.

Q4: How do static data members work in C++?

A static data member is shared across ALL instances of the class (only 1 copy exists in memory).

Q5: What is class composition in C++?

Building complex classes by combining simpler objects as data members ("has-a" relationship), preferred over inheritance ("is-a").