←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Diagrams

Diagrams

Visual representations for Unit 3

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.
Detailed Notes →Quick Revision →

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.
Detailed Notes →Quick Revision →

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.
Detailed Notes →Quick Revision →

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 object

Object 1 (s1 in RAM)

  • rollNo : 101
  • marks : 85.5
  • Takes separate independent 8 bytes in RAM

Object 2 (s2 in RAM)

  • rollNo : 102
  • marks : 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.
Detailed Notes →Quick Revision →

Instance Variables vs. Shared Static Class Variable

Instance Variables vs. Shared Static Class Variable
Object 1 (emp1)
id = 101, salary = 40000
── Shares ──▶
ONE SHARED STATIC VARIABLE
static int totalCount = 2
(Stored only once in Global Memory)
◀── Shares ──
Object 2 (emp2)
id = 102, salary = 55000
Explanation
  • An instance field can differ between two objects.
  • A static data member is shared by instances of the class.
Detailed Notes →Quick Revision →

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.
Detailed Notes →Quick Revision →

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.
Detailed Notes →Quick Revision →