Question

I'm learning inheritance in c++, and I'm new to abstract pure virtual, base, and derived classes. So I came up with this below code, it works but I'm not sure if I'm implementing the c++ principals correct. Please can someone evaluate the below code for improvement.

#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();}
};
Was it helpful?

Solution

If I were to change anything, I'd make cost(), price() and profit() be non-virtual and define them in the base Pizza class.

This makes sense because they all depend on area -- that's the only thing that CircularPizza is really defining. If you were to make a RectangularPizza class, the cost per area, the price per area, the calculation of profit would be the same. The only thing that would change would be the area itself. Realising this fact is what should lead you to design the classes in the same way, where only the area changes and other code is common to both CircularPizza and RectangularPizza using a base class.

OTHER TIPS

That's looking okay... However, it's strange to have a getCost() method as well as cost(). Same with price... If the whole point of the cost and price functions is to be implementation-specific, then providing access to the internal pCost and pPrice members doesn't seem right.

Perhaps it's just a naming thing? Since you provide a cost and price in the constructor it's fair enough to be able to query these later. So in that case, there's a semantic problem (in my eyes anyway) with the names of the cost() and price() members. Or perhaps what you mean is something like "unit cost" and "total cost".

You may want to consider making the member variables protected instead of private.

In terms of your C++ it all looks okay, except possibly those virtual functions should be constant if they do not modify the class (that way they can be called on a const instance of the class).

Also, for completeness you may want to implement a function CircularPizza::getRadius() to be consistent with functions provided by the Pizza class.

For homework, looks fine. For additional reading consider this on when to use private virtual functions. Additionally I find it's helpful to occasionally create interfaces, similar to C#.

One other point worth mentioning is that the area(), cost(), price() and profit() functions in the CircularPizza object don't need to be virtual unless you are intending to use CircularPizza as a base class for another derived class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top