我真的很希望這是我最後一次問作業問題的時間。過去七個小時我一直在這裏工作,三次拋出所有的代碼,並且我似乎無法通過繼承構造函數來克服我的問題。我看了其他的問題,跟着我看到的每一個建議,而且還沒有骰子。任何和所有的幫助表示讚賞。C++繼承具有多個參數的構造函數[作業]
頭文件:
#ifndef PACKAGE_H
#define PACKAGE_H
struct info
{
std::string sName;
std::string sAddr;
std::string sCity;
std::string sState;
std::string sZip;
std::string rName;
std::string rAddr;
std::string rCity;
std::string rState;
std::string rZip;
};
class Package
{
public:
Package(int c, double w, double co, info *i); //Constructor. Will implement
//all data types
~Package(); // Default Destructor
virtual double calculateCost(); // Not pure, can be overloaded
protected:
int counter;
double weight;
double cost;
info sendRecieve; // Struct for holding tedious info
};
class TwoDayPackage : public Package
{
public:
TwoDayPackage(int c, double w, double co, info *i, double a)
:Package(c, w, co, *i, a);
double calculateCost();
private:
double flatFee;
};
class OvernightPackage : public Package
{
public:
OvernightPackage(int c, double w, double co, info *i, double a)
:Package(c, w, co, *i);
double calculateCost();
private:
double addtlCost;
};
的.cpp定義文件:
#include "Package.h"
using namespace std;
Package::Package(int c, double w, double co, info *i)
{
cost = c;
weight = w;
cost = co;
sendRecieve.sName = i->sName;
sendRecieve.sAddr = i->sAddr;
sendRecieve.sCity = i->sCity;
sendRecieve.sState = i->sState;
sendRecieve.sZip = i->sZip;
sendRecieve.rName = i->rName;
sendRecieve.rAddr = i->rAddr;
sendRecieve.rCity = i->rCity;
sendRecieve.rState = i->rState;
sendRecieve.rZip = i->rZip;
}
double Package::calculateCost()
{
return (cost * weight);
}
// end base class definitions
TwoDayPackage::TwoDayPackage(int c, double w, double co, info *i, double a)
:Package(c, w, co, *i)
{
flatFee = a;
}
double TwoDayPackage::calculateCost()
{
return ((cost * weight) + flatFee);
}
//end TwoDayPackage definitions
OvernightPackage::OvernightPackage(int c, double w, double co, info *i, double a) :Package(c, w, co, *i)
{
addtlCost = a;
}
double OvernightPackage::calculateCost()
{
double multi = (addtlCost + cost);
return (multi * weight);
}
這裏是我不斷收到錯誤的圖像。如果有人可以幫助,我會感激。此外,歡迎任何建議。提前感謝大家。
錯誤說明了這一切:'Package'沒有帶5個參數的構造函數。然而你試圖在'TwoDayPackage'構造函數中調用它。 – juanchopanza