1.运算符的重载
#include <stdio.h>
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(){
}
Point(int x,int y){
this->x = x;
this->y = y;
}
void setx(int x){
this->x = x;
}
void sety(int y){
this->y = y;
}
void printInfo(void){
cout<<"x = "<<x<<" y = "<<y<<endl;
}
friend Point add(Point &p1,Point &p2);
friend Point operator+(Point &p1,Point &p2);
};
Point add(Point &p1,Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1,Point &p2)
{
cout<<"operator+"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
int main()
{
Point p1(1,2);
Point p2(2,3);
Point sum = p1 + p2;
sum.printInfo();
system("pause");
return 0;
}
#include <stdio.h>
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(){
}
Point(int x,int y){
this->x = x;
this->y = y;
}
void setx(int x){
this->x = x;
}
void sety(int y){
this->y = y;
}
void printInfo(void){
cout<<"x = "<<x<<" y = "<<y<<endl;
}
friend Point add(Point &p1,Point &p2);
friend Point operator+(Point &p1,Point &p2);
friend Point operator++(Point &p);
friend Point operator++(Point &p,int a);
};
Point add(Point &p1,Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1,Point &p2)
{
cout<<"operator+"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator++(Point &p)
{
cout<<"operator++(Point &p)"<<endl;
p.x += 1;
p.y += 1;
return p;
}
Point operator++(Point &p,int a)
{
cout<<"operator++(Point &p,int a)"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
int main()
{
Point p1(1,2);
Point p2(2,3);
Point n = ++p1;
n.printInfo();
p1.printInfo();
cout<<"***********************"<<endl;
Point m = p2++;
m.printInfo();
p2.printInfo();
system("pause");
return 0;
}
- 返回引用
前++ 可以直接返回引用,避免没必要的重新构造新类
后++ 不可以发回引用,因为后++中定义了局部类,用完就释放了
值返回效率比较低,引用返回效率高
#include <stdio.h>
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(){
cout<<"Point()"<<endl;
}
Point(int x,int y){
cout<<"Point(int x,int y)"<<endl;
this->x = x;
this->y = y;
}
Point(const Point& p){
cout<<"Point(const Point& p)"<<endl;
this->x = p.x;
this->y = p.y;
}
~Point(){
cout<<"~Point()"<<endl;
}
void setx(int x){
this->x = x;
}
void sety(int y){
this->y = y;
}
void printInfo(void){
cout<<"x = "<<x<<" y = "<<y<<endl;
}
friend Point add(Point &p1,Point &p2);
friend Point operator+(Point &p1,Point &p2);
friend Point& operator++(Point &p);
friend Point operator++(Point &p,int a);
};
Point add(Point &p1,Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1,Point &p2)
{
cout<<"operator+"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point& operator++(Point &p)
{
cout<<"operator++(Point &p)"<<endl;
p.x += 1;
p.y += 1;
return p;
}
Point operator++(Point &p,int a)
{
cout<<"operator++(Point &p,int a)"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
int main()
{
Point p1(1,2);
cout<<"begin"<<endl;
++p1;
cout<<"***********************"<<endl;
p1++;
cout<<"end"<<endl;
system("pause");
return 0;
}
#include <stdio.h>
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(){
cout<<"Point()"<<endl;
}
Point(int x,int y){
cout<<"Point(int x,int y)"<<endl;
this->x = x;
this->y = y;
}
Point(const Point& p){
cout<<"Point(const Point& p)"<<endl;
this->x = p.x;
this->y = p.y;
}
~Point(){
cout<<"~Point()"<<endl;
}
void setx(int x){
this->x = x;
}
void sety(int y){
this->y = y;
}
void printInfo(void){
cout<<"x = "<<x<<" y = "<<y<<endl;
}
friend Point add(Point &p1,Point &p2);
friend Point operator+(Point &p1,Point &p2);
friend Point& operator++(Point &p);
friend Point operator++(Point &p,int a);
friend ostream& operator<<(ostream &o,Point p);
};
Point add(Point &p1,Point &p2)
{
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point operator+(Point &p1,Point &p2)
{
cout<<"operator+"<<endl;
Point n;
n.x = p1.x+p2.x;
n.y = p1.y+p2.y;
return n;
}
Point& operator++(Point &p)
{
cout<<"operator++(Point &p)"<<endl;
p.x += 1;
p.y += 1;
return p;
}
Point operator++(Point &p,int a)
{
cout<<"operator++(Point &p,int a)"<<endl;
Point n;
n = p;
p.x += 1;
p.y += 1;
return n;
}
ostream& operator<<(ostream &o,Point p)
{
cout<<"("<<p.x<<", "<<p.y<<")";
return o;
}
int main()
{
Point p1(1,2);
Point m,n;
cout<<"begin"<<endl;
m = ++p1;
cout<<"***********************"<<endl;
n = p1++;
cout<<"end"<<endl;
cout<<m<<" "<<n<<endl;
system("pause");
return 0;
}