Friend Function in C++
• Special function that can access private and protected
members of a class
• Not a member of the class
• Helps in controlled access to private data
• Declared using the keyword friend
• Useful in real-world scenarios like banking or auditing
Definition:
•A friend function is a normal function with special
access
•Can access private and protected members of a
class
•Declared inside the class using friend keyword
•Defined outside the class like a normal function
•Called directly like a regular function
Example:
•Class Box has a private member length
class Box {
private:
•Friend function show Length accesses
int length; length
public: •Function defined outside the class
Box(int l) { length = l; } •Called directly using object as argument
friend void showLength(Box •Prints the value of private member
b);
};
• Bank Account has private balance
• Auditor needs to check the balance
• Auditor is a friend function
• Not part of the BankAccount class
• Can safely access balance without modifying it
• friend void audit(BankAccount acc);
Advantages:
•Can access private members safely
•Useful for cross-class operations
•Increases code flexibility
•Avoids exposing private data directly
•Helpful in real-life simulations like auditing or
reporting
Overloading Member Functions in
C++
Overloading Member Functions in C++
• A function with the same name but different inputs
• Helps do similar tasks in different ways
• Works inside a class
• Compiler decides which one to use
• Part of compile-time polymorphism
What is Function Overloading?
•Two or more functions have same name
•Different number or type of inputs
•Makes code shorter and easier
•Helps do similar work differently
•Compiler knows which function to call
class Math {
public:
int add(int a, int b) { return a + b; } // 2 numbers
int add(int a, int b, int c) { return a+b+c; } // 3 numbers
float add(float a, float b) { return a+b; } // 2 decimals
};
Math m;
[Link](10, 20); // calls first function
[Link](10,20,30); // calls second function
[Link](2.5,3.5); // calls third function
Real-Life Example
•Deposit cash → integer
•Deposit coins → float
•Deposit cheque → cheque number + amount
•All use same function name deposit
•Makes program easy to read and use
Advantages
•Makes code shorter and simple
•No need different function names for same task
•Works with different types of data
•Compiler automatically chooses function
•Used in ATM, Calculator, Payment Systems
Bit fields and classes in c++
•Special variable that uses 1 or few bits
•Saves memory instead of using full integer
•Declared using : and number of bits
•Can store 0 or 1, or small numbers
•Works inside class or struct
Real-World Example
•Machine Control Panel:
•Power → ON/OFF → 1 bit
•Error → YES/NO → 1 bit
•Mode → Manual/Auto → 2 bits
•Only 4 bits used instead of full integers
•Saves memory
•Makes program efficient and simple
Advantages:
•Saves memory
•Handles flags and small data
•Can be private or public
•Useful in embedded systems
•Makes program efficient
What is a Constructor?
•A constructor is a special function inside a class.
•It has the same name as the class.
•It runs automatically when an object is created.
•Used to set initial values for variables.
•It has no return type.
class Student {
public:
Student() {
cout << "Object Created";
}
};
What is a Destructor?
•Destructor also a special function.
•It starts with a ~ (tilde) symbol before class name.
•It runs when the object is destroyed or program ends.
•Used to free memory or clean up.
•Only one destructor per class.
class Student {
public:
~Student() {
cout << "Object Destroyed";
}
};
What is a Static Member?
•Declared using static keyword inside a class.
•Same value is shared by all objects.
•Does not depend on any single object.
•Used to count objects or store common data.
•Must be defined outside the class once.
Ex: static int count;
Real-Life Example:
•Think of students entering and leaving a classroom.
•When a student enters, constructor adds 1.
•When a student leaves, destructor subtracts 1.
•Static variable shows total students inside.
•Everyone shares the same “total count”.
"Every great
programmer was once
a beginner — just like
you!"