C++ Programs

Function Overloading in C++

Aim(Function Overloading)

To write a C++ program to demonstrate function overloading with default arguments and inline function by finding the area of a square, circle, and rectangle.

Procedure

Step 1: Include the necessary header files for the program.
Step 2: Define a class named “shape” to encapsulate different shape-related functionalities.
Step 3: Define various public member functions with the same name “area()” with different parameters by overloading functions with default arguments and an inline function.
Step 4: Implement the member functions to calculate the area of a square, circle, and rectangle.
Step 5: In the main() function, create an object “s” for the class “shape”.
Step 6: Get the inputs from the user and pass them through the functions , by invoking the area() member functions using the object , to find the area of square, circle, and rectangle.
Step 7: Print the results.
Step 8: Terminate the program.

Source Code(Function overloading)

Output

Function overloading

Explanation

1. Function Overloading : Function overloading enables the implementation of multiple functions with the same name but with different parameters.The programmer can distinguish the functions by number of parameters, datatype of parameters and return type.
In this program, the “area” function is overloaded within the “shape” class. There are three “area” functions:
(i) The first function with an integer argument (for calculating the area of a square).
(ii) The second function with two integer arguments (for calculating the area of a rectangle).
(iii) The third function with  a double argument (for calculating the area of a circle).
This allows the program to call the appropriate “area” function based on the number and types of arguments , passed by the user.

2. Default Arguments : Default arguments provide default values for function parameters, if  there is no argument value is passed during the function call. This default argument is defined by the programmer.
In the above program, the “area” function for calculating the area of a circle has a default argument for “pi” with a value of 3.14. If the user does not provide a value for “pi” when calling this function, it will use the default value of 3.14.

3. Inline Functions :  Inline functions are functions whose code is expanded at the time of the function call, rather than being executed through a function call mechanism.
In the above program, the “area” function for calculating the area of a square is declared as an inline function using the “inline” keyword. This means that the function call is replaced with the actual code of the function during compilation, which can result in faster execution compared to a regular function call.

We hope this article has helped readers learn how to write a C++ program to demonstrate function overloading, default arguments, and inline functions. This C++ practical program is  included in the I year BCA TANSCHE syllabus.

If you found this article helpful, consider sharing it with your friends. For more tutorials and content, subscribe to our YouTube channel and follow us on TwitterInstagramLinkedIn and  Facebook.

Share the Article

1 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Spread the code...
0
Would love your thoughts, please comment.x
()
x