2014-02-23 163 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; 
}