In this example program, the decrement -- operator is overloaded. i.e., decrease the value of a data member by 1 if the -- operator operates on an object.
Polymorphism: Polymorphism (or operator overloading) is a manner in which OO systems allow the same operator name or symbol to be used for multiple operations. That is, it allows the operator symbol or name to be bound to more than one implementation of the operator.
Before practicing the concept of operator overloading, you should have to know about the below topics.
A class is a blueprint for the object.
We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
class className {
// some data
// some functions
};
A class is defined in C++ using the keyword class followed by the name of the class.
The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.
When a class is defined, only the specification for the object is defined; no memory or storage is allocated.
To use the data and access functions defined in the class, we need to create objects.
className objectVariableName;
// sample function
void sampleFunction() {
// create objects
Room room1, room2;
}
int main(){
// create objects
Room room3, room4;
}
Here, two objects room1 and room2 of the Room class are created in sampleFunction(). Similarly, the objects room3 and room4 are created in the main().
As we can see, we can create objects of a class in any function of the program. We can also create objects of a class within the class itself, or in other classes.
A constructor is a special type of member function that is called automatically when an object is created.
In C++, a constructor has the same name as that of the class and it does not have a return type.
class Wall {
public:
// create a constructor
Wall() {
// code
}
};
Here, the function Wall() is a constructor of the class Wall. Notice that the constructor
• has the same name as the class,
• does not have a return type, and
• is public
In C++, we can change the way operators work for user-defined types like objects and structures. This is known as operator overloading.
class className {
... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};
Here,
• returnType is the return type of the function.
• operator is a keyword.
• symbol is the operator we want to overload. Like: +, <, -, ++, etc.
• arguments are the arguments passed to the function
Create a class check and declare a variable i in private
The class members declared as private can be accessed only by the member functions inside the class.
A constructor check () is created in public and set the value of variable i to 0. Here, we are using the operator -- to work in a way that increases the value of the variable by 1.
Void operator --()
{ --i; }
A function to display the value of the variable is defined.
Void Display()
{ cout << “i=” << i << endl; }
Within the main function body, create an object obj for the class check. Display the initial value of the data member i for object obj.
Involke the operator function void operator --( )
--obj;
Display the value of the data member i after the operation
obj.Display();
Algorithm
Step 1: Call the header file iostream.
Step 2: Use the namespace std.
Step 3: Create a class check with a private variable i
Step 4: create a constructor check( ); set the value of i to 0
Step 5: Define the function for operator overloading
Step 6: Define function display for displaying the data member.
Step 7: call the function main
Step 8: create an object obj for the class check
Step 9: Display the value of the data member by accessing the function with the object
Step 10: call the function for operator overloading
Step 11: Display the data member after the operation by calling the display function.
Step 12: Exit
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
void operator --()
{ --i; }
void Display()
{ cout << "i=" << i << endl; }
};
int main()
{
Check obj;
// Displays the value of data member i for object obj
obj.Display();
// Invokes operator function void operator --( )
--obj;
// Displays the value of data member i for object obj
obj.Display();
return 0;
}
i=0 i=-1