虚函数与重载
这个是《C++ Primer Plus》中文第五版475页的第四题,有如下定义的基类和派生类
class Port{private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bottles;public: Port(const char * br = "one", const char * st = "one", int b = 0); Port(const Port & p); // copy constructor virtual ~Port() { delete [] brand; } Port & operator=(const Port & p); Port & operator+=(int b); // adds b to bottles Port & operator-=(int b); // subtracts b from bottles, if int BottleCount() const { return bottles; } virtual void Show() const; friend ostream & operator<<(ostream & os, const Port & p);};class VintagePort : public Port // style necessarily = “vintage”{private: char * nickname; // i.e., “The Noble” or “Old Velvet”, etc. int year; // vintage yearpublic: VintagePort(); VintagePort(const char * br, int b, const char * nn, int y); VintagePort(const VintagePort & vp); ~VintagePort() { delete [] nickname; } VintagePort & operator=(const VintagePort & vp); void Show() const; friend ostream & operator<<(ostream & os, const VintagePort & vp);};