2016-09-27 81 views
0

我的程序正在爲每個客戶輸出一組奇怪的數字,並且沒有得到每個客戶的名字。我不確定我是否正確使用了虛擬功能,因爲這對我來說在概念上是比較新的。使用虛函數的C++

Compute_Bill()函數在每個類中都使用,因爲Premium_Customer對他們的賬單使用與正常客戶不同的計算。我已經評估了每個賬單的計算成本。

main()函數僅用於爲每個人創建具有不同名稱和調用次數的列表,這樣程序應該顯示兩個不同定價計劃的示例。

這裏是輸出:

客戶欠10美元。

客戶欠20.4美元。

客戶欠款-7.02934e + 114美元。

客戶欠20.4美元。

客戶欠10美元。

客戶欠20.4美元。

客戶欠10美元。

客戶欠20.4美元。

客戶欠10美元。

客戶欠20.4美元。

客戶欠10美元。

客戶欠20.4美元。

客戶欠10美元。

客戶欠20.4美元。

客戶欠10美元。

客戶欠20.4美元。

客戶欠10美元。

客戶欠款1.24244e + 150美元。

這裏是我的程序,從上到下將其與Customer類,然後高級班開始,然後主:

#include <iostream> 

using namespace std; 

// CUSTOMER CLASS 
class Customer 
{ 
private: 
    double numCalls; 
    string name; 
    const double MONTH_FEE = 10; 
    const double PER_CALL = .5; 

protected: 
    double bill; 

public: 
    Customer(); 
    Customer(string aName, double aCalls); 
    virtual double Compute_Bill(); 
    string getName(); 
    void setName(string aName); 
    double getCalls(); 
    void setCalls(double aCalls); 
}; 

Customer::Customer() 
{ 
} 

Customer::Customer(string aName, double aCalls) 
{ 
    aName = ""; 
    aCalls = 0; 
} 

string Customer::getName() 
{ 
    return name; 
} 

void Customer::setName(string aName) 
{ 
    aName = name; 
} 

double Customer::getCalls() 
{ 
    return numCalls; 
} 

void Customer::setCalls(double aCalls) 
{ 
    aCalls = numCalls; 
} 

// Computing the bill for the Customer, uses 
// bill = monthlyfee + (percallrate * numcalls) 
// monthly fee = $10 
// per call charge = .50 
double Customer::Compute_Bill() 
{ 
    bill = MONTH_FEE + (PER_CALL * numCalls); 
    return bill; 
} 


// PREMIUM_CUSTOMER CLASS 
class Premium_Customer : public Customer 
{ 
private: 
    double numCalls; 
    string name; 
    const double MONTH_FEE = 20; 
    const double PER_CALL = .05; 
    const double PER_MINUTE = .1; 
    const double NUM_MINS = 4; 

protected: 
    double bill; 

public: 
    Premium_Customer(); 
    Premium_Customer(string aName, double aCalls); 
    virtual double Compute_Bill(); 
    string getName(); 
    void setName(string aName); 
    double getCalls(); 
    void setCalls(double aCalls); 
}; 

Premium_Customer::Premium_Customer() 
{ 
} 

Premium_Customer::Premium_Customer(string aName, double aCalls) 
{ 
    aName = ""; 
    aCalls = 0; 
} 

string Premium_Customer::getName() 
{ 
    return name; 
} 

void Premium_Customer::setName(string aName) 
{ 
    aName = name; 
} 

double Premium_Customer::getCalls() 
{ 
    return numCalls; 
} 

void Premium_Customer::setCalls(double aCalls) 
{ 
    aCalls = numCalls; 
} 

// Computing the bill for the Premium_Customer, uses 
// bill = monthlyfee + (percallrate * numcalls) + (permin_callrate * nummins) 
// monthly fee = $20 
// per call charge = .05 
// per minute call rate = .10 
// nummins = 4 
double Premium_Customer::Compute_Bill() 
{ 
    bill = MONTH_FEE + (PER_CALL * numCalls) + (PER_MINUTE * NUM_MINS); 
    return bill; 
} 

// MAIN CLASS 
int main() { 
    Customer* list[18] ; 
    list[0] = new Customer("John Dough", 20); 
    list[1] = new Premium_Customer("Bob Dough", 20); 
    list[2] = new Customer("Tim Dough", 30); 
    list[3] = new Premium_Customer("Jane Dough", 30); 
    list[4] = new Customer("Bill Dough", 40); 
    list[5] = new Premium_Customer("Tom Dough", 40); 
    list[6] = new Customer("Jim Dough", 50); 
    list[7] = new Premium_Customer("Kane Dough", 50); 
    list[8] = new Customer("Hon Dough", 60); 
    list[9] = new Premium_Customer("Jill Dough", 60); 
    list[10] = new Customer("Mary Dough", 70); 
    list[11] = new Premium_Customer("Un Dough", 70); 
    list[12] = new Customer("Sarah Dough", 80); 
    list[13] = new Premium_Customer("Liz Dough", 80); 
    list[14] = new Customer("Will Dough", 90); 
    list[15] = new Premium_Customer("Mike Dough", 90); 
    list[16] = new Customer("Brian Dough", 100); 
    list[17] = new Premium_Customer("Kurt Dough", 100); 
    for(int i=0; i<18; i++) { 
     cout << "Customer " << list[i]->getName() << " owes " 
     << list[i]->Compute_Bill() << " dollars." << endl; 
    } 
    // delete all the customers 
    return 1; 
} 
+0

