【问题标题】:Calculate car movement and distance with C++ classes使用 C++ 类计算汽车运动和距离
【发布时间】:2017-09-11 08:33:57
【问题描述】:

这个程序编译不过,我需要让这个函数在 x & y 坐标上移动,然后输出总行驶距离。 xCord 左右移动它,而 yCord 上下移动它。我想我需要更新我的 int Taxicab::getDistanceTraveled()、void Taxicab::moveX(int getX) 和 void Taxicab::moveX(int getX)。但是对于我的生活来说,我无法弄清楚该怎么做才能让它正确更新。当我编译并运行时,它给我 132617596 表示 cab1 的行驶距离和 0 表示 cab2 上的 Y 坐标。谢谢您的帮助!

#ifndef TAXI_CPP
#define TAXI_CPP

class Taxicab
{
private:
    int xCord;
    int yCord;
    int totalDist;
public:
    Taxicab(); //default constructor
    Taxicab(int, int); //overload constructor
    int getX(); //returns X coordinate
    int getY(); //returns Y coordinate
    int getDistanceTraveled(); //returns distance calculation
    void moveX(int); //moves X left or right
    void moveY(int); //moves Y left or right 
};
#endif // !TAXI_CPP




#include "Taxi.h"
#include <iostream> 
#include <math.h>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;
using std::abs;

Taxicab::Taxicab() //default constructor
{

}

Taxicab::Taxicab(int xCord, int yCord) //overload constructor
{
    xCord = 0; //initialize to 0
    yCord = 0; //initialize to 0
    totalDist = 0; //initialize to 0
}

int Taxicab::getX()
{
    return xCord; //return x coordinate
}

int Taxicab::getY()
{
    return yCord; //return y coordinate
}

void Taxicab::moveX(int getX)
{
    int moveX = 0;
    moveX = moveX + getX;
}

void Taxicab::moveY(int getY)
{
    int moveY = 0;
    moveY = moveY + getY;
}

int Taxicab::getDistanceTraveled()
{
    return abs(xCord) + abs(yCord);
}


#include <iostream>
#include "Taxi.h"
#include <math.h>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    Taxicab cab1;
    Taxicab cab2(5, -8);
    cab1.moveX(3);
    cab1.moveY(-4);
    cab1.moveX(-1);
    cout << cab1.getDistanceTraveled() << endl;
    cab2.moveY(7);
    cout << cab2.getY() << endl;
}

【问题讨论】:

  • 调试器。使用调试器。调试器允许您一次执行一个语句并观察变量的值。请使用调试会话的结果编辑您的帖子。

标签: c++


【解决方案1】:

您的构造函数没有意义。 在默认构造函数中,您必须将成员变量初始化为某些东西,否则它们的值未定义并且可以设置为某个随机值。试试这些吧:

Taxicab::Taxicab() //default constructor
{
    xCord = 0; //initialize to 0
    yCord = 0; //initialize to 0
    totalDist = 0; //initialize to 0
}

Taxicab::Taxicab(int xCord, int yCord) //overload constructor
{
    this->xCord = xCord;
    this->yCord = yCord;
    totalDist = 0; //initialize to 0
}

移动出租车的方法也没有多大意义。也许这样的东西会更好:

void Taxicab::moveX(int offsetX)
{
    totalDist += abs(offsetX);
    xCoord += offsetX;
}

void Taxicab::moveY(int offsetY)
{
    totalDist += abs(offsetY);
    yCoord += offsetY;
}

int Taxicab::getDistanceTraveled()
{
    return totalDist;
}

【讨论】:

  • 稍微编辑了您的答案。没有moveX / moveY变量,它是类中的xCoord,yCoord,它应该只是abs(getX),因为它是相对运动。
  • @JasonLang 从问题中有点不清楚距离应该是多少:如果您将汽车移动 3 次到moveX(1),行驶的距离应该是 3 还是 1?我认为它应该是 1,因为第 2 次和第 3 次调用不会移动它,因为它已经在 xCord=1
  • 啊,如果它的偏移量不是实际的位置那么是的,这是有道理的。
  • 请注意:我们可以收集到他的意思是相对运动,因为他的 moveX 函数最初的结构是 x = x + dx,变量名称不正确,但意图是 添加参数到x
猜你喜欢
  • 2017-07-29
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多