對于我最終的 OOP 專案,我必須在 C 中創建一個“流媒體服務”,我基本上完成了整個程式,我只有一個問題。我給每部電影評分,但是當我列印這個評分時,我只是得到一些巨大的亂數而不是實際評分。
這是我的電影類和我遇到問題的函式的定義。
class movie : public video{
public:
movie();
movie(int, std::string, int, std::string, int);
float getRating();
void setRating(int);
void showrating();
protected:
int rating;
};
movie::movie(){
rating = 0;
}
movie::movie(int _id, std::string _name, int _length, std::string
_genre, int _rating) : video(_id, _name, _length, _genre){}
void movie::showrating(){
std::cout << name <<" has been rated " << rating << " stars out of 5." << std::endl;
}
這是我在 main.cpp 中使用它的方式
movie LordOfTheRings(0, "Lord of the Rings", 155, "Adventure", 5);
movie StarWars(1, "Star Wars", 132, "SciFi", 5);
movie Inception(2, "Inception", 143, "Action", 4);
movie Interstellar(3, "Interstellar", 153, "SciFi", 5);
movie Tenet(4, "Tenet", 135, "Action", 3);
movie moviearr [5] = {LordOfTheRings, StarWars, Inception, Interstellar, Tenet};
for (int i = 0; i <= 4; i ){
moviearr[i].showrating();
}
運行程式后,結果如下。評級應該出現在那里,但我只是得到這些亂數。
結果我從上面的代碼中得到
我真的很感激一些幫助。
uj5u.com熱心網友回復:
您忘記在建構式中為任何內容設定評級。這就是為什么它的值是“隨機的”
改變這個
movie::movie(int _id, std::string _name, int _length, std::string
_genre, int _rating) : video(_id, _name, _length, _genre){}
對此
movie::movie(int _id, std::string _name, int _length, std::string
_genre, int _rating) : video(_id, _name, _length, _genre), rating(_rating) {}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491700.html
上一篇:進入目錄掛起終端
