Reputation: 3373
I'm having:
class DeliveryVehicle{
public:
//c'tor
DeliveryVehicle(const char* ID, Quality quality);
//d'tor
virtual ~DeliveryVehicle();
int performDeliveryDay(int* numberOfDeliveries);
...
protected:
...
/* PrintDailySummary: here numberOfDeliveries is a "dummy" parameter but
it would be used in the ProfessionalDeliveryVehicle overriding function */
virtual void PrintDailySummary(int dailyProfit, int numberOfDeliveries = 0) const;
};
As can be seen performDeliveryDay() is a non-virtual function and only the printing function is virtual, for I want to print extra information in the derived type.
Virtual PrintDailySummary() is called inside the non-virtual function performDeliveryDay()
[I don't add the implementation of performDeliveryDay() - if it is relevant I will edit my post]
Additionally I'm having derived class:
class ProfessionalDeliveryVehicle:public DeliveryVehicle {
public:
//c'tor
ProfessionalDeliveryVehicle(const char* ID, Quality quality):
DeliveryVehicle(ID,quality) {}
//d'tor
// Vehicle destructor is called by default
protected:
void PrintDailySummary(int dailyProfit, int numberOfDeliveries);
};
The implementation of the printing function in the derived class is:
void ProfessionalDeliveryVehicle::PrintDailySummary(int dailyProfit, int numberOfDeliveries){
DeliveryVehicle::PrintDailySummary(dailyProfit, numberOfDeliveries);
// print some extra statistics
}
within the program I have a queue of base pointers that may point to base or derived class.
For each element in the queue I call the function performDeliveryDay(). I expected to see the extra printing for the derived class objects. From some reason I don't see them, only the printing of the base method. When I put breakpoint inside the printing function of the derived class I saw it doesn't even entered.
Can someone point my problem? thanks
EDIT: Etienne Maheu pointed out the problem. There was a mismatch between the printing functions - "const" part - signature. The problem was solved.
Upvotes: 3
Views: 2146
Reputation: 3255
Your derived class's virtual method does not have the same signature. It is missing the const
qualifier. Might also want to specifiy the default value depending on your usage.
virtual void PrintDailySummary(int dailyProfit, int numberOfDeliveries = 0) const;
void PrintDailySummary(int dailyProfit, int numberOfDeliveries);
Note: if you are using C++11, you might want to use override
keyword to declare your override intention to the compiler. This will help catch such bugs at compile time.
Upvotes: 11