2017-02-24 58 views
-1

嘗試打印時無法訪問變量「shipcost」。我得到0.00或十六進制的地址。不知道爲什麼會發生這種情況,我宣佈它在主類Package的保護範圍內,然後在私人Video Games子類中再次具體指定爲4.99。在其他小類中,它們是不同的......我應該注意,它是受保護的全部原因。所以4.99不是一個常數。無法在受保護的類中顯示變量

但請注意,它在成本和總成本的計算中使用時會返回正確的美元金額。所以它正在正確使用,但我嘗試使用「cout < < shipcost < < endl;」在類Package中,它不能輸出0.00以外的任何內容。這是我的問題。

class Package 
{ 
protected: 
    string name_and_address = "?"; 

    double cost = 0.0; 
    double discount = 0.0; 
    double discount_rate = 0.0; 

    bool overnight_delivery = false; 
    bool insured = false; 

    string package_contents = "?"; 

    double shipcost = 0.0; 

}; 

class Video_Games :public Package 
{ 
private: 
    int num_games = 0; 
    double shipcost = 4.99; 

public: 
    Video_Games(string location, int number_of_games, bool express, bool insurance) 
    { 
     num_games = number_of_games; 
     name_and_address = location; 
     overnight_delivery = express; 
     insured = insurance; 

     package_contents = to_string(num_games) + " Video Game(s)"; 

     cost = calculate_cost(); 
     discount = calculate_discount(); 

    } 

    ~Video_Games() {}; 

protected: 

    double calculate_cost() 
    { 
     cost = num_games * 19.99; 
     if (overnight_delivery) { cost += shipcost; } 
     if (insured) { cost *= 1.06; } 

     return cost; 
    } 
}; 
+2

*「所以它正在被正確使用......」 - - 實際上,事實並非如此。你不能像C++那樣「覆蓋」成員變量的值。可以使用參數在構造函數中初始化成員變量,也可以創建一個虛擬成員函數來返回運輸成本。 –

+1

看起來你在基類和派生類中都有「shipcost」。這會讓你度過一個糟糕的一天。 – DiB

+0

lol @ DiB ...謝謝你澄清。我有一個預感,船舶不會返回任何事情都會成爲頭痛。 – polymorphism

回答

0

我不知道你是如何試圖訪問變量,但一個簡單的公共職能將打印正確的值:

class Video_Games :public Package 
{ 
private: 
    int num_games = 0; 
    double shipcost = 4.99; 

public: 
    Video_Games(string location , int number_of_games , bool express , bool insurance) 
    { 
     num_games = number_of_games; 
     name_and_address = location; 
     overnight_delivery = express; 
     insured = insurance; 

     package_contents = to_string(num_games) + " Video Game(s)"; 

     cost = calculate_cost(); 
     //discount = calculate_discount(); 

    } 

    ~Video_Games() {}; 
    void PrintVars(ostream& out) 
    { 
     out << num_games << '\t' << shipcost << '\n'; 
    } 
protected: 

    double calculate_cost() 
    { 
     cost = num_games * 19.99; 
     if (overnight_delivery) { 
      cost += shipcost; 
     } 
     if (insured) { 
      cost *= 1.06; 
     } 

     return cost; 
    } 
}; 


Video_Games vg("abcde",10,true,true); 

vg.PrintVars(cout); 

我仍然不知道如果我得到了它。在我看來,設置shipcost的值,而不是宣佈一個新的,可以讓每一個派生類來訪問自己的價值:

class Package 
{ 
protected: 
    string name_and_address = "?"; 

    double cost = 0.0; 
    double discount = 0.0; 
    double discount_rate = 0.0; 

    bool overnight_delivery = false; 
    bool insured = false; 

    string package_contents = "?"; 

    double shipcost = 0.0; 
    void PrintVars(ostream& out) 
    { 
     cout << "Cost = " << cost << '\n' 
      << "Discount = " << discount << '\n' 
      << "Discount Rate = " << discount_rate << '\n' 
      << "Ship Cost = " << shipcost << '\n'; 

    } 

}; 
class Video_Games :public Package 
{ 
private: 
    int num_games = 0; 

public: 
    Video_Games(string location , int number_of_games , bool express , bool insurance) 
    { 
     shipcost = 4.99; 
     num_games = number_of_games; 
     name_and_address = location; 
     overnight_delivery = express; 
     insured = insurance; 

     package_contents = to_string(num_games) + " Video Game(s)"; 

     cost = calculate_cost(); 
     //discount = calculate_discount(); 

    } 

    ~Video_Games() {}; 
    void Print(ostream& out) 
    { 
     PrintVars(out); 
    } 
protected: 

    double calculate_cost() 
    { 
     cost = num_games * 19.99; 
     if (overnight_delivery) { 
      cost += shipcost; 
     } 
     if (insured) { 
      cost *= 1.06; 
     } 

     return cost; 
    } 
}; 
class Board_Games :public Package 
{ 
private: 
    int num_games = 0; 
public: 
    Board_Games(string location , int number_of_games , bool express , bool insurance) 
    { 
     shipcost = 3.99; 
     num_games = number_of_games; 
     name_and_address = location; 
     overnight_delivery = express; 
     insured = insurance; 

     package_contents = to_string(num_games) + " Video Game(s)"; 

     cost = calculate_cost(); 
     //discount = calculate_discount(); 

    } 

    ~Board_Games() {}; 
    void Print(ostream& out) 
    { 
     PrintVars(out); 
    } 
protected: 

    double calculate_cost() 
    { 
     cost = num_games * 19.99; 
     if (overnight_delivery) { 
      cost += shipcost; 
     } 
     if (insured) { 
      cost *= 1.06; 
     } 

     return cost; 
    } 
}; 

Video_Games vg("abcde",10,true,true); 
vg.Print(cout); 
cout << '\n'; 
Board_Games bg("cdefg" , 20 , false , false); 
bg.Print(cout); 

這會產生這樣的回報:

Cost = 217.183 
Discount = 0 
Discount Rate = 0 
Ship Cost = 4.99 

Cost = 399.8 
Discount = 0 
Discount Rate = 0 
Ship Cost = 3.99 
+0

我可以打印這個值,這不是問題。我試圖創建一個響應正在使用的類的變量。因此,如果我在Package類中使用「shipcost」作爲變量,它應該對每個子類都做出響應,並根據需要更改爲適當的成本。所以對於視頻遊戲來說,它將是4.99,對於電話來說它將是9.99等等。以同樣的方式,我的「成本」不是單一的「成本」,而是取決於輸入的變量。我在通過變量傳遞的常數和通過主 – polymorphism

+0

中的參數傳遞的數字之間彌合這種差距時出現了問題,我試圖將它與位於頂部的Package類中的所有其他數據一起打印出來。這是擁有所有其他課程的大班。 – polymorphism

+0

@polymorphism - 我添加了更多代碼供您查看。 – tinstaafl

-1

你沒有問一個問題。但是,因爲你的狀態:

「不知道爲什麼會這樣」

以及,

「我嘗試使用cout << shipcost << endl;」 的class Package裏面,它不能輸出除0.00以外的任何內容。這就是我的問題」

我必須解釋你的意圖的問題是:

爲什麼下面的代碼不輸出價值4.99當我添加"cout << shipcost << endl;"class Package

我至少看到?一種可能性: 4.99的值在class Video_Games。 嘗試在class Video_Games內添加"cout << shipcost << endl;",而不是將此語句置於class Package之內 另外,您通過兩次使用單詞"double"宣告了兩次發貨成本。 嘗試在class Video_Games嘗試使用setter函數(刪除「double」)