Name: Pal Jain
PRN: 24070123067
Class: ENTC A3
To study constructor overloading, function overloading, and operator overloading in C++.
In Object-Oriented Programming, overloading refers to using the same name for a function, constructor, or operator in such a way that its behavior adapts based on the input or context. This makes code cleaner, simpler, and easier to reuse. Instead of creating multiple function names for similar tasks, one name is reused with different definitions. Overloading improves code readability and helps in writing modular programs.
A constructor can accept different types of arguments to create an object in multiple ways. Operators can be redefined so objects behave like built-in data types during mathematical operations.
-
Constructor Overloading When a class includes multiple constructors with different parameter lists, allowing objects to be created in multiple ways.
-
Function Overloading When a function name is reused with different parameter types or numbers of parameters, and the compiler decides which function to call during compilation.
-
Operator Overloading Redefining operators (like
+
,-
,*
,/
) for user-defined types, enabling intuitive operations between objects.
class ClassName {
public:
ClassName() {
// default initialization
}
ClassName(int a, int b) {
// initialization with parameters
}
};
class ClassName {
public:
void display() {
// no argument version
}
void display(int a) {
// one integer argument version
}
void display(double x, double y) {
// two double arguments version
}
};
class ClassName {
int value;
public:
ClassName(int v = 0) {
value = v;
}
ClassName operator+(ClassName &obj) {
ClassName temp;
temp.value = value + obj.value;
return temp;
}
};
This program demonstrates multiple constructors in a class. A class has three constructors with different parameter lists. The compiler automatically selects the appropriate constructor based on the arguments during object creation. Different constructors handle adding integers, floating-point numbers, or multiple integers.
This program demonstrates constructor overloading using a Product
class.
One constructor calculates profit or loss from cost price, selling price, and quantity sold.
Another constructor calculates remaining stock by subtracting sold items from total stock.
It shows how the same class can perform different tasks using constructor overloading.
- Start
- Define class
Product
. - Create a constructor to calculate profit/loss using cost price, selling price, and items sold.
- Create another constructor to calculate remaining stock from total and sold quantity.
- In
main()
, create objects using both constructors. - Display results.
- End
The Shapes
class demonstrates function overloading by defining multiple circumference()
functions with different parameter types.
Depending on the arguments passed, it calculates the circumference or perimeter of a square, circle, rectangle, or triangle.
This avoids writing separate functions with different names and keeps the code simple and easy to manage.
-
Start
-
Define class
Shapes
. -
Create multiple
circumference()
functions:- For square circumference (takes one double and an int as dummy argument)
- For circle circumference (takes one double argument)
- For rectangle perimeter (takes two double arguments)
- For triangle perimeter (takes three double arguments)
-
In
main()
, create objects ofShapes
class. -
Call
circumference()
with appropriate arguments for each shape. -
Display the results.
-
End
This program demonstrates operator overloading by redefining the +
operator for a ComplexNumber
class.
It allows adding complex numbers using the simple syntax num1 + num2
.
The overloaded operator function adds the realPart
and imaginaryPart
of the objects and returns a new object with the result.
This makes complex addition as intuitive as working with integers.
- Start
- Define class
ComplexNumber
with membersrealPart
andimaginaryPart
. - Write a constructor to initialize values.
- Overload the
+
operator to add twoComplexNumber
objects. - In
main()
, create two objects, add them using+
, and display the result usingdisplay()
. - End
This program demonstrates operator overloading by redefining arithmetic operators (+
, -
, *
, /
) for a Calculator
class.
It allows objects to interact using natural arithmetic syntax, e.g., num1 + num2
.
Each overloaded operator performs the respective operation on the value
members of two objects and returns a new object with the result.
This makes arithmetic operations with objects as intuitive as working with primitive data types.
-
Start
-
Define class
Calculator
with a membervalue
. -
Write a constructor to initialize
value
. -
Overload operators:
+
to add twoCalculator
objects-
to subtract twoCalculator
objects*
to multiply twoCalculator
objects/
to divide twoCalculator
objects
-
Write a
display()
function to print thevalue
. -
In
main()
, create twoCalculator
objects. -
Perform addition, subtraction, multiplication, and division using overloaded operators.
-
Display the results.
-
End
- Constructor overloading enables multiple ways to initialize objects.
- Function overloading allows using the same function name for different tasks, improving readability.
- Operator overloading makes user-defined types behave like built-in types, simplifying interactions.
These techniques promote a clean, modular, and object-oriented programming style.