一,实例
operator+
#include"pch.h"
#include<iostream>
using namespace std;
//+运算法重载
class Box {
public:
double getVolume(void) {
return length * breadth*height;
}
void setLength(double len) {
length = len;
}
void setBreadth(double bre) {
breadth = bre;
}
void setHeight(double hei) {
height = hei;
}
//重载+运算符,用于把两个Box对象相加
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length;
double breadth;
double height;
};
int main()
{
Box box1;
Box box2;
Box box3;
double volume = 0.0;
//Box1 详述
box1.setLength(10);
box1.setBreadth(10);
box1.setHeight(10);
//Box2 详述
box2.setLength(100);
box2.setBreadth(10);
box2.setHeight(10);
volume = box1.getVolume();
cout << "Volume of box1: " << volume << endl;
volume = box2.getVolume();
cout << "Volume of box2: " << volume << endl;
box3 = box1 + box2;
volume = box3.getVolume();
cout << "Volume of box3: " << volume << endl;
return 0;
}
operator-
#include"pch.h"
#include<iostream>
using namespace std;
class Distance {
private:
int feet;
int inches;
public:
Distance() {
feet = 0;
inches = 0;
}
Distance(int f,int i) {
feet = f;
inches = i;
}
//显示距离
void displayDistance() {
cout << "F: " << feet << " I: " << inches << endl;
}
//重载负运算符(-)
Distance operator - () {
feet = -feet;
inches = -inches;
return Distance(feet,inches);
}
};
int main()
{
Distance D1(11,10), D2(-5,11);
-D1;
D1.displayDistance();
-D2;
D2.displayDistance();
return 0;
}
#include"pch.h"
#include<iostream>
using namespace std;
class Distance {
private:
int feet;
int inches;
public:
Distance() {
feet = 0;
inches = 0;
}
Distance(int f,int i) {
feet = f;
inches = i;
}
//显示距离
void displayDistance() {
cout << "F: " << feet << " I: " << inches << endl;
}
//重载负运算符(-)
Distance operator - () {
feet = -feet;
inches = -inches;
return Distance(feet,inches);
}
//重载小于运算符(<)
bool operator <(const Distance & d) {
if (feet<d.feet) {
return true;
}
if (feet==d.feet&& inches<d.inches) {
return true;
}
return false;
}
};
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;
}
-D1;
D1.displayDistance();
-D2;
D2.displayDistance();
return 0;
}
operator<<(>>)
#include"pch.h"
#include <iostream>
using namespace std;
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 &output,const Distance& D)
{
output << "F : " << D.feet << " I : " << D.inches;
return output;
}
friend istream &operator >> (istream &input,Distance &D) {
input >> D.feet >> D.inches;
return input;
}
};
int main()
{
Distance D1(11,10), D2(5,11), D3;
cout << "ENTER the value of object : " << endl;
cin >> D3;
cout << "First Distance : " << D1 << endl;
cout << "Second Distance : " << D2 << endl;
cout << "Third Distance : " << D3 << endl;
}
重载++
#include"pch.h"
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
public:
Time() {
hours = 0;
minutes = 0;
}
Time(int h,int m) {
hours = h;
minutes = m;
}
//显示时间方法
void display()
{
cout << "H : " << hours << " M : " << minutes << endl;
}
//重载前缀递增运算符(++)
Time operator++()
{
++minutes;
if (minutes>=60) {
++hours;
minutes -= 60;
}
return Time(hours,minutes);
}
//重载后缀递增运算符(++)
Time operator++(int)//int表示后缀
{
Time T(hours, minutes);
++minutes;
if (minutes >= 60) {
++hours;
minutes -= 60;
}
return T;
}
};
int main()
{
Time T1(11, 59), T2(10,40);
++T1;
T1.display();
++T1;
T1.display();
T2++;
T2.display();
T2++;
T2.display();
return 0;
}