问题:
解答:
main.cpp
#include <iostream>
#include "Move.h"
using namespace std;
int main()
{
Move m(3.3, 2.2);
m.showmove();
Move m1(1.2, 2.4);
m.add(m1).showmove();
m.reset(1, 1);
m.showmove();
return 0;
}
Move.h
#pragma once
class Move
{
private:
double x;
double y;
public:
Move(double a = 0, double b = 0);
void showmove()const;
Move add(const Move& m);
void reset(double a = 0, double b = 0);
};
Move.cpp
#include "Move.h"
#include <iostream>
using namespace std;
Move::Move(double a, double b)
{
this->x = a;
this->y = b;
}
void Move::showmove()const
{
cout << "x:" << this->x << endl;
cout << "y:" << this->y << endl;
}
Move Move::add(const Move& m)
{
double value1 = 0;
double value2 = 0;
value1 = this->x + m.x;
value2 = this->y + m.y;
Move move(value1, value2);
return move;
}
void Move::reset(double a , double b)
{
this->x = a;
this->y = b;
}
运行结果:
考查点:
- 类和对象
2024年9月4日16:14:19