我正在学习 C++ 中的继承,而且我是抽象纯虚拟、基类和派生类的新手。所以我想出了下面的代码,它可以工作,但我不确定我是否正确地实现了 c++ 原则。请有人评估以下代码以进行改进。
#include <iostream>
using namespace std;
class Pizza
{
private:
double pCost;
double pPrice;
public:
Pizza(const double& c, const double& p) : pCost(c), pPrice(p){}
virtual ~Pizza(){}
virtual double area() = 0;
virtual double cost() = 0;
virtual double price () = 0;
virtual double profit() = 0;
double getCost() const {return pCost;}
double getPrice() const {return pPrice;}
};
class CircularPizza : public Pizza
{
private:
double radius;
public:
CircularPizza(const double& r, const double& c, const double& p)
: Pizza(c, p), radius(r){}
virtual ~CircularPizza(){}
virtual double area(){ return (3.14 * radius * radius);}
virtual double cost() { return area() * getCost(); }
virtual double price() { return area() * getPrice(); }
virtual double profit() { return price() - cost();}
};