2014-02-23 112 views
0

我在写一个程序,询问用户两个向量(带有力和大小),然后返回两个向量的和。我并不是真的在找人给我一些代码,但我真的需要一些关于如何继续的指导。我觉得我真的不明白类/构造函数的实现,所以我很确定我正在做的事情不正确,或者至少效率低下。注意:我希望这很明显,我没有完成。我只是有样的“编码器的块”的:P使用类/构造函数的指导

#include "std_lib_facilities_4.h" 

class Physics_vector { 
    double force, magnitude, x, y, f, m; 
    vector<double> final; 
    vector<double> v; 

    public: 
    Physics_vector(double x, double y) :force(x), magnitude(y) {}; 
    void set_vector(double f, double m); 
    int get_vector(vector<double> final); 
    double add_physics_vector(); 
}; 

void Physics_vector::set_vector(double f, double m) 
{ 
    f = force; 
    m = magnitude; 
    vector<double> final; 
    final.push_back(f); 
    final.push_back(m); 
} 

int Physics_vector::get_vector(vector<double> final) 
{ 
    for (int i = 0; i < 2; ++i) { 
     cout << final[i] << '\n'; 
    } 
    return 0; 
} 


int main() 
{ 
    cout << "Howdy!" << '\n'; 
    cout << "This program adds together two vectors." 
     << endl; 
    cout << "First, enter in the force and magnitude of your first vector." 
     << "\nExample: 4 7." << endl; 

    double user_force, user_magnitude, force, magnitude; 
    cin >> user_force >> user_magnitude; 
    Physics_vector first(user_force, user_magnitude); 
    first.set_vector(force, magnitude); 

    cout << "Next, enter in the force and magnitude of your second vector." 
     << endl; 
    cin >> user_force >> user_magnitude; 
    Physics_vector second(user_force, user_magnitude); 
} 

编辑:好吧,让我改变了我的代码一点,使它更清洁(如果它不告诉我)。但现在我的问题是函数调用。

class Physics_vector { 
public: 
    Physics_vector(double x = 0, double y = 0) :x(x), y(y) {} 
    double get_vector(double x, double y); 
private: 
    double x, y; 
}; 

double Physics_vector::get_vector(double x, double y) 
{ 
    return x; 
    return y; 
} 

double add_physics_vector(vector<double> vect_1, vector<double> vect_2) 
{ 
    return 0.0; 
} 

int main() 
{ 
    cout << "Howdy! Please enter your first vector (direction and magnitude) ." 
     << "\nExample: 1 2." << endl; 
    double user_direction = 0; 
    double user_magnitude = 0; 
    cin >> user_direction >> user_magnitude; 
    Physics_vector(user_direction, user_magnitude); 
    //get_vector(...aaaand I'm stuck... 
} 

如何获得get_vector(double x, double y)使用xy值从Physics_vector(),因为它的参数呢?我相信这对你们中的大多数人来说似乎是非常基本的。我讨厌我在课上遇到这么多麻烦...

在此先感谢。

回答

0

如果你正在得到你想要的效果,你做的事情很好。不过,我不确定情况是否如此。据我所知,vector in physics是一个有大小和方向的身体。

如果你想设计一个简单Physics_vector类,我可能会坚持使用:

class Physics_vector{ 
    double x, y; 

public: 
    Physics_vector(double x, double y); 
}; 

或者,如果你想在N维空间进行操作:

class Physics_vector{ 
    unsigned int dimensions; // Optional, same as magnitudes.size(); 
    vector<double> magnitudes; 

public: 
    Physics_vector(unsigned int dimensions, ...); 
} 

或者,在最后一种情况下 - 只需使用templates(如果您使用C++ 11操作,则可能使用variadic)。

+0

你可能不应该离开构造私有。 – jaho

+0

@Marian谢谢你指出。使它成为'public'可能会帮助:) –

0

通常,类用于表示单个数据单元 - 在您的情况下,是一个矢量(物理意义上的)。类本身理想地应该只包含它需要的成员变量来保存它所代表的数据和(只有在绝对必要时)其成员函数需要工作的任何其他数据段。看起来你正试图实现一个有两个维度的向量,所以我们将从那里开始。

首先,我们需要确定类需要执行其功能所需的最小成员数据。我发现对这个想法太过分了,只会让班级过于复杂。在物理学中,二维矢量可以用多种方式表示,但最常用的是笛卡尔形式(x,y)。所以,为了实现这一切,需要的是这两个变量。所有关于矢量的其他信息都可以从这两个数字中计算出来。开始这个类的声明:

