Chapter 4

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Chapter 4:

1. Describe inline function in C++ with example. Give its advantage ,


List some situations where
inline function may not work.

->>>>>An inline function is a function that is


expanded in place at each point in the code where
it is called, rather than being called like a regular
function. It is used to improve the performance of a program
by reducing the function call overhead.

Syntax:To declare a function as inline, you use the inline


keyword before the function declaration.
 Ex:
inline int square(int x) { return x * x; }
int main()
{
cout << "Square: " << square(5);
return 0;
}

Advantages:
 Does not require function calling overhead.(i.e Reduction in Function Call
Overhead)
 Saves time
 Less execution time(i.e faster execution of code.)
Disadvantage/Limitations:
Requires more memory.

The compiler may not perform inlining in such circumstances as:

 Large Functions: If a function is large


 Control structures: If a function contains a loop. (for, while and do-while) and
switch or goto statement.
 Virtual Functions: If a function contains static variables and virtual functions.
 Recursive Functions: If a function is recursive.
 I/O Operations: Functions involving I/O operations or system calls

2. Write a program to calculate the volume of different shapes like cube, cylinder
and sphere and
hence implement the concept of function overloading.

#include <iostream>
using namespace std;
const double PI = 3.142;

double Volume(float side) {


return side * side * side;
}

double Volume(double radius, double height)


{
return PI * radius * radius * height;
}

double Volume(double radius) {


return (4.0 / 3.0) * PI * radius * radius *
radius;
}
int main() {
float side=3.0; double radius=2.0, height=4.12;
cout << "\nVolume of the cube: " <<
Volume(side);

// Volume of a cylinder
cout << "\nVolume of the cylinder: " <<
Volume(radius, height);

// Volume of a spher
cout << "\nVolume of the sphere: " <<
Volume(radius) ;

return 0;
}

3. Explain Friend function with example. Enlist any 2 properties of friend


function.
-->>>>>Friend Function

Friend function is declared using the friend keyword


that is not a member of a class but is granted access to
the private and protected members of the class.
Primary use of the friend keyword is to allow a function
or class to access the private and protected members of
another class.

Properties of Friend Functions:


Not a Member of the Class:

A friend function is not a member function of the class for which it is declared as
a friend.
Can Access Private and Protected Members:

A friend function has the privilege to access the private and protected members of
the class for which it is a friend.
Declared Inside the Class:

The declaration of a friend function is done inside the class using the friend
keyword.

4. Explain a concept of call by reference with reference arguments for swapping of


two integer
numbers.
5. What is Constructor?. What are the characteristics of a constructor?
OR What are rules for declaring constructors?
6. Explain copy constructor with suitable example.
Or Constructor overloading with suitable example.
7. What is Constructor and Destructor? Explain types of Constructor with example
8. Define operator overloading? Discuss the rules for overloading operators.
9. Give the general syntax for operator overloading function. List the operators
cannot be
overloaded?
10. What is an operator overloading? Explain how to overload unary operator and
binary operator.
11. Write a program in C++ to overload unary minus operator.
12. Write program in C++ to add two complex number by operator overloading binary '
+ ' operator
and display the result.
13. Explain Basic to class type conversion with example.

You might also like