|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
建立点类Point,包含两个成员变量x,y,分别表示横坐标和纵坐标。要求该类可以显示点的坐标值,重载前置运算符++、--;重载后置运算符++、--。
主函数需包含的内容:创建点对象,进行前置和后置运算并显示点值。
- #include<iostream>
- using namespace std;
- class AB
- {
- public:
- AB(int xx, int yy);
- void ShowAB();
- AB& operator ++();
- AB operator ++(int);
- AB& operator --();
- AB operator --(int);
- private:
- int x1,x2;
- };
- AB::AB(int xx, int yy)
- {
- x1=xx;
- x2=yy;
- }
- void AB::ShowAB()
- {
- cout<<x1<<" , "<<x2<<endl;
- }
- AB& AB::operator ++()
- {
- x1++;
- x2++;
- return *this;
- }
- AB AB::operator ++(int)
- {
- AB old=*this;
- ++(*this);
- return old;
- }
- AB& AB::operator --()
- {
- x1--;
- x2--;
- return *this;
- }
- AB AB::operator --(int)
- {
- AB old=*this;
- --(*this);
- return old;
- }
- int main(void)
- {
- AB AA(0,0);
- AB BB(0,0);
- cout<<"A的值为:";
- AA.ShowAB();
- cout<<"B的值为:";
- BB.ShowAB();
- cout<<"B=A++运算后,A的值为:";
- (++AA).ShowAB();
- cout<<" B的值为:";
- (BB++).ShowAB();
- cout<<"B=++A运算后,A的值为:";
- (++AA).ShowAB();
- cout<<" B的值为:";
- (++BB).ShowAB();
- cout<<"B=A--运算后,A的值为:";
- (--AA).ShowAB();
- cout<<" B的值为:";
- (BB--).ShowAB();
- cout<<"B=--A运算后,A的值为:";
- (--AA).ShowAB();
- cout<<" B的值为:";
- (--BB).ShowAB();
- return 0;
- }
复制代码
你把类名改一下就符合了~
|
|