您的構造函數似乎沒有正確設置成員變量。而且你的許多功能也一樣。嘗試通過const/const&將輸入傳遞給應該設置變量以捕獲錯誤的函數。 –

+0

@RobertPrévost你能解釋他們爲什麼錯了嗎? – Yoyokolo

+0

以'Customer :: Customer(string aName,double aCalls)'爲例。而不是設置成員變量,而是設置臨時輸入變量。 –

回答

1

你有很多的小錯誤。你的產業,基本上是罰款,你只需要刪除部分不需要:

#include <iostream> 
#include <string> 

using namespace std; 

// CUSTOMER CLASS 
class Customer 
{ 
private: 
    double numCalls; 
    string name; 
    const double MONTH_FEE = 10; 
    const double PER_CALL = .5; 

protected: 
    double bill; 

public: 
    Customer(); 
    Customer(const string& aName, double aCalls); 
    virtual double Compute_Bill(); 
    string getName() const; 
    void setName(const string& aName); 
    double getCalls() const; 
    void setCalls(double aCalls); 
}; 

Customer::Customer() 
{ 
} 

Customer::Customer(const string& aName, double aCalls) 
{ 
    setName(aName); 
    setCalls(aCalls); 
} 

string Customer::getName() const 
{ 
    return name; 
} 

void Customer::setName(const string& aName) 
{ 
    // you had this mixed up 
    name = aName; 
} 

double Customer::getCalls() const 
{ 
    return numCalls; 
} 

void Customer::setCalls(double aCalls) 
{ 
    // this was mixed up too 
    numCalls = aCalls; 
} 

// Computing the bill for the Customer, uses 
// bill = monthlyfee + (percallrate * numcalls) 
// monthly fee = $10 
// per call charge = .50 
double Customer::Compute_Bill() 
{ 
    bill = MONTH_FEE + (PER_CALL * numCalls); 
    return bill; 
} 


// PREMIUM_CUSTOMER CLASS 
class Premium_Customer : public Customer 
{ 
private: 
    const double MONTH_FEE = 20; 
    const double PER_CALL = .05; 
    const double PER_MINUTE = .1; 
    const double NUM_MINS = 4; 

public: 
    Premium_Customer(); 
    Premium_Customer(const string& aName, double aCalls); 
    virtual double Compute_Bill(); 
    // the other methods are already inherited, no need to implement them again... 
}; 

Premium_Customer::Premium_Customer() 
{ 
} 

// no special logic here, just delegate to your base class constructor 
Premium_Customer::Premium_Customer(const string& aName, double aCalls) : Customer(aName, aCalls) 
{ 
} 

// Computing the bill for the Premium_Customer, uses 
// bill = monthlyfee + (percallrate * numcalls) + (permin_callrate * nummins) 
// monthly fee = $20 
// per call charge = .05 
// per minute call rate = .10 
// nummins = 4 
double Premium_Customer::Compute_Bill() 
{ 
    // no direct access to private customer variables here, used getCalls method instead 
    bill = MONTH_FEE + (PER_CALL * getCalls()) + (PER_MINUTE * NUM_MINS); 
    return bill; 
} 

// MAIN CLASS 
int main() { 
    Customer* list[18]; 
    list[0] = new Customer("John Dough", 20); 
    list[1] = new Premium_Customer("Bob Dough", 20); 
    list[2] = new Customer("Tim Dough", 30); 
    list[3] = new Premium_Customer("Jane Dough", 30); 
    list[4] = new Customer("Bill Dough", 40); 
    list[5] = new Premium_Customer("Tom Dough", 40); 
    list[6] = new Customer("Jim Dough", 50); 
    list[7] = new Premium_Customer("Kane Dough", 50); 
    list[8] = new Customer("Hon Dough", 60); 
    list[9] = new Premium_Customer("Jill Dough", 60); 
    list[10] = new Customer("Mary Dough", 70); 
    list[11] = new Premium_Customer("Un Dough", 70); 
    list[12] = new Customer("Sarah Dough", 80); 
    list[13] = new Premium_Customer("Liz Dough", 80); 
    list[14] = new Customer("Will Dough", 90); 
    list[15] = new Premium_Customer("Mike Dough", 90); 
    list[16] = new Customer("Brian Dough", 100); 
    list[17] = new Premium_Customer("Kurt Dough", 100); 

    for (int i = 0; i<18; i++) { 
     cout << "Customer " << list[i]->getName() << " owes " << list[i]->Compute_Bill() << " dollars." << endl; 
    } 

    // delete all the customers 
    return 0; 
} 
+0

我相信你以前幫過我。再一次感謝你。總有那些讓我喜歡的東西。但我喜歡從我的錯誤中學習!謝謝! – Yoyokolo

0

你的代碼未定義的行爲,因爲你的構造函數不初始化成員字段。特別是成員numCalls從不初始化,它用於方法Compute_Bill的結果。

工具像Valgrind的點到這樣的錯誤:在你的代碼,Valgrind的說

by 0x401C55: main (file.cpp:157) 
Conditional jump or move depends on uninitialised value(s) 

事實上,在你的方法setCall,您應該定義

numCalls = aCall; 

,而不是

aCall = numCalls; 

由於它確實影響參數,所以它沒有效果。