我想提高自己在C ++中的技能,我试图构建一个存储不同能量及其消耗的代码。一切正常,但我不明白如何计算消费量。我的问题是,当我创建指向不同能量的指针时,我在Energy类中的d_consumption被重置,并且我只有最新的能量消耗。如果可能的话,我想知道我的编码风格是否很好。
class Energy
{
private:
double d_totalconsumption = 0.0; //count total consumption
double d_totalbill = 0.0; //count total bill $
public:
Energy(){}
virtual ~Energy(){}
void addConsumption(double d) { d_totalconsumption += d; }
double totalConsumption() const { return d_totalconsumption; }
void addBill(double d) { d_totalbill += d; }
double totalBill() const { return d_totalbill; }
virtual void setConsumption(double consumption) =0;
virtual void setBill(double billCAD) =0;
virtual double getConsumption() const =0;
virtual double getBill() const =0;
};
class Mazout : public Energy
{
private:
double d_consumption = 1.e-16; // init to avoid divide by zero later
double d_bill = 1.e-16; // init to avoid divide by zero later
public:
Mazout() : Energy() { }
virtual ~Mazout(){}
void setConsumption(double consumption) { d_consumption = liters; Energy::addConsumption(consumption); }
void setBill(double billCAD) { d_bill = billCAD; }
double getConsumption() const { return d_consumption; }
double getBill() const { return d_bill; }
};
class NaturalGas : public Energy
{
private:
double d_consumption = 1.e-16; //init to avoid divide by zero later
double d_bill = 1.e-16; //init to avoid divide by zero later
public:
NaturalGas() : Energy() {std::cout << std::fixed;}
virtual ~NaturalGas(){}
void setConsumption(double consumption) { d_consumption = consumption; Energy::addConsumption(consumption); }
void setBill(double billCAD) { d_bill = billCAD; }
double getConsumption() const { return d_consumption; }
double getBill() const { return d_bill; }
};
int main()
{
string input;
stringstream inputStream;
int choice;
bool exit = false;
Energy *ptrEnergy;
vector<Energy*> energyList;
ptrEnergy = new Mazout();
ptrEnergy->setConsumption(26180);
ptrEnergy->setBill(26952);
energyList.push_back(ptrEnergy)
ptrEnergy = new NaturalGas();
ptrEnergy->setConsumption(34000);
ptrEnergy->setBill(17000);
energyList.push_back(ptrEnergy)
// print data
std::cout << "Consumption " << energyList(0)->getConsumption() << std::endl;
std::cout << "Bill " << energyList(0)->getBill() << std::endl;
std::cout << "Consumption " << energyList(1)->getConsumption() << std::endl;
std::cout << "Bill " << energyList(1)->getBill() << std::endl;
std::cout << "Total Consumption " << ptrEnergy()->getConsumption() << std::endl;
std::cout << "Total Bill " << ptrEnergy()->getBill() << std::endl;
}
我期望这样。
Consumption 26180.0
Bill 26952.0
Consumption 34000.0
Bill 17000.0
Total Consumption 29580.0 // I have 34000.0
Total Bill 43952.0 // I have 17000.0
答案 0 :(得分:0)
我认为您不了解的概念是类的静态变量。
静态变量是同一类中的共享变量。您使用Classname::variable
在代码中引用它们。静态函数也是如此。
例如,
class A {
public:
static int object_count;
int x;
A(int _x) : x(_x) {
++object_count;
}
};
int A::object_count = 0;
int main(){
A a1(8);
cout << A::object_count << endl; // prints 1
A a2(9);
cout << A::object_count << endl; // prints 2
}
对于您的特定问题,您希望计算所有对象实例的总消耗/成本,因此希望计数器和函数是静态的。
class Energy
{
private:
static double d_totalconsumption = 0.0; //count total consumption
static double d_totalbill = 0.0; //count total bill $
public:
Energy(){}
virtual ~Energy(){}
static void addConsumption(double d) { Energy::d_totalconsumption += d; }
static double totalConsumption() const { return Energy::d_totalconsumption; }
static void addBill(double d) { Energy::d_totalbill += d; }
static double totalBill() const { return Energy::d_totalbill; }
virtual void setConsumption(double consumption) =0;
virtual void setBill(double billCAD) =0;
virtual double getConsumption() const =0;
virtual double getBill() const =0;
};
// ...
int main()
{
// ...
std::cout << "Total Consumption " << Energy::totalConsumption() << std::endl;
std::cout << "Total Bill " << Energy::totalBill() << std::endl;
}
答案 1 :(得分:0)
感谢Daniel的静态解决方案!我将对此进行探讨。
drescherjm,我真的没有客户,Energy与另一类具有能量的建筑相关。我可能可以将d_totalconsumption
放在Building
类中,但是我想将其保留在Energy
类中(因为这是能源的总消耗)。但是您的观点很有趣!谢谢大家