C++-练习-68

题目:

下面是一个类声明

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 add(const Move & m) const:该函数将m的x添加到调用对象的x以获得新的x,将m的y添加到调用的对象的y以获得新y,创建一个初始化为新x,y值的新移动对象并返回I

reset(double a = 0,double b = 0):将x,y重置为a,b

源代码:

test.h

#include <iostream>

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);
};

test.cpp

#include "test.h"


int main()
{
	Move x;
	x.reset(1,2);
	x.showmove();
	x = x.add(x);
	x.showmove();
	return 0;
}

test_function.cpp

#include "test.h"

Move::Move(double a, double b)
{
	this->x = a;
	this->y = b;
}
void Move::showmove() const
{
	std::cout << "x: " << x << " y: " << y << std::endl;
}
Move Move::add(const Move& m) const
{
	Move tmp;
	tmp.x = m.x + x;
	tmp.y = m.y + y;
	return tmp;
}
void Move::reset(double a, double b)
{
	this->x = a;
	this->y = b;
}

演示效果:


如果朋友你感觉文章的内容对你有帮助,可以点赞关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈

猜你喜欢

转载自blog.csdn.net/little_startoo/article/details/143279757