6.下面是一个类声明:
class Move
{
private:
double x;
double y;
public:
Move(doublea = 0,doubleb = 0);//sets x,yto a,b
showmove()const;
//shows currentx,yvalues
Move add(const Move& m)const;
//this function adds xof m to x of invoking object to get new x
// adds yof m to yof invoking object to get new y,creates a new
//move object initialized to new x,y values and returns it
reset(double a = 0, doubleb = 0);//resets x,y to a,b
};
class Move
{
private:
double x;
double y;
public:
Move(double a = 0,double b = 0);
void showmove()const;
Move add(const Move& m)const;
void reset(double a = 0, double b = 0);
};
#include<iostream>
#include"pe10-6.h"
Move::Move(double a, double b){
this->x = a;
this->y = b;
}
void
Move::showmove()const{
std::cout << "x,y = " << this->x<<", " << this->y;
}
Move Move::add(const Move& m)const{
return Move((x+m.x),(y+m.y));
}
void
Move::reset(double a, double b){
this->x = a, this->y = b;
}
#include <iostream>
#include"pe10-6.h"
/*
*/
using std::cout;
using std::endl;
int main()
{
//using namespace std;
Move move1(3,4);
move1.showmove();
cout << endl;
Move move2(12, 13);
move2.showmove();
cout << endl;
move1.add(move2).showmove();
cout << endl;
move1.reset();
move1.showmove();
cout << endl;
return 0;
}
打印输出的结果,在函数add中一定要给上数据类型,不然会转换失败
Move Move::add(const Move& m)const{
return Move((x+m.x),(y+m.y));
}

1020

被折叠的 条评论
为什么被折叠?



