What is inherited from the base class?
子类从基类中继承下来了什么?
In principle, a derived class inherits every member of a base class except:
原则上讲,子类继承了父类中所有的成员,除了以下:- its constructor and its destructor
- 构造器和析构器
- its operator=() members
- 赋值运算符重载函数
- its friends
- 友元
Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.
尽管基类的构造器和析构器本身不能被继承,但是它默认构造器(比如无参的)和析构器总是被调用,当派生类对象被创建和销毁时。
If the base class has no default constructor or you want that an overloaded constructor is called when a new derived object is created, you can specify it in each constructor definition of the derived class:
如果基类没有默认的构造器,或是派生类被创建时你想调用基类重载的构造器,你可以定义派生类构造器时指定父类的构造器,如下:
derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
For example:
| | mother: no parameters daughter: int parameter mother: int parameter son: int parameter |
Notice the difference between which mother 's constructor is called when a new daughter object is created and which when it is a son object. The difference is because the constructor declaration of daughter and son :
注意当一个女儿或是儿子对象创建时,母亲的构造器被调用时的不同。不同的原因是女儿和儿子的构造器声明不同。
| |