0% found this document useful (0 votes)
8 views5 pages

Function Overloading in C++ Explained

Uploaded by

mayursarode820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Function Overloading in C++ Explained

Uploaded by

mayursarode820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical No.

18: Write programs which show the use of function overloading


18.1 Exercise :-
1. Does constructor overloading is implementation of function overloading?
Yes, constructor overloading is a form of function overloading.
Function overloading is a feature of object-oriented programming (OOP) where multiple
functions with the same name can be defined, but with different parameter lists. This allows
a function to behave differently based on the number and types of arguments passed to it.
Constructor overloading is a specific application of function overloading to constructors,
which are special functions used to initialize objects when they are created. In constructor
overloading, multiple constructors with the same name (since constructors have the same
name as the class) can be defined, but with different parameter lists.
Here are the key characteristics of constructor overloading:

1. Multiple constructors with the same name (class name)


2. Different parameter lists (number and/or types of parameters)
3. Compilers resolve which constructor to call based on the arguments provided

Constructor overloading allows for flexibility in object initialization, enabling you to create
objects in different ways.

4. Which of the following in Object Oriented Programming is supported by Function


overloading and default arguments features of C++?
a) Inheritance b) Polymorphism c) Encapsulation d) None of these

The correct answer is:


b) Polymorphism
Function overloading and default arguments in C++ support polymorphism, which is the
ability of an object to take on multiple forms. Polymorphism allows for more flexibility in
programming, enabling objects of different classes to be treated as objects of a common
superclass.
Function overloading is a form of compile-time polymorphism (static polymorphism), where
multiple functions with the same name can be defined, but with different parameter lists.
Default arguments also contribute to polymorphism by allowing functions to adapt to
different situations based on the input provided.
Here's why the other options are incorrect:
a) Inheritance: Inheritance is supported by C++ features like derived classes, base classes,
and access modifiers.
c) Encapsulation: Encapsulation is supported by C++ features like classes, objects, access
modifiers (public, private, protected), and data hiding.
d) None of these: Incorrect, as function overloading and default arguments directly support
polymorphism.
4. Overloaded functions are
a) Very long functions that can hardly run
b) One function containing another one or more functions inside it.
c) Two or more functions with the same name but different number parameters
or type.
d) None of these
The correct answer is:
c) Two or more functions with the same name but different number parameters or type.
Function overloading is a feature in C++ where two or more functions can share the same
name, but must have:

1. Different number of parameters


2. Different types of parameters
3. Different order of parameters

This allows multiple functions with the same name to be defined, enabling:

 Increased flexibility
 Improved code readability
 Reduced function name duplication

18.2 Practical Related Questions


1. Write a C++ Program to interchange the values of two int , float and
char using function overloading.
#include <iostream>
using namespace std;

// Function to swap two integers


void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

// Function to swap two floats


void swap(float &a, float &b) {
float temp = a;
a = b;
b = temp;
}

// Function to swap two characters


void swap(char &a, char &b) {
char temp = a;
a = b;
b = temp;
}
OUTPUT
int main() {
int intA = 5, intB = 10;
float floatA = 3.5f, floatB = 7.8f;
char charA = 'A', charB = 'B';

cout << "Before swapping:" << endl;


cout << "intA = " << intA << ", intB = " << intB << endl;
cout << "floatA = " << floatA << ", floatB = " << floatB << endl;
cout << "charA = " << charA << ", charB = " << charB << endl;

swap(intA, intB);
swap(floatA, floatB);
swap(charA, charB);

cout << "\nAfter swapping:" << endl;


cout << "intA = " << intA << ", intB = " << intB << endl;
cout << "floatA = " << floatA << ", floatB = " << floatB << endl;
cout << "charA = " << charA << ", charB = " << charB << endl;

return 0;
}

2. Write a C++ Program that find the sum of two int and double number
using function overloading.
#include <iostream>
using namespace std;

// Function to add two integers


int add(int a, int b) { OUTPUT
return a + b;
}

// Function to add two doubles


double add(double a, double b) {
return a + b;
}

// Function to add one integer and one double


double add(int a, double b) {
return a + b;
}

// Function to add one double and one integer


double add(double a, int b) {
return a + b;
}

int main() {
int intA = 5, intB = 10;
double doubleA = 3.5, doubleB = 7.8;

cout << "Sum of two integers: " << add(intA, intB) << endl;
cout << "Sum of two doubles: " << add(doubleA, doubleB) << endl;
cout << "Sum of int and double: " << add(intA, doubleA) << endl;
cout << "Sum of double and int: " << add(doubleA, intB) << endl;

return 0;
}

3. Write C++ program to find the area of various geometrical shapes by Function overloading.
([Link] of circle, circumference of circle etc…)

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159;

// Function to calculate area of circle


double area(double radius) {
return PI * radius * radius;
}

// Function to calculate circumference of circle


double circumference(double radius) {
return 2 * PI * radius;
}

// Function to calculate area of rectangle


double area(double length, double width) {
return length * width;
}

// Function to calculate perimeter of rectangle


double perimeter(double length, double width) {
return 2 * (length + width);
}

// Function to calculate area of triangle


double area(double base, double height) {
return 0.5 * base * height;
}

// Function to calculate perimeter of triangle (assuming isosceles)


double perimeter(double side1, double side2, double side3) {
return side1 + side2 + side3; Output:
}
Geometry Calculator
int main() { 1. Circle
2. side3;
double radius, length, width, base, height, side1, side2, Rectangle
3. Triangle
cout << "Geometry Calculator" << endl; Enter your choice: 1
cout << "1. Circle" << endl; Enter radius of circle: 5
cout << "2. Rectangle" << endl; Area of circle: 78.5398
cout << "3. Triangle" << endl; Circumference of circle: 31.4159
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "Enter radius of circle: ";
cin >> radius;
cout << "Area of circle: " << area(radius) << endl;
cout << "Circumference of circle: " << circumference(radius) << endl;
break;
case 2:
cout << "Enter length and width of rectangle: ";
cin >> length >> width;
cout << "Area of rectangle: " << area(length, width) << endl;
cout << "Perimeter of rectangle: " << perimeter(length, width) << endl;
break;
case 3:
cout << "Enter base and height of triangle: ";
cin >> base >> height;
cout << "Area of triangle: " << area(base, height) << endl;
cout << "Enter sides of triangle (a b c): ";
cin >> side1 >> side2 >> side3;
cout << "Perimeter of triangle: " << perimeter(side1, side2, side3) << endl;
break;
default:
cout << "Invalid choice." << endl;
}

return 0;
}

You might also like