Write a C++ program that overloads a function to accept null and calculate the area. Show the complete code and the output.


Q.) Write a C++ program that overloads a function to accept null and calculate the area. Show the complete code and the output.

Subject: Object Oriented Programming and Methodology

I. Introduction

Function overloading in C++ is a feature that allows us to have more than one function with the same name but different parameters. The function that is to be executed is determined at compile time. This is achieved by the compiler's ability to distinguish between different functions based on their parameters.

In this task, we are required to create a C++ program that overloads a function to accept null and calculate the area. The function will be overloaded to calculate the area of a rectangle and a circle.

II. Detailed Steps

  1. Function Overloading in C++

Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++. The correct function to be invoked is determined based on the arguments passed.

  1. Null in C++

In C++, null is a constant that typically represents a pointer that does not point to a valid object. In the context of function overloading, we can have a function that accepts null as an argument and returns a default value.

  1. Area Calculation

The area of a rectangle is calculated as the product of its length and width (Area = length * width). The area of a circle is calculated as π times the square of its radius (Area = πr²).

III. Code Explanation

  1. Basic Structure of a C++ Program

A C++ program starts with the inclusion of headers, followed by the declaration and definition of functions, and finally the main function where the program execution begins.

  1. Overloaded Functions

We will define three overloaded functions. The first function will calculate the area of a rectangle, the second function will calculate the area of a circle, and the third function will accept null and return a default value.

  1. Main Function

In the main function, we will call the overloaded functions with appropriate arguments and display the results.

IV. Code

Here is the complete C++ code as per the explanation above:

#include 
#define PI 3.14159

// Function to calculate the area of a rectangle
double area(double length, double width) {
    return length * width;
}

// Function to calculate the area of a circle
double area(double radius) {
    return PI * radius * radius;
}

// Function to handle null
double area(std::nullptr_t nptr) {
    return 0;
}

int main() {
    double length = 5.0, width = 3.0, radius = 2.0;

    std::cout << "Area of rectangle: " << area(length, width) << std::endl;
    std::cout << "Area of circle: " << area(radius) << std::endl;
    std::cout << "Area when null is passed: " << area(nullptr) << std::endl;

    return 0;
}

V. Output

When the program is run with different arguments (null, rectangle dimensions, circle radius), the output will be:

Area of rectangle: 15
Area of circle: 12.5664
Area when null is passed: 0

VI. Conclusion

Function overloading in C++ is a powerful feature that allows us to have multiple functions with the same name but different parameters. In this program, we have used function overloading to calculate the area of a rectangle and a circle, and to handle null. This program can be extended to handle other shapes and null values in different ways.

Summary

Function overloading in C++ allows us to have more than one function with the same name but different parameters. In this task, we create a C++ program that overloads a function to accept null and calculate the area of a rectangle and a circle. The program demonstrates the use of function overloading and handling null values.

Analogy

Function overloading in C++ is like having multiple versions of a song with different singers. Each version has the same name (song title) but different singers (parameters). Similarly, in function overloading, we have multiple functions with the same name but different parameters.

Quizzes
Flashcards
Viva Question and Answers

Quizzes

What is function overloading?
  • Having multiple functions with the same name but different parameters
  • Having multiple functions with different names but the same parameters
  • Having a single function with multiple parameters
  • Having a single function with a single parameter