C++ Classes and Objects Fundamentals

Understanding Classes and Objects

A clas serves as an abstract blueprint for objects, while an object represents a concrete instance of that class. Classes are conceptual and don't occupy memory, whereas objects are physical entities that consume storage space.

Procedural vs Object-Oriented Paradigms

C follows a procedural approach focusing on data and methods within processes. C++ employs object-oriented principles, concentrating on object attributes and functionality.

Class Definition Structure

Basic Class Syntax

  1. The class keyword initiates class definition, followed by the class name and body enclosed in braces. Terminate with a semicolon.
  2. Member variables typically use prefixes like _ or m for identification (convention, not requirement).
  3. While struct can define classes in C++, class is preferred for explicit object-oriented design.
  4. Member functions defined within the class default to inline.
class Date {
public:
    void Display() {
        cout << day_ << "/" << month_ << "/" << year_ << endl;
    }
    void SetDate(int y, int m, int d) {
        year_ = y;
        month_ = m;
        day_ = d;
    }
private:
    int year_;
    int month_;
    int day_;
};

Access Specifiers

  1. Encapsulation is achieved through access modifiers that control member visibility.
  2. public members are externally accessible; protected and private members are restricted.
  3. Access scope extends from the specifier until the next one or class end.
  4. Default access differs: class members are private, struct members are public.
  5. Typically, member variables are private/protected, while public interfaces expose functionality.

Class Scope

  1. Classes establish their own scope. External member definitions require scope resolution (::).
  2. The compiler searches the class scope when resolving member references.
class Stack {
public:
    void Initialize(int capacity);
private:
    int* data_;
    size_t capacity_;
    size_t top_;
};

void Stack::Initialize(int cap) {
    data_ = new int[cap];
    capacity_ = cap;
    top_ = 0;
}

Object Instantiation

Instantiation Process

  1. Creating physical objects from class blueprints allocates actual memory.
  2. Multiple objects can be instantiated from one class, each with independent storage.
Date appointment;
Date deadline;
appointment.SetDate(2024, 6, 15);
deadline.SetDate(2024, 12, 31);

Object Memory Layout

  • Objects contain only member variables (data occupies memory).
  • Member functions reside in code segments; objects don't store function implementations.
  • Memory alignment rules apply to object layout:
    1. First member at offset 0
    2. Subsequent members align to their size or compiler default
    3. Total size aligns to largest member's alignment requirement

The this Pointer

When member functions execute, they implicitly receive a this pointer referencing the calling object. The compiler automatically:

  1. Adds a this parameter to member functions
  2. Translates member access through this->
  3. Prohibits explicit this parameter declaration
class Date {
public:
    void SetDate(int y, int m, int d) {
        // Implicit: this->year_ = y;
        year_ = y;
        month_ = m;
        day_ = d;
    }
private:
    int year_;
    int month_;
    int day_;
};

Tags: C++ Object-Oriented Programming Classes Objects this Pointer

Posted on Sun, 10 May 2026 18:36:04 +0000 by eyalrosen