![]() |
#2
rjsp2021-02-04 16:52
|

class Move{
private:
double x;
double y;
public:
Move(double a=0,double b=0);//set x,y to a,b
showmove() const; //shows current x,y values
Move add(const Move &m)const;
//this function adds x of m to x of invoking object to get new x,
//adds y of m to y of invoking object to get new y,creates a new move object initialized to new x,y values and returns it.
reset(double a=0,double b=0); //resets x,y to a,b
};
private:
double x;
double y;
public:
Move(double a=0,double b=0);//set x,y to a,b
showmove() const; //shows current x,y values
Move add(const Move &m)const;
//this function adds x of m to x of invoking object to get new x,
//adds y of m to y of invoking object to get new y,creates a new move object initialized to new x,y values and returns it.
reset(double a=0,double b=0); //resets x,y to a,b
};
请提供成员函数的定义和测试这个类的程序。
以上就是题目。
代码如下:

#include<iostream>
using namespace std;
class Move{
private:
double x;
double y;
public:
Move(double a=0,double b=0);
showmove() const;
Move add(const Move &m)const;
reset(double a=0,double b=0);
};
Move::Move(double a,double b){
x=a;
y=b;
}
Move::showmove()const{
cout<<"x值:"<<x<<endl;
cout<<"y值:"<<y<<endl;
}
Move Move::add(const Move &m)const{
Move invoking;
invoking.x=m.x;
invoking.y=m.y;
Move aaa(x,y);
return (x,y);
}
Move::reset(double a,double b){
x=a;
y=b;
}
int main(){
Move bbb;
bbb.showmove();
double x,y;
cout<<"输入x值:";
cin>>x;
cout<<"输入y值:";
cin>>y;
Move ccc(x,y);
ccc.add(ccc);
ccc.reset(x,y);
ccc.showmove();
return 0;
}
using namespace std;
class Move{
private:
double x;
double y;
public:
Move(double a=0,double b=0);
showmove() const;
Move add(const Move &m)const;
reset(double a=0,double b=0);
};
Move::Move(double a,double b){
x=a;
y=b;
}
Move::showmove()const{
cout<<"x值:"<<x<<endl;
cout<<"y值:"<<y<<endl;
}
Move Move::add(const Move &m)const{
Move invoking;
invoking.x=m.x;
invoking.y=m.y;
Move aaa(x,y);
return (x,y);
}
Move::reset(double a,double b){
x=a;
y=b;
}
int main(){
Move bbb;
bbb.showmove();
double x,y;
cout<<"输入x值:";
cin>>x;
cout<<"输入y值:";
cin>>y;
Move ccc(x,y);
ccc.add(ccc);
ccc.reset(x,y);
ccc.showmove();
return 0;
}