The Class vs. Object Relationship
The Class vs. Object Relationship
Class (The Blueprint)
A user-defined data type that acts like an architectural map or drawing. It defines what variables and functions the object will have. Takes 0 bytes of RAM on its own.
Object (The Real Entity)
A real, physical instance of the class created at runtime. When you create an object, the system allocates actual RAM memory for it.
Explanation
- A class describes fields and operations for a type.
- Constructing objects creates separate instances with their own state.
Member Function Definition Styles
Member Function Definition Styles
Method 1: Inside the Class
You write the full code body right inside the
class { ... } block. The compiler automatically treats it as inline by default. Best for small 1–2 line functions.Method 2: Outside the Class
You only write the prototype (declaration) inside the class, and write the full code outside using the Scope Resolution Operator (::). Keeps the class neat and clean!
Explanation
- A member function may be defined in the class or defined later with a qualified name.
- Both forms refer to members declared by that class.
Object Memory Architecture in RAM
Object Memory Architecture in RAM
Code Segment (Loaded ONCE)
Member functions are stored in RAM only once when the program loads. All objects share the exact same function code!
Stack Memory (Separate per Object)
Memory for data variables (like
rollNo and marks) is allocated separately for every individual object.Explanation
- Each instance has storage for its own non-static fields.
- Invoking a member function identifies which instance supplies those fields.
Memory Allocation Architecture: Code Segment vs Object Data
Memory Allocation Architecture: Code Segment vs Object Data
Code Segment (Shared)
Member Functions Stored Exactly ONCE in RAM
void getDetails() & void display() shared by every objectObject 1 (s1 in RAM)
rollNo : 101marks : 85.5- Takes separate independent 8 bytes in RAM
Object 2 (s2 in RAM)
rollNo : 102marks : 92.0- Takes separate independent 8 bytes in RAM
Explanation
- Ordinary member functions can share one implementation across objects.
- Non-static data members belong to each object separately.
Mechanisms for Passing Objects
Mechanisms for Passing Objects
1. Pass by Value
void show(Complex c)Makes a complete duplicate copy of the object in RAM. Changes made inside do not affect the original object. Takes extra memory.
2. Pass by Reference
void show(Complex &c)Passes an alias (nickname) to the original object. Zero copying! Fast, but the function can modify original data.
3. Pass by Const Reference
void show(const Complex &c)Best Practice! Passes an alias for super fast speed, but
const prevents any modification to the object!Explanation
- Passing an object by value constructs a separate parameter object.
- A reference avoids that copy and a const reference prevents modification through that reference.
How a Friend Function Bridges Two Classes
How a Friend Function Bridges Two Classes
Class Alpha
private: int dataA
◀── Accesses private data ──
FRIEND FUNCTION: compare()
Declared with
friend keyword in both classes── Accesses private data ──▶
Class Beta
private: int dataB
Explanation
- A class can grant a named non-member function access to its private members.
- Two classes can both grant friendship when an operation needs data from each.