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
- The
classkeyword initiates class definition, followed by the class name and body enclosed in braces. Terminate with a semicolon. - Member variables typically use prefixes like
_ormfor identification (convention, not requirement). - While
structcan define classes in C++,classis preferred for explicit object-oriented design. - 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
- Encapsulation is achieved through access modifiers that control member visibility.
publicmembers are externally accessible;protectedandprivatemembers are restricted.- Access scope extends from the specifier until the next one or class end.
- Default access differs:
classmembers areprivate,structmembers arepublic. - Typically, member variables are
private/protected, while public interfaces expose functionality.
Class Scope
- Classes establish their own scope. External member definitions require scope resolution (
::). - 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
- Creating physical objects from class blueprints allocates actual memory.
- 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:
- First member at offset 0
- Subsequent members align to their size or compiler default
- 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:
- Adds a
thisparameter to member functions - Translates member access through
this-> - Prohibits explicit
thisparameter 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_;
};