Here is an easy way to understand explanation of each of the assigned topics, along with example code and clear, simple language that could be helpful when learning.
Operator Overloading in C++
What is Operator Overloading? Operator overloading allows us to define how operators (like +, -, *, etc.) work with user-defined types (like classes). It essentially lets us give custom behaviors to operators when they’re used with objects of a class.
Example program:
#include <iostream> using namespace std; class Complex { public: float real, imag; Complex(float r = 0, float i = 0) : real(r), imag(i) {} // Overloading + operator Complex operator + (const Complex &obj) { Complex temp; temp.real = real + obj.real; temp.imag = imag + obj.imag; return temp; } }; int main() { Complex c1(3, 4), c2(1, 2); Complex result = c1 + c2; cout << "Result = " << result.real << " + " << result.imag << "i" << endl; return 0; }
Explanation:
Here, the +
operator is overloaded to add two complex numbers (c1
and c2
). When c1 + c2
is executed, it combines the real
and imaginary
parts.
Output:
Result = 4 + 6i
Function Overloading in C++
What is Function Overloading? Function overloading lets us create multiple functions with the same name but different parameters. This is useful when you want a function to perform similar tasks with different types or numbers of inputs.
Example Program:
#include <iostream> using namespace std; void display(int i) { cout << "Integer: " << i << endl; } void display(double d) { cout << "Double: " << d << endl; } int main() { display(5); // Calls display(int) display(5.5); // Calls display(double) return 0; }
Explanation:
The function display
is overloaded to accept both an int
and a double
. The compiler decides which function to call based on the argument type.
Output:
Integer: 5 Double: 5.5
Friend Function in C++
What are Friend Classes and Friend Functions? A friend function is a function declared with the keyword friend
in a class. This function can access the private and protected members of that class. Similarly, a friend class has access to the private and protected members of another class when declared as a friend.
Example program:
#include <iostream> using namespace std; class Box { private: int length; public: Box() : length(5) {} // Default length is 5 // Friend function declaration friend void showLength(Box); }; // Friend function definition void showLength(Box b) { cout << "Length of box: " << b.length << endl; } int main() { Box box; showLength(box); return 0; }
Explanation:
The showLength
function is a friend of the Box
class, allowing it to access the private member length
directly.
Output:
Length of box: 5
Key points:
- Operator Overloading: Allows customizing operators for user-defined types.
- Function Overloading: Enables multiple functions with the same name but different parameter types or counts.
- Friend Classes and Functions: Provides special access to another class’s private/protected members without being a member of the class.