OOP Using C++ – C++ Programming Functions based Questions and Answers

Lists of Questions and Answers

  • 1. Explain Call by Value and Call by Reference with appropriate example.
  • 2. What is inline function? Explain with example
  • 3. Explain Function with Default Arguments with appropriate example
  • 4. What is friend function? Explain with example.
  • 5. Explain function overloading with example.
  • 6. Explain operator overloading with example

 

Question – 1. Explain Call by Value and Call by Reference with appropriate example.    

  • In call by value, value of variable is passed during function call.
  • And copy this value in another variable at function definition.
  • In call by value the original value in calling function will never change after execution of function.

 

Example:

#include<iostream>

using namespace std;

void swp(int a, int b)

{

int temp=a;

a=b;

b=temp;

}

int main()

{

int x,y;

cout<<"enter value of a and b:";

cin>>x>>y;

swp(x,y);

cout<<”\nafter swapping a = ”<<a<<”and b = ”<<b;

return 0;

}

Output:

Enter value of a and b:

4

5

After swapping a = 5 and b = 4

 

Call By Reference

  • In call by reference, reference is passed during function call.
  • The formal arguments in the called function become aliases to the actual function call.
  • In call by reference the original value in calling function will change after execution of function.

 

Example:

#include<iostream>

using namespace std;

void swap(int &a, int &b) // It is the only difference from above program

{

int temp=a;

a=b;

b=temp;

}

int main()

{

int a,b;

cout<<"Enter two numbers:";

cin>>a>>b;

swap(a,b);

cout<<"a="<<a<<"b="<<b;

return 0;

}

Output:

Enter value of a and b:

4

5

After swapping a = 5 and b = 4

 

Question – 2. What is inline function? Explain with example.

  • The functions can be made inline by adding prefix inline to the function definition.
  • An inline function is a function that is expanded in line when it is invoked.
  • The complier replaces the function call with the corresponding function code.
  • Inline function saves time of calling function, saving registers, pushing arguments onto the stack and returning from function.
  • Preprocessor macros are popular in C, which has similar kind of advantages mentioned above.
  • The major drawback of macros is that they are not really functions.
  • Therefore, the usual error checking does not occur during execution of macros.
  • We should be careful while using inline function. If function has very few lines of code and simple expressions then only it should be used.
  • Critical situations for inline function:

1) If a loop, a switch or a goto exists in function body.

2) If function is not returning any value.

3) If function contains static variables.

4) If function is recursive.

 

Example:

inline int cube(int n)

{

return n*n*n;

}

int main()

{

int c;

c = cube(10);

cout<<c;

}
  • Function call is replaced with expression so c = cube(10); becomes c=10*10*10; at compile time

 

Question – 3. Explain Function with Default Arguments with appropriate example.

  • C++ allows us to call a function without specifying all its arguments.
  • In such cases, the function assigns a default value to the parameter.
  • Default values are specified when the function is declared.
  • We must add default arguments from right to left.
  • We cannot provide a default value to a particular argument in the middle of an argument list.
  • Default arguments are useful in situations where some arguments always have the same value.

For Example, passing marks.

  • Legal and illegal default arguments
    • void f(int a, int b, int c=0); // legal
    • void f(int a, int b=0, int c=0); // legal
    • void f(int a=0, int b, int c=0); // illegal
    • void f(int a=0, int b, int c); // illegal
    • void f(int a=0, int b=0, int c=0); // legal

 

Example:

 

#include <iostream>

    using namespace std;

    void f(int a=0, int b=0)

    {

        cout<< "a= " << a << ", b= " << b<<endl;

    }

    int main()

    {

        f();

        f(10);

        f(10, 99);

        return 0;

    }

Output:

a=0 , b=0

a=10 ,b=0

a=10, b=99

 

Question – 4. What is friend function? Explain with example.

A friend function is a function which is declared using friend keyword.

  • It is not a member of the class but it has access to the private and protected members of the class.
  • It is not in the scope of the class to which it has been declared as friend.
  • It cannot access the member names directly.
  • It can be declared either in public or private part of the class.
  • It is not a member of the class so it cannot be called using the object.
  • Usually, it has the objects as arguments.
  • It is normal external function which is given special access privileges.

Syntax:

class ABC

{

public:

……………………………………………

friend void xyz(void); // declaration

……………………………………………

};

 

Example:

#include<iostream>

using namespace std;

class numbers

{

int num1, num2;

public:

void setdata(int a, int b);

friend int add(numbers N);

};

void numbers :: setdata(int a, int b)

{

num1=a;

num2=b;

}

int add(numbers N)

{

return (N.num1+N.num2);

}

int main()

{

numbers N1;

N1.setdata(10,20);

cout<<”Sum = ”<<add(N1);

}

 

