operator overloading (1)
operator overloading (1)
Operator Overloading:
• C++ allows you to redefine how standard operators work when used
with class objects.
• In C++, we can change the way operators work for user-defined types
like objects and structures.
This is known as operator overloading
How to use an operator to manipulate class objects ?
For example:
• assume that a class named Date exists,
• and objects of the Date class hold the month, day, and year in member variables.
Suppose the Date class has a member function named add.
• The add member function adds a number of days to the date and adjusts the
member variables if the date goes to another month or year.
today.add(5); // adds five days to the date stored in the today object:
today += 5;
class Count {
private:
int value;
public:
Count() {
value=5; }
{ ++value; }
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Notice that we have created an object temp and returned its value to the operator
function. Also, notice the code temp.value = ++value;
The variable value belongs to the count1 object in main() because count1 is calling the
function, while temp.value belongs to the temp object.
int main() {
Count count1, result;
// Call the "Count operator ++ ()" function
result = ++count1;
result.display();
return 0;
}
Overloading the unary minus (-) operator.
#include <iostream>
using namespace std;
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
Distance() {
feet = 0;
inches = 0;}
Distance(int f, int i) {
feet = f;
inches = i; }
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
// overloaded minus (-) operator
Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};
int main() {
Distance D1(11, 10), D2(-5, 11);
return 0;
}
Ovrerloading Logical NOT ( ! ):
// Overloading ! Operator
if (!a1) {
cout << "a1 value is zero" << endl;
}
else {
cout << "a1 value is non-zero" << endl;
}
if (!a2) {
cout << "a2 value is zero" << endl;
}
else {
cout << "a2 value is non-zero" << endl;
}
}
C++ Binary Operator Overloading :
// C++ program to overload the binary operator +
// This program adds two complex numbers
class Complex {
private: float real; float imag;
public:
// Constructor to initialize real and imag to 0
Complex() {
real=0,
imag=0; }
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
// Overload the + operator
Complex operator + (const Complex &obj)
{
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
void output() {
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};
int main() {
Complex complex1, complex2, result;
return 0;
}
Overload the binary operator +
return false;
}
// method to display distance
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
};
int main() {
Distance D1(11, 10), D2(5, 11);
if( D1 < D2 ) {
cout << "D1 is less than D2 " << endl;
} else {
cout << "D2 is less than D1 " << endl;
}
return 0;
}
Overloading the << and >> Operators :
• The stream insertion and stream extraction operators also can be
overloaded to perform input and output for user-defined types like an
object.
• Overloading these operators is done in a slightly different way, than
overloading other operators.
• cout is an object of ostream class and cin is an object of istream class
• These operators must be overloaded as a global function. And if we want to
allow them to access private data members of the class, we must make
them friend.
class Distance {
private:
int feet; int inches;
public:
Distance() {
feet = 0; inches = 0; }
Distance(int f, int i) {
feet = f; inches = i; }
friend ostream & operator<<( ostream &, const Distance & ) ;
friend istream &operator >> ( istream &, Distance & ) ; };
ostream &operator<<( ostream &output, const Distance &D )
{ output << "F : " << D.feet << " I : " << D.inches;
return output;
}
istream &operator>>( istream &input, Distance &D ) {
input >> D.feet >> D.inches;
return input;
}
int main() {
Distance D1(11, 10), D2(5, 11), D3;
return 0;
}
class Complex
{ private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c); };
ostream & operator << (ostream &out, const Complex &c) {
out << c.real;
out << "+i" << c.imag << endl;
return out; }
istream & operator >> (istream &in, Complex &c) {
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imaginary Part ";
in >> c.imag;
return in; }
int main()
{
Complex c1;
cin >> c1;
cout << "The complex object is ";
cout << c1;
return 0;
}
Overloading the Subscript Operator
[]
The subscript operator is most frequently overloaded when a class has
an array as one of its data members.
class IntArray {
private:
int m_Array[100];
int m_length;
public:
IntArray(int length) {
if (length > 100)
m_length = length;
else
m_length = length;
for (int i = 0; i < m_length; i++)
m_Array[i] = i; }
void PrintArray() {
for (int i=0; i < m_length; i++)
cout << m_Array[i] << ", " ;
cout << endl << endl;
}
//overloaded indexing operator []
return 0;
}
const int SIZE = 10;
class safearay {
private:
int arr[SIZE];
public:
safearay() {
register int i;
for(i = 0; i < SIZE; i++) {
arr[i] = i; } }
int &operator[](int i) {
if( i > SIZE ) {
cout << "Index out of bounds" <<endl;
// return first element.
return arr[0]; }
return arr[i]; }
};
int main() {
safearay A;
return 0;
}
Things to Remember in C++ Operator Overloading :