我在写一个程序,询问用户两个向量(带有力和大小),然后返回两个向量的和。我并不是真的在找人给我一些代码,但我真的需要一些关于如何继续的指导。我觉得我真的不明白类/构造函数的实现,所以我很确定我正在做的事情不正确,或者至少效率低下。注意:我希望这很明显,我没有完成。我只是有样的“编码器的块”的: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
和y
值从Physics_vector()
,因为它的参数呢?我相信这对你们中的大多数人来说似乎是非常基本的。我讨厌我在课上遇到这么多麻烦...
在此先感谢。
你可能不应该离开构造私有。 – jaho
@Marian谢谢你指出。使它成为'public'可能会帮助:) –