Classes & Objects in C++: Fundamentals & First OOP Program
Overview: Creating two Account objects from one class gives each account its own balance.
What is a Class and What is an Object?
- In traditional C programming, data and functions were kept separate.
- Class (The Blueprint) A user-defined data type that acts like an architectural map or drawing.
- Object (The Real Entity) A real, physical instance of the class created at runtime.
-
-
View diagram · The Class vs. Object Relationship
Syntax of a Class in C++
- To create a class, use the
classkeyword followed by the class name.
Access Specifiers: private vs. public vs. protected
- Access specifiers decide who is allowed to view or use the members of a class:
private(Default): Yespublic: Yesprotected: Yes- Exam Tip: In C++, if you don’t write anything, all members are
privateby default.
Your First Complete OOP Program in C++
- Here is a complete, working student management program:
The Dot Operator (.)
- To call a public function or access a public variable of an object inside
main(), we use the Dot Operator (.):
Defining Member Functions in C++: Outside Inlining & Nesting
Overview: An out-of-class definition uses the class name and scope resolution operator to identify its owner.
Two Ways to Define Member Functions
- In C++, when you write a function for a class, you can write its code in two different places:
- Method 1: Inside the Class You write the full code body right inside the class { …
- Method 2: Outside the Class You only write the prototype (declaration) inside the class, and write the full code outside using the Scope Resolution…
View diagram · Member Function Definition Styles
Defining Member Functions Outside Using ::
- When you write the function code outside the class body, how does the compiler know which class it belongs to?
Making an Outside Function Inline
- By default, any function defined outside the class is treated as a normal jumping function.
Nesting of Member Functions
- Normally, when you call a member function from
main(), you have to use an object and the dot operator:obj.display();.
Private Member Functions (Internal Helper Functions)
- Usually, member functions are placed in
public:so they can be called frommain().
Arrays Within a Class & Memory Allocation for Objects
Overview: Two Report objects each have their own marks array if it is an ordinary member.
Arrays as Member Data Within a Class
- In C++, variables inside a class don’t just have to be single numbers like
int rollNo;.
How Does C++ Allocate Memory for Objects?
- A very common myth among students is:
- Code Segment (Loaded ONCE) Member functions are stored in RAM only once when the program loads.
- Stack Memory (Separate per Object) Memory for data variables (like rollNo and marks ) is allocated separately for every individual object .
- Why is this design so clever?: Saves Massive RAM: If your class has 20 functions taking 100 KB of compiled code, creating 10,000 objects won’t waste 1 GB of memory!
- How does the shared function know which object called it?
View diagram · Object Memory Architecture in RAM View diagram · Memory Allocation Architecture: Code Segment vs Object Data
Static Data Members & Static Member Functions in C++
Overview: Each library card object can store its own card number while the class shares one static count.
What are Static Data Members?
- Normally, in a C++ class, every object gets its own separate copy of variables.
- Counting the total number of students currently enrolled.
- Storing a common bank interest rate that applies to all accounts.
- Object 1 (emp1) id = 101, salary = 40000
- ONE SHARED STATIC VARIABLE static int totalCount = 2 (Stored only once in Global Memory)
- Object 2 (emp2) id = 102, salary = 55000
- Key Rules for Static Data Members: Single Copy: Created only once when the program starts.
- Auto-Zero: If you don’t give it any initial value, C++ automatically sets it to zero (
0).
View diagram · Instance Variables vs. Shared Static Class Variable
What are Static Member Functions?
- Just like static variables, you can also make member functions
static. - Rules for Static Member Functions (Golden Exam Points): No Object Needed: You can call a static function directly using the class name:
ClassName::functionName();. - Access Restrictions: A static function can ONLY access static variables or other static functions.
- No
thisPointer: Because a static function doesn’t belong to any particular object, it has nothispointer.
Comparison Table: Regular vs. Static Members
- Memory Allocation: Copied separately inside every object
- How to Call?: Needs an object:
obj.display(); thisPointer: Has an implicitthispointer- What can it access?: Can access both static and regular variables
- Where to Initialize?: Usually inside constructors
Array of Objects, Passing Objects & Constant Member Functions
Overview: An array of Student objects contains separate students with their own fields.
Array of Objects in C++
- Just like you can create an array of 5 integers (
int arr[5];), you can also create an Array of Objects (Student classroom[60];).
Passing Objects to Functions
- You can pass objects into functions in three ways:
- 1. Pass by Value void show(Complex c) Makes a complete duplicate copy of the object in RAM.
- 2. Pass by Reference void show(Complex &c) Passes an alias (nickname) to the original object.
- 3. Pass by Const Reference void show(const Complex &c) Best Practice!
View diagram · Mechanisms for Passing Objects
Returning Objects from Functions
- Just as a function can return an
intorfloat, it can also return a complete object! - Classic Exam Example: Adding Two Complex Numbers: A complex number has a real part and an imaginary part (e.g.
Constant Member Functions (const)
- If a function’s job is only to display or check data without changing any variable, you should mark it with the
constkeyword at the… - Why is this important?: If you create a constant object (e.g.,
const Account acc;), that object is only allowed to call constant member functions!
Friend Functions, Member Pointers & Local Classes in C++
Overview: A friend function can access selected private members but is still a non-member function.
What is a Friend Function?
- In C++, the golden rule of Object-Oriented Programming is Data Hiding: outside functions are strictly forbidden from touching
private…
View diagram · How a Friend Function Bridges Two Classes
Special Rules for Friend Functions (Exam Favorites)
- Not a Member Function: A friend function is NOT a member of the class.
- No Dot Operator (
.): You cannot call it using an object (writingobj.friendFunc();is an error!). - Takes Objects as Arguments: Because it is an outside function, it has no
thispointer. - Placement Doesn’t Matter: You can write the
frienddeclaration underpublic:orprivate:inside the class; it works exactly the same.
Practical Code Example: Comparing Private Data of Two Classes
- When two different classes want to compare their private data, a friend function is the easiest solution:
Friend Classes
- Instead of making just one function a friend, you can make an entire class a friend of another class!
- Important Exam Rules About Friendship: Friendship is NOT Mutual: If Class B is a friend of Class A, Class A is NOT automatically a friend of Class B.
- Friendship is NOT Transitive: If B is a friend of A, and C is a friend of B, C is NOT automatically a friend of A!
Pointer to Class Members (.* and ->*)
- C++ allows you to create pointers that point to variables inside a class:
Local Classes in C++
- A class that is defined completely inside a function is called a Local Class.
- Rules for Local Classes: It can only be used inside that specific function.
- It cannot contain static data members.