class Physics_vector { 

double x, y; 

public: 
// Constructors and member functions here... 
... 

你似乎有构造函数down:它所需要做的就是初始化这两个变量。但是,我们可以添加一些附加功能:如果我们想要声明Physics_vector而未真正给它赋值,该怎么办?好了,那么我们就可以编写构造给予xy一些合理的默认值时,它没有任何价值构造:

... 
explicit Physics_vector(double x = 0, double y = 0):x(x), y(y) {} 
... 

explicit关键字是确保构造函数必须显式调用,因为在C++中可能带有一个参数的构造函数还定义了从该参数到类的类型的隐式转换,并且Physics_vector vec = 0;不是一种明智的转换。接下来,我们需要一些方法来访问x和从类的外部y,所以我们会让访问两个值的一些getter和setter函数:

... 
double get_x() const { 
    return x; 
} 

double get_y() const { 
    return y; 
} 

void set_x(double x) { 
    this->x = x; 
} 

void set_y(double y) { 
    this->y = y; 
} 
... 

const关键字想象成你的承诺向编译器提出成员函数不会修改任何成员变量。你可以删除它,而班级仍然可以工作,但通常最好在有意义时加入。

语法this->x = x;是必要的,因为x在setter成员函数的范围内声明了两次:一次在函数的参数中,一次在类成员变量中。编译器无法判断哪个被引用,因此C++标准定义了本地声明优先。因此,试图编写x = x;将自动分配函数的参数x。我注意到你在set_vector函数中偶然发生了这种情况(您将成员变量force分配给了函数参数f,而mmagnitude也是这样,我不认为这就是您的意图)。解决这个问题的方法是使用this指针(它在成员函数,构造函数和析构函数中定义,并且始终指向类的当前实例)。

接下来,我们将定义一个add函数,因为在代码中添加两个Physics_vector是合理的。该函数应该采用另一个Physics_vector并将其添加到*this,并返回结果。

... 
Physics_vector add(Physics_vector other) const { 
    return Physics_vector(x + other.x, y + other.y); 
} 
... 

虽然这接下来的部分是更先进的(并且可能不是在你的类被覆盖还),我会把它放在那里呢。如果我们想添加两个Physics_vector就像我们两个double s?换句话说,添加Physics_vector s vec1vec2,如Physics_vector result = vec1 + vec2;。那么,C++提供了一种定义这种操作的方法。我们将这样的代码是:

... 
Physics_vector operator + (Physics_vector other) const { 
    return Physics_vector(x + other.x, y + other.y); 
} 
... 

我们还可以添加一些其他有用的功能,如返回Physics_vector的大小的功能。

... 
double magnitude() const { 
    return sqrt(x*x + y*y); 
} 
}; // End of the the declaration/definition of Physics_vector. 

sqrt功能在头cmath定义,并计算其自变量的平方根。您所包含的神奇std_lib_facilities_4.h标题(可能由您的教练为了您的方便而创建)可能包含或不包含您的cmath。

鉴于这种类,那么你可以实现你的程序是这样的:

int main() 
{ 
    double x1, y1, x2, y2; 
    cout << "Please give two vectors to add.\n"; 
    cout << "x1: "; 
    cin >> x1; 
    cout << "\ny1: "; 
    cin >> y1; 
    cout << "\nx2: "; 
    cin >> x2; 
    cout << "\ny2: "; 
    cin >> y2; 

    Physics_vector vec1(x1, y1); 
    Physics_vector vec2(x2, y2); 

    Physics_vector result = vec1.add(vec2); // Or vec1 + vec2, if you use the operator + overload I described. 

    cout << "\n\nvec1 + vec2 = (" 
     << result.get_x() << ", " 
     << result.get_y() << ")"; 
} 

希望帮助你和你的程序员的块!

0

看起来你可能应该提高你对C++类的理解。 Here是一个很好的开始。尽管如此,我会尝试给你一些指示。

  1. 假设你的向量类只需要你有 方式两个值存储许多成员变量。你实际上只需要两个双打: 的力量和幅度(你的意思是幅度和方向?)。 vector类型的 变量是不必要的,因为尽管它的名称是 std::vector只是一个动态数组。

  2. 因为你已经 在那里传递 力和幅度值设置在构造函数中的向量中的值不需要单独set_vector()方法。在另一方面,你可能想 为成员变量添加访问器get_force()get_magnitude()以及可能set_force()set_magnitude(),如果你认为你可能需要从外部改变这些值 。

  3. 对于添加你应该使用操作符重载和 实施一个名为operator+方法可以让你可以轻松地 由单纯的喜欢v3 = v1 + v2输入东西两个向量在一起的载体。

  4. 这主要是风格和它没有必要的事,但我 建议你到某种前缀添加到您的成员变量,所以 他们是从代码的局部变量容易辨别。 A 流行的约定是使用m_variable。同时首先列出您的公众 成员,通常使得类接口稍微更容易被 阅读。

考虑到上述所有的矢量界面看起来是这样的(我会离开的实施给你):

class PhysicsVector 
{ 
public: 
    PhysicsVector(double force, double magnitude) : m_force(force), m_magnitude(magnitude) 
    { 
    } 

    double getForce() const; 
    double getMagnitude() const; 
    void setForce(double force); 
    void setMagnitude(double magnitude); 

    PhysicsVector operator+(PhysicsVector other) const; 

private: 
    double m_force, m_magnitude; 
}