derived class's constructor need call the base class's constructor
Derived_class_name :: Derived_class_name(expressions)
: Base_class_name(expressions)
{
statements
}
| Example: | Manager::Manager(string name, double salary, string dept)
: Employee(name, salary)
{
department = dept;}
int TravelClock::get_hours() const
{
int h = Clock::get_hours();//extend the base class member function
if (is_military())
return (h + time_difference) % 24;
else
{
h = (h + time_difference) % 12;
if (h == 0) return 12;
else return h;
}
}
To tell the C++ compiler that a particular function
needs to be bound dynamically, the function must be tagged as virtual in the base class.
class clock {
public:
Clock(bool use_military);
virtual string get_location() const;
virtual int get_hours() const;
int get_minutes() const;
bool is_military() const;
private:
. . .
};And in oeder to show polymorphism, we need to use base class pointer to point to both base class and derived class.(note that base class pointer can point to derived class)
|
没有评论:
发表评论