Object Oriented Programming · Unit 5
Function Features and Inline Functions
Exam-focused notes for Function Features and Inline Functions (Object Oriented Programming, BIT153): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
Function Features and Inline Functions in short
This chapter covers features that make functions more flexible: default arguments, overloading, inline functions and friend functions. It also shows how a class can give special access to outside code.
What this unit covers
- Default arguments
- Function overloading
- Inline functions: advantages and disadvantages
- Friend functions
- Friend classes
- Member functions defined outside class
Default arguments
2082 · 5 marks
Explain default arguments and inline functions in C++ with suitable examples. [5]
2079 · 5 marks
In which case default argument is used? Describe with an example. [5]
0 · 5 marks
What is meant by default argument? Explain with suitable example. [5]
Friend functions
2082 · 5 marks
Explain friend function and friend class with example. [5]
Show solved answerHide answer
Friend Function and Friend Class in C++
Friend Function
A friend function is a function that is not a member of a class but has access to the private and protected members of that class. It is declared inside the class using the keyword friend.
Key Points:
- It is defined outside the class scope
- It is not called using an object (no
thispointer) - It can access private data of the class directly
Example of Friend Function
#include <iostream>
using namespace std;
class Rectangle {
private:
float length, breadth;
public:
Rectangle(float l, float b) {
length = l;
breadth = b;
}
// Declaring friend function
friend float calculateArea(Rectangle r);
};
// Definition of friend function (outside the class)
float calculateArea(Rectangle r) {
// Accessing private members directly
return r.length * r.breadth;
}
int main() {
Rectangle rect(5.0, 3.0);
cout << "Area = " << calculateArea(rect) << endl;
return 0;
}
Output:
Area = 15
Friend Class
A friend class is a class that is granted access to the private and protected members of another class. When a class is declared as a friend, all member functions of that friend class can access private data of the other class.
Key Points:
- Declared using
friend class ClassName;inside the class - Friendship is not mutual (if A is friend of B, B is not automatically friend of A)
- Friendship is not inherited
Example of Friend Class
#include <iostream>
using namespace std;
class Engine {
private:
int horsepower;
public:
Engine(int hp) {
horsepower = hp;
}
// Declaring Car as a friend class
friend class Car;
};
class Car {
public:
void showEngineDetails(Engine e) {
// Accessing private member of Engine class
cout << "Engine Horsepower: " << e.horsepower << endl;
}
};
int main() {
Engine eng(150);
Car car;
car.showEngineDetails(eng);
return 0;
}
Output:
Engine Horsepower: 150
Summary Table
| Feature | Friend Function | Friend Class |
|---|---|---|
| Type | A standalone function | A complete class |
| Declaration | friend returnType funcName(); | friend class ClassName; |
| Access | Private members of one class | Private members of another class |
| Membership | Not a member of the class | Not a member of the class |
Note: Use of friend functions and classes should be minimized as they break the principle of data encapsulation in Object-Oriented Programming.
2080 · 5 marks
What is friend function? Write a program to add private member of two different classes using friend function. [5]
2080.1 · 5 marks
What is friend function? Write a program to add any two private numbers of two different classes using friend function. [1+4]
Show solved answerHide answer
Friend Function
Definition
A friend function is a function that is not a member of a class but has access to the private and protected members of that class. It is declared inside the class using the keyword friend. A friend function can be a friend of more than one class at the same time, which allows it to access private data of multiple classes.
Key points:
- Declared with the
friendkeyword inside the class body - Defined outside the class without using the scope resolution operator (
::) - Not called using an object of the class
- Can access private and protected members of the class it is declared friend of
Program: Add Two Private Numbers from Two Different Classes
#include <iostream>
using namespace std;
// Forward declaration of class B
class B;
class A {
private:
int num1;
public:
A(int x) {
num1 = x;
}
// Declaring friend function
friend void addNumbers(A, B);
};
class B {
private:
int num2;
public:
B(int y) {
num2 = y;
}
// Declaring friend function
friend void addNumbers(A, B);
};
// Definition of friend function
void addNumbers(A obj1, B obj2) {
int sum;
sum = obj1.num1 + obj2.num2; // Accessing private members of both classes
cout << "Sum of two private numbers = " << sum << endl;
}
int main() {
A a(15);
B b(25);
// Calling friend function directly (not using object)
addNumbers(a, b);
return 0;
}
Output
Sum of two private numbers = 40
Explanation
| Step | Description |
|---|---|
class B; | Forward declaration so class A can reference class B |
friend void addNumbers(A, B); | Declared in both classes to grant access |
obj1.num1 | Accessing private member of class A inside friend function |
obj2.num2 | Accessing private member of class B inside friend function |
addNumbers(a, b) | Called directly without any object, like a normal function |
Note: The friend function
addNumbers()is not a member of either class, yet it can freely access the private data membersnum1andnum2of both classes A and B respectively.
Inline functions
2081 · 5 marks
Explain advantages and disadvantages of inline function. [5]
Friend classes
2079 · 5 marks
Why do we need friend class? How can we define member function outside the class? [5]
Function overloading
2080.1 · 5 marks
What is function overloading? Explain with an example. [5]
Make Unit 5 stick
Practice BIT153 with flashcards & quizzes