Output: Sum = 30

 

  • add is a friend function of the class numbers so it can access all members of the class(private, public and protected).
  • Member functions of one class can be made friend function of another class, like…
class X

{

………………………………………

int f();

};

class Y

{

………………………………………

friend int X :: f();

};

The function f is a member of class X and a friend of class Y.

  • We can declare all the member functions of one class as the friend functions of another class. In such cases, the class is called a friend class, like class X is the friend class of class Z

 

class Z

{

………………………………………

friend class X;

……………………………………….

};

 

Question – 5. Explain function overloading with example.

  • Function overloading is compile time polymorphism.
  • Function overloading is the practice of declaring the same function with different signatures.
  • The same function name will be used with different number of parameters and parameters of different type.
  • Overloading of functions with different return types is not allowed.
  • Compiler identifies which function should be called out of many using the type and number of arguments.
  • A function is overloaded when same name is given to different functions.
  • However, the two functions with the same name must differ in at least one of the following,
  1. a) The number of parameters
  2. b) The data type of parameters
  3. c) The order of appearance

 

Example:

#include <iostream>

using namespace std;

class Math

{

public:

void Add(int num1, int num2) //Function 1

{

cout<<num1 + num2 <<endl;

}

void Add(float num1, float num2) //Function 2

{

cout<<num1 + num2 <<endl;

}

void Add(int num1, int num2, int num3) //Funciton 3

{

cout<<num1 + num2 + num3 <<endl;

}

};

int main()

{

Math m;

m.Add(10,20); \\ Calls function 1

m.Add(10.15, 25.70); \\ Calls function 2

m.Add(1,2,3); \\ Calls function 3

}

Output:

30

35.85

6

 

Question – 6. Explain operator overloading with example.

  • Operator overloading is compile time polymorphism.
  • The operator overloading provides mechanism to perform operations on user defined data type.
  • We can give special meaning to any operators in which program it is implemented.
  • Rules for operator overloading
  1. Only existing operator can be overloaded.
  2. The overloaded operator must have at least one operand that is user defined type.
  3. We cannot change the basic meaning and syntax of an operator.
  4. We cannot use friend function to overload certain operators. However member function can be used to overload them.
  5. Unary operators, overloaded by means of a member function, take no explicit argument s and return no explicit value, but, those overloaded by means of a friend function, take one reference argument.
  6. Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.
  7. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class.
  8. We cannot overload following operators.

 

Operator Name
. and .* Class member access operator
:: Scope Resolution Operator
sizeof() Size Operator
?: Conditional Operator

 

         

Example for Unary Operator Overloading

#include <iostream>

using namespace std;

class sample

{

int a,b;

public:

void getdata()

{

a=10;

b=20’

}

void operator -() //Unary Member Function

{

a = a - 5;

b = b - 5;

}

 void disp()

{

cout<<"\nThe value of a="<<a;

cout<<"\nThe value of b="<<b;

}

};

int main()

{

sample S;

S.getdata();

    -S; //Call Unary Member Function

S.disp();

getch();

return 0;

}

Example for Unary Operator Overloading using Friend function.

#include <iostream>

using namespace std;

class sample

{

int a,b;

public:

void getdata()

{

a=10;

b=20;

}

friend sample operator +(sample A) //Unary Friend Function

{

A.a = A.a + 5;

A.b = A.b + 5;

return A;

}

void disp()

{

cout<<"\nThe value of a="<<a;

cout<<"\nThe value of b="<<b;

}

};

int main()

{

sample S;

S.getdata();

S=+S; //Call Unary Friend Function

S.disp();

getch();

return 0;

}

Example for Binary Operator Overloading

#include <iostream>

using namespace std;

class complex

{

float real, imag;

public:

complex(float _real, float _imag) // constructor

{

real = _real;

imag = _imag;

}

void disp()

{

cout<<"The value of real="<<real;

cout<<"The value of imag="<<imag;

}

void operator +(complex c) //Binary Member function

{

    real = real + c.real;

imag = imag + c.imag;

}

};

int main()

{

    complex x(4,4);

complex y(6,6);

    x + y; // Call Binary Member Function

    x.disp();

getch();

return 0;

}

Example of Binary operator overloading using friend function

#include <iostream>

using namespace std;

class complex

{

float real, imag;

public:

complex(float _real, float _imag) // constructor

{

real = _real;

imag = _imag;

}

void disp()

{

cout<<"The value of real="<<real;

cout<<"The value of imag="<<imag;

}

friend complex operator +(complex c, complex d) //Binary Friend function

{

d.real = d.real + c.real;

d.imag = d.imag + c.imag;

return d;

}

};

int main()

{

complex x(4,4);

complex y(6,6);

complex z = x + y; // Call Binary Friend Function

z.disp();

}