C++对象的++,--运算符重载 374-39
2006-12-06 21:26
330 查看
#include <iostream.h>
class CPoint
{
public:
CPoint(int i=0,int j=0)
{
x=i;
y=j;
}
CPoint operator++();
CPoint operator++(int);
CPoint operator--();
CPoint operator--(int);
void print()
{
cout<<"x="<<x<<" y="<<y<<endl;
}
private:
int x,y;
};
CPoint CPoint::operator++()
{
x++;
y++;
return *this;
}
CPoint CPoint::operator ++(int)
{
CPoint a;
a.x =x;
a.y=y;
x++;
y++;
return a;
}
CPoint CPoint::operator--()
{
x--;
y--;
return *this;
}
CPoint CPoint::operator --(int)
{
CPoint a;
a.x=x;
a.y=y;
x--;
y--;
return a;
}
void main()
{
CPoint a;
CPoint b;
cout<<"CPoint a; /na.print() ";
a.print();
cout<<endl;
b=a++;
cout<<"CPoint b;"<<endl;
cout<<"b=a++; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;
b=++a;
cout<<"b=++a; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;
b=a--;
cout<<"b=a--; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;
b=--a;
cout<<"b=--a; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
}
class CPoint
{
public:
CPoint(int i=0,int j=0)
{
x=i;
y=j;
}
CPoint operator++();
CPoint operator++(int);
CPoint operator--();
CPoint operator--(int);
void print()
{
cout<<"x="<<x<<" y="<<y<<endl;
}
private:
int x,y;
};
CPoint CPoint::operator++()
{
x++;
y++;
return *this;
}
CPoint CPoint::operator ++(int)
{
CPoint a;
a.x =x;
a.y=y;
x++;
y++;
return a;
}
CPoint CPoint::operator--()
{
x--;
y--;
return *this;
}
CPoint CPoint::operator --(int)
{
CPoint a;
a.x=x;
a.y=y;
x--;
y--;
return a;
}
void main()
{
CPoint a;
CPoint b;
cout<<"CPoint a; /na.print() ";
a.print();
cout<<endl;
b=a++;
cout<<"CPoint b;"<<endl;
cout<<"b=a++; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;
b=++a;
cout<<"b=++a; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;
b=a--;
cout<<"b=a--; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
cout<<endl;
b=--a;
cout<<"b=--a; b.print() ";
b.print();
cout<<" a.print() ";
a.print();
}
相关文章推荐
- 【2016/3】C++ 类与对象进阶 运算符重载 new delete 模板 继承
- C++基础(六)在对象中使用运算符——运算符重载
- C++对象初始化以及运算符重载
- C++构造,析构,友元类,对象,static成员,复制构造函数,运算符重载杂谈
- C/C++注册动态对象到Lu系统并进行运算符重载
- C/C++注册动态对象到Lu系统并进行运算符重载
- C/C++对Lu系统内置动态对象进行运算符重载
- C++找对象的季节(补)——运算符重载一二事
- C++ 运算符重载时,将运算符两边对象交换问题.
- C/C++对Lu系统内置动态对象进行运算符重载
- C++技术点积累(3)——对象初始化列表、运算符重载
- 【c++】类和对象--四个默认成员函数+运算符重载
- C++第5周任务【任务2】本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,二则也为第10章做运算符重载等积累些感性认识。
- 【C++】类和对象--基础知识+四个默认成员函数+运算符重载
- C++ 运算符重载之把类对象当着函数名使用
- 你好,C++(33)对象生死两茫茫 6.2.3 一个对象的生与死:构造函数和析构函数
- C++中的对象指针
- C++对象对内存的使用
- C++ 通过cvs格式文件构建Eigen的Matrix对象
- C++学习笔记三:类和对象