C + + Primer Learning Note _31_ Object-oriented Programming (2)-Inheritance (ii): Inheritance and constructors, conversions from derived classes to base classes, base classes to derived classes

Source: Internet
Author: User
Tags class manager

C + + Primer Learning Note _31_ Object-oriented Programming (2)-Inheritance (ii): Inheritance and constructors, conversions from derived classes to base classes, base classes to derived classes
a member function that cannot be automatically inherited

constructor function

Copy constructor

Destructors
= operator


second, inheritance and constructor function

  The constructors of the     base class are not inherited, and you need to declare your own constructors in derived classes.
     declare the constructor, only the new members in this class need to be initialized, the inherited base Call the base class constructor to complete (default constructor is called if not given).
     a constructor for a derived class needs to pass parameters to the constructor of the base class

#include <iostream>using namespace Std;class objectb{public:objectb (int objb): objb_ (OBJB) {cout &lt    ;< "Objectb ..." << Endl;    } ~OBJECTB () {cout << "~objectb ..." << Endl; } int objb_;};    Class OBJECTD{PUBLIC:OBJECTD (int objd): Objd_ (OBJD) {cout << objectd ... "<< Endl;    } ~objectd () {cout << "~objectd ..." << Endl; } int objd_;};    Class Base{public:base (int b): B_ (b), Objb_ (111) {cout << "Base ..." << Endl; } Base (const base &other): Objb_ (Other.objb_), B_ (other.b_) {} ~base () {cout << ~base    ... "<< Endl;    } int b_; OBJECTB objb_;}; Class Derived:public base{public:derived (int b, int d): D_ (d), Base (b), Objd_ (222) {cout << Deriv    Ed ... "<< Endl; } Derived (const Derived &other): D_ (Other.d_), Objd_ (Other.objd_), Base (other) {} ~deriveD () {cout << "~derived ..." << Endl;    } int d_; OBJECTD Objd_;};    int main (void) {Derived D (100, 200);    cout << d.b_ << "<< d.d_ << Endl;    Base B1 (100);    Base B2 (B1);    cout << b2.b_ << Endl;    Derived D2 (d); return 0;}
Operation Result:
OBJECTB ...
Base ...
Objectd ...
Derived ...
100 200
OBJECTB ...
Base ...
100
~derived ...
~objectd ...
~base ...
~OBJECTB ...
~base ...
~OBJECTB ...
~base ...
~OBJECTB ...
~derived ...
~objectd ...
~base ...
~OBJECTB ...

Explanation: From the output you can see:

The construction order of derived class objects:

The constructor for the base class object member is called first, then the constructor for the base class, then the constructor for the object member of the derived class, and finally the constructor for the derived class itself.

You can also see this: the order in which the constructors are executed is to execute the initialization list first and then the function body. When initializing a list parameter and having a call to the base class constructor, the base class constructor is executed (from the farthest start, if multiple inheritance is in the order of inheritance), and other object members are constructed in the order defined, regardless of the order of the initialization list.

The order of the destruction is the opposite of the Order of construction.



three, friend relations, static members and inheritance

Friend relationships cannot be inherited

Static members do not matter inheritance

Example

#include <iostream>using namespace Std;class base{public:    static int b_;}; int base::b_ = 100;class derived:public base{};int main (void) {    Base B;    Derived D;    cout << base::b_ << Endl;    cout << b.b_ << Endl;    cout << derived::b_ << Endl;    cout << d.b_ << Endl;    return 0;}

Operation Result:

100
100
100
100

Explanation: All can access, output 100, but the recommended use of the class:: XX access, such as b.b_ access ambiguity, in fact, static members do not belong to any object.



iv. Conversions between classes

The conversion rules for C + + user customizations are:

1, in the public inheritance mode (private, protected inheritance, cannot be implicitly converted), the derived class object/object pointer/object reference can be assigned to the base class object/object pointer/object reference (an implicit conversion occurs). The object/object pointer/object reference of the base class cannot be assigned to the object/object pointer/object reference of the derived class. Because a derived class contains all the information for a base class, the base class lacks information from the derived class.

Example

Class A{};class B:public a{}; A; b b;

The

A = b; Valid, the derived class implicitly converts to the base class B = A; Error, base class converted to derived class, statement 1a* PA = &b;  Legal, implicit conversion b* PB = &a;  Error, statement 2a& QA = b;  Legal, implicit conversion b& QB = A;  Error, Statement 3

2, C + + allows the base classobject pointer/object referenceCast (explicit) into a derived classobject pointer/object reference。 However, Statement 1 cannot be completed by casting the type. As in the 1 code:
Statement 2 instead: b* PB = (b*) &a; Legal

Statement 3 instead:

b& QB = (b&) A; Legal


3. A pointer to a base class can be used to point to any object of the base class's public derived class, which is the key to the C + + implementation run-time polymorphism.

Note: The above implicit conversions will be used at the static_cast of the following article.

Example

#include <iostream> #include <string>using namespace Std;class employee{public:employee (const string &     Name, const int age, const int deptno): name_ (name), Age_ (age), Deptno_ (deptno) {}private:string name_;    int Age_; int deptno_;};        Class Manager:public Employee{public:manager (const string &name, const int age, const int DEPTNO, int. level) : Employee (name, age, Deptno), Level_ (level) {}private:int level_;};        Class Manager2:private Employee{public:manager2 (const string &name, const int age, const int DEPTNO, int. level) : Employee (name, age, Deptno), Level_ (level) {}private:int level_;};    int main (void) {Employee e1 ("Zhangsan", 25, 20);    Manager M1 ("Lisi", 38, 20, 10);    Manager2 m2 ("Wangwu", 40, 15, 8);    Employee *pe;    Manager *pm;    Manager2 *pm2;    PE = &e1;    PM = &m1;    PM2 = &m2;   PE = &m1; A pointer to a derived class object can be converted to a base class object pointer. Treat a derived class object as a base class object//pm = &e1; Base class object pointer cannot be transferredPointer to a derived class object.    The base class object cannot be considered a derived class object e1 = M1; Derived class objects can be converted to base class objects. Consider a derived class object as a base class object//Produces an object cut (the derived class-specific member disappears). Object slicing//pe = PM2;    When a private or protected inheritance is inherited, the derived class object pointer cannot be automatically converted to the base class object pointer pe = Reinterpret_cast<employee *> (PM2);  e1 = m2;    A derived class object cannot be converted to a base class object when it is private or protected from inheritance. e1 = reinterpret_cast<employee> (m2);    A derived class object cannot be coerced into a base class object when private or protected inheritance.    PM = Static_cast<manager *> (PE); Base-class pointers can be coerced into derived class pointers, but unsafe//m1 = reinterpret_cast<manager>e1; A base class object cannot be coerced into a derived class object return 0;}



Reference:

C + + Primer Fourth Edition

C + + Primer Fifth Edition

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer Learning Note _31_ Object-oriented Programming (2)-Inheritance (ii): Inheritance and constructors, conversions from derived classes to base classes, base classes to derived classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.