New Features of C ++ (inheriting the *** five stars)

Source: Internet
Author: User

Inheritance: A class directly contains the attributes and methods of another class.

Similar things have common characteristics. In similar things, each things has special characteristics. Abstract principles are used to discard the particularity of objects and extract their commonalities. A Class adapted to a batch of objects is obtained. This is the base class (parent class );

A special class is called a derived class (subclass ). The object of a derived class has all or part of the attributes and methods of the base class, which is called the inheritance of the base class by the derived class.

 

There are three inheritance Methods: Public inheritance, private inheritance, and protection inheritance.

You can use a column to understand the impact of different inheritance methods on the member functions of the class:

Class cfruit // declare the base class {public: void func1 (); // member function protected: int x1; // protect data member private: int Y1; // Private Data member}; Class capple: Public cfruit // capple class inherits from cfruit class {public: void func2 (); // member function protected: int X2; // protect the data member private: int Y2; // Private Data member}; Class corange: Private cfruit // The corange class private inherits from the cfruit class {public: void func3 (); // member function}; Class crichapple: Public capple // The crichapple class inherits from the capple class {public: void func4 (); // member function}; Class cgrapefruit: public corange // The cgranpefruit class inherits from the corange class {public: void func5 ();};

1. The member function func2 In the subclass capple can access the member functions func1 and data member X1 in the parent class cfruit, but cannot access the data member Y1;

2. The member function func3 In the subclass corange is the same as the first one;

3. The member function func4 in crichapple can access the member function func1 in cfruit, the member function func2 in capple, and the data member X2, however, cfruit data members X1 and Y1 and capple data members Y2 cannot be accessed;

4. The member function func5 In the subclass cgrapefruit cannot access any member in cfruit, but can access the member function func3in corange.

 

Constructor of subclass:

To implement the subclass constructor, in addition to initializing the data members of the subclass, you must also call the constructor of the parent class to initialize the data members of the parent class. To initialize a data member of the parent class, you must initialize the member list. If the child class contains sub-objects, the initialization of sub-objects should also be included.

The sequence of calling a subclass constructor is to call the parent class constructor before calling the subclass constructor.

Using a program example:

# Include <iostream> # include <fstream> using namespace STD; Class cpoint {public: cpoint (); cpoint (int x, int y); void disp (); void getxy (Int & X, Int & Y); Private: int m_x; int m_y;}; cpoint: cpoint () {m_x = 0; m_y = 0; cout <"Call the default constructor of cpoint" <Endl;} cpoint: cpoint (int x, int y) {m_x = x; m_y = y; cout <"Call cpoint constructor" <Endl;} void cpoint: getxy (Int & X, Int & Y) {x = m_x; y = m_y ;} void cpoint: disp () {cout <m_x <"," <m_y <Endl;} class c3dpoint: public cpoint // The declared subclass c3dpoint inherits from the cpoint class {public: c3dpoint (); c3dpoint (int x, int y, int Z); void disp (); Private: cpoint P; int m_z;}; c3dpoint: c3dpoint (): cpoint () // call the default constructor of the parent class {m_z = 0; cout <"Call c3cpoint default constructor" <Endl;} c3dpoint: c3dpoint (int x, int y, int Z): cpoint (x, y ), P (x, y) // (initialize the member list) Call the constructor of the parent class and initialize the sub-object {m_z = z; cout <"Call c3cpoint constructor" <Endl;} void c3dpoint: disp () {int x = 0, y = 0; p. getxy (x, y); cout <x <"," <Y <"," <m_z <Endl;} int main () {c3dpoint point3d1, point3d2 (50, 80, 10); cpoint P (20, 30); p. disp (); point3d1. disp (); point3d2. disp (); Return 0 ;}

From the code output structure, we can see that the subclass object point3d can not only implement all the functions of the parent class Object P, but also add new functions to achieve the output of the Z axis coordinates.

 

The sub-class destructor. The Calling process is to first call the sub-class destructor and then call the parent class's destructor.

 

Subclass type adaptation:

The pointer and reference of the base class object can represent the pointer and reference of the derived class object. However, the pointer and reference of child classes do not represent the object pointer and reference of the parent class. Otherwise, a compilation error occurs.

 

Multi-inheritance:

The relationship between a subclass and multiple parent classes:

Example: Multi-inherited subclass constructor example:

 

# Include <iostream> # include <fstream> using namespace STD; Class canimal {public: canimal () {m_age = 0;} canimal (INT age); void disp (); PRIVATE: int m_age;}; canimal: canimal (INT age) {m_age = age; cout <"Call the canimal constructor" <Endl;} void canimal :: disp () {cout <m_age <"," ;}class cmammal {public: cmammal () {m_latex = 0 ;}cmammal (INT latex); int getnum () {return m_latex;} void disp (); Private: int m_latex;}; cmammal: cmammal (INT latex) {m_latex = latex; cout <"Call cmammal constructor" <Endl;} void cmammal: disp () {cout <m_latex <",";} class cmonkey: Public canimal, public cmammal // both canimal and cmammal {public: cmonkey (INT age, int latex, int A, int tail); void print (); Private: cmammal mammal; int m_tail;}; cmonkey: cmonkey (INT age, int latex, int A, int tail): cmammal (latex), canimal (AGE), mammal () // class cmonkey constructor. In multi-inherited subclass constructor, you must call the constructor {m_tail = tail; cout <"Call cmonkey constructor" <Endl;} void cmonkey: Print () {canimal: disp (); cmammal: disp (); cout <mammal. getnum () <"," <m_tail <Endl;} int main () {cmonkey monkey (10, 20, 30, 40); monkey. print (); Return 0 ;}

Subclass in Multi-Inheritance
 

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.