5 Function Features And Inline Functions

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.

Function Features and Inline Functions: notes

Default arguments

A default argument is a value given in the function declaration. If the caller leaves out that argument, the default is used.

float interest(float p, float r = 5.0, int t = 1) {
    return p * r * t / 100;
}
int main() {
    cout << interest(1000);          // uses r=5, t=1
    cout << interest(1000, 8);       // t=1
    cout << interest(1000, 8, 2);    // no default used
}

Rules:

  • Defaults are given from right to left. After one default, all later parameters need defaults.
  • Defaults are written once, in the declaration or the definition, not both.
  • They reduce the need for several overloaded versions.

Function overloading

Function overloading means using the same function name for different functions. They must differ in the number, type or order of parameters. The return type alone is not enough.

int area(int s) { return s * s; }                 // square
int area(int l, int b) { return l * b; }          // rectangle
float area(float r) { return 3.14 * r * r; }      // circle

The compiler chooses the right one at compile time. This is static polymorphism. The call area(5) picks the first version, and area(2, 3) picks the second.

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]

--- Definition: Default arguments are values provided in a function declaration that are automatically used when the caller does not supply a corresponding argument. They allow a function to be called with fewer arguments than it is defined to accept. Rules: - Default arguments must be specified ...

Open this answer on its own page →

2079 · 5 marks

In which case default argument is used? Describe with an example. [5]

A default argument is used when a function is called without providing a value for one or more parameters. If the caller omits an argument, the compiler automatically substitutes the default value specified in the function declaration/definition. 1. When a function parameter has a commonly used v...

Open this answer on its own page →

0 · 5 marks

What is meant by default argument? Explain with suitable example. [5]

A default argument is a value provided in a function declaration that is automatically used by the compiler if the caller does not supply a corresponding argument in the function call. It allows a function to be called with fewer arguments than it is defined to accept. --- - Default arguments mus...

Open this answer on its own page →

Friend functions

2082 · 5 marks

Explain friend function and friend class with example. [5]

Show solved 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 this pointer)
  • 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

FeatureFriend FunctionFriend Class
TypeA standalone functionA complete class
Declarationfriend returnType funcName();friend class ClassName;
AccessPrivate members of one classPrivate members of another class
MembershipNot a member of the classNot 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.

Open this answer on its own page →

2080 · 5 marks

What is friend function? Write a program to add private member of two different classes using friend function. [5]

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, but it is defined outside the class like a normal function. Key Points: - It is declared with the friend key...

Open this answer on its own page →

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 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 friend keyword 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

StepDescription
class B;Forward declaration so class A can reference class B
friend void addNumbers(A, B);Declared in both classes to grant access
obj1.num1Accessing private member of class A inside friend function
obj2.num2Accessing 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 members num1 and num2 of both classes A and B respectively.

Open this answer on its own page →

Inline functions

2081 · 5 marks

Explain advantages and disadvantages of inline function. [5]

An inline function is a function declared with the inline keyword that requests the compiler to replace the function call with the actual function body code at the point of invocation, rather than performing a normal function call. --- - The function body is substituted directly at the call site....

Open this answer on its own page →

Friend classes

2079 · 5 marks

Why do we need friend class? How can we define member function outside the class? [5]

In C++, data members declared as private or protected are not accessible from outside the class. This enforces data encapsulation. However, sometimes it is necessary for an external class or function to access the private and protected members of another class. This is where the friend class is n...

Open this answer on its own page →

Function overloading

2080.1 · 5 marks

What is function overloading? Explain with an example. [5]

Function overloading is a feature in C++ (and other OOP languages) that allows multiple functions to have the same name but with different parameter lists (different number, type, or order of parameters). The compiler determines which function to call based on the arguments passed at the time of ...

Open this answer on its own page →