运算符重载是面向对象编程中的一项功能,它允许程序员重新定义内置运算符以使用用户定义的数据类型。
为什么运算符超载?
假设我们已经定义了一个Integer类来处理整数操作。我们可以使用add(),subtract(),multipli()和divide()函数来处理相应的操作。但是,为了使代码更直观并增强可读性,最好使用与给定操作(分别为+,-,*,/)相对应的运算符,即,我们可以替换以下代码
例子:
Replace
i5 = divide(add(i1, i2), subtract(i3, i4))
by a simpler code:
i5 = (i1 + i2) / (i3 - i4)
重载增量运算符
前缀(++ i)和后缀(i ++)的运算符符都相同。因此,我们需要两个不同的函数定义来区分它们。这是通过在后缀版本中传递虚拟int参数来实现的。
这是演示相同代码的代码。
预增量超载
CPP
// C++ program to demonstrate
// prefix increment operator overloading
#include
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the prefix operator
Integer operator++()
{
Integer temp;
temp.i = ++i;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before increment: ";
i1.display();
// Using the pre-increment operator
Integer i2 = ++i1;
cout << "After pre increment: ";
i2.display();
}
CPP
// C++ program to demonstrate
// postfix increment operator overloading
#include
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator++(int)
{
Integer temp;
temp.i = i++;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before increment: ";
i1.display();
// Using the post-increment operator
Integer i2 = i1++;
cout << "After post increment: ";
i2.display();
}
CPP
// C++ program to demonstrate
// prefix decrement operator overloading
#include
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the prefix operator
Integer operator--()
{
Integer temp;
temp.i = --i;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before decrement: ";
i1.display();
// Using the pre-decrement operator
Integer i2 = --i1;
cout << "After pre decrement: ";
i2.display();
}
CPP
// C++ program to demonstrate
// postfix decrement operator overloading
#include
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator--(int)
{
Integer temp;
temp.i = i--;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before decrement: ";
i1.display();
// Using the post-decrement operator
Integer i2 = i1--;
cout << "After post decrement: ";
i2.display();
}
输出:
Before increment: i = 3
After pre increment: i = 4
后增量超载
CPP
// C++ program to demonstrate
// postfix increment operator overloading
#include
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator++(int)
{
Integer temp;
temp.i = i++;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before increment: ";
i1.display();
// Using the post-increment operator
Integer i2 = i1++;
cout << "After post increment: ";
i2.display();
}
输出:
Before increment: i = 3
After post increment: i = 3
重载减量运算符
同样,我们也可以按如下方式重载减量运算符
递减前超载
CPP
// C++ program to demonstrate
// prefix decrement operator overloading
#include
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the prefix operator
Integer operator--()
{
Integer temp;
temp.i = --i;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before decrement: ";
i1.display();
// Using the pre-decrement operator
Integer i2 = --i1;
cout << "After pre decrement: ";
i2.display();
}
输出:
Before decrement: i = 3
After pre decrement: i = 2
递减后重载
CPP
// C++ program to demonstrate
// postfix decrement operator overloading
#include
using namespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator--(int)
{
Integer temp;
temp.i = i--;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before decrement: ";
i1.display();
// Using the post-decrement operator
Integer i2 = i1--;
cout << "After post decrement: ";
i2.display();
}
输出:
Before decrement: i = 3
After post decrement: i = 3
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。