使用类/构造函数的指导

时间:2014-02-23 02:06:55

标签: c++ class constructor

我正在编写一个程序,要求用户提供两个向量(具有力和幅度),然后返回两个向量的总和。我并不是真的在找人给我代码,但我真的需要一些关于如何继续的指导。我觉得我真的不理解类/构造函数的实现,所以我很确定我做错了,或者至少效率低下。注意:我希望很明显我没有完成。我只是有一种“编码器块”: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)使用来自x的{​​{1}}和y值作为参数?我相信这对你们大多数人来说似乎是如此简陋;我讨厌我在课上遇到这么多麻烦......

提前致谢。

3 个答案:

答案 0 :(得分: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。)

答案 1 :(得分:0)

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

首先,我们需要确定该类执行其功能所需的最少成员数据。我觉得这样做太过刻意,只会让课程变得过于复杂。在物理学中,二维矢量可以用多种方式表示,但最常见的是笛卡尔形式(x,y)。因此,为了实现这一点,所需要的只是那两个变量。可以从这两个数字计算关于向量的所有其他信息。开始宣读课程:

class Physics_vector {

double x, y;

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

你似乎有了构造函数:它需要做的就是初始化这两个变量。但是,我们可以添加一些额外的功能:如果我们想要声明Physics_vector而不实际给它一个值呢?那么,我们可以编译构造函数,以便在构造时没有任何值时为xy提供一些合理的默认值:

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

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

y

... 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是必要的,因为x在setter成员函数的范围内被声明两次:一次在函数的参数中,一次在类成员变量中。编译器无法分辨哪个被引用,因此C ++标准定义了本地声明优先。因此,尝试编写this->x = x;会将函数的参数x = x;分配给自身。我注意到你在set_vector函数中偶然发现了这个(你将成员变量x分配给函数参数forcefm也是如此,我认为这不是你的意图。解决它的方法是使用magnitude指针(在成员函数,构造函数和析构函数中定义,并始终指向类的当前实例)。

接下来,我们将定义一个add函数,因为在类中添加两个this代码是合理的。该函数应该使用另一个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_vectorvec1,例如vec2。好吧,C ++提供了一种定义这种操作的方法。我们会这样编码:

Physics_vector result = vec1 + vec2;

我们还可以添加一些其他有用的函数,例如返回... 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标题(可能由您的导师为方便起见而创建)可能包括或不包含cmath。

鉴于此课程,您可以按照以下方式实施您的课程:

std_lib_facilities_4.h

希望能帮助你解决程序员的问题!

答案 2 :(得分:0)

看起来你应该提高对C ++类的理解。 Here是一个很好的起点。我会尝试给你一些指示。

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

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

  3. 要添加矢量,您应该使用运算符重载和 实现一个名为v3 = v1 + v2的方法,它可以让您轻松实现 只需输入类似m_variable的内容即可将两个向量相加。

  4. 这主要是风格问题而且没有必要,但我会 建议你为你的成员变量添加一些前缀,所以 它们很容易与代码中的局部变量区分开来。一个 流行的惯例是使用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; } 。同时列出您的公众 成员第一,一般使类接口稍微容易 读取。

  5. 考虑到以上所有因素,您的矢量界面可能如下所示(我将把实现留给您):

    {{1}}
相关问题