6. 下面是一个类声明:
(……代码省略……)
请提供成员函数的定义和测试这个类的程序。
// stack.h -- struct and class definition
#ifndef STACK_H_
#define STACK_H_
class Move
{
private:
double x;
double y;
public:
Move(double a = 0,double b = 0);
~Move();
void showmove()const;
Move add(const Move& m) const;
void reset(double a = 0, double b = 0);
};
#endif
// stack.cpp -- class member functions' definition
#include "stack.h"
#include <iostream>
Move::Move(double a , double b ) {
x = a;
y = b;
}
Move::~Move() {
}
void Move::showmove()const {
std::cout << "x= " << x << std::endl;
std::cout << "y= " << y << std::endl;
}
Move Move::add(const Move& m) const {
Move* pl = new Move;
pl->x = this->x + m.x;
pl->y = this->y + m.y;
return *pl;
}
void Move::reset(double a , double b ) {
x = a;
y = b;
}
#include "stack.h"
#include <iostream>
using namespace std;
int main()
{
Move m1;
m1 = Move{ 100,200 };
m1.showmove();
m1.reset(300, 100);
m1.showmove();
Move m2{ 1,1 };
Move m3 = m1.add(m2);
cout << "m3:\n";
m3.showmove();
cout << "m2:\n";
m2.showmove();
cout << "m1:\n";
m1.showmove();
delete[]&m3;//?
return 0;
}