C ++ inheritance and combination

C ++ inheritance and combination

Inheritance and combination

Inheritance and memory structures resulting combination of the two objects are identical, but the portion to be rewritten contains member functions when used in combination with a class object
, for example:
wpse890.tmp.jpeg
When used in combination, if not members of the object to obtain override member function, we need to use
information m_Cperson.GetSex () and m_Cperson.GetName to get members of the object

Inheritance and access control

wpse891.tmp.jpeg
Note:

  • The above table "invisible" just compile-time checking whether a code to access the parent class's private members, regardless of the manner in which inheritance, the parent class's
    private data members and member functions in subclasses are not visible, inheritance is used to reassign the parent class member access in sub-class.
  • If not specified at the time of inheritance inheritance, the default is private inheritance
  • If the parent class has to modify private data member of non-private member functions, then call these subclasses to call the parent class member functions can be changed
    private data members of the parent class.
  • When you create a subclass object, the object is to create a parent class inherits the parent class object on the front, this one is for compatibility,
    if you want to be a member of a subclass later expansion, just add to the tail , the previous procedures do not make changes still can be a normal visit, this
    also simplifies the compiler calculates the offset parent object.
  • And data hiding function overloading distinction:
    Data Hiding: a member of the same name in different scopes b c argument list is the same...
    Overloaded functions:... A scope of the same functions with the same name b c different parameter list

Inherited member function of the access method:

  • 当写有子类对象调用函数的代码时,编译器先在子类的类域中寻找是否有该名称的函数,如果有这样的一个
    或者多个函数名称挑,就选出最佳的 (这个过程与调用重载函数类似),如果有该名称的函数,但是参数列表
    不匹配,这时就会报错,即使所继承的父类中有匹配的同名函数,编译器也不会在父类的类域中寻找.
  • 当写有子类对象调用函数的代码时,编译器先在子类的类域中寻找是否有该名称的函 数,如果子类的类域中
    没有该名称的函数,那么编译器就去父类的类域中寻找是否有匹配的该名称函数,如果没有就报错
  • 当子类和父类中有同名的函数时,当调用该函数时,默认的是优先调用子类的同名函数,其过程如上述第一
    项所描述,如果要想调用父类的同名函数,只能在调用时在函数名前加上父类的类域

子类对象向父类对象的类型转换:

一个子类对象包含两大部分:子类自己定义的成员和继承的其它父类对象(当然可能还有组合对象),因为子类对
象中含有基类的部分,所以可以把子类对象当成父类对象来使用,还可以将指向父类对象的指针指向子类中的
父类部分。
例如:

     CStudent * pstu = new CStudent(“xx”,’f’,7);
     CPerson * pPer = pstu;
     pPer->GetSex();

But let a pointer to point to a subclass object object's parent class is unsafe, cross-border access may occur resulting in abnormal program, unless it can be
determined that the parent object is included in the subclass object.

Constructors and destructors order in succession:

  • Construction: parent object constructor first and only call the constructor to initialize the parent class in the list of the subclass constructor to initialize the parent object section
    points in the constructor's own class data members
  • Destructor: first class destructor, the destructor of the parent class, subclass may be used as the parent class of resources

Inheritance use in combination with a constructor and destructor sequence when:

First call the parent class constructor during construction, the constructor call members of the object, and finally call their constructor, destructor first destructor own,
calling members of the object's destructor, and finally call the destructor of the parent class .

 

Guess you like

Origin www.cnblogs.com/UnknowCodeMaker/p/11276588.html