Object-Oriented Programming-c++_ class 16 subclass parent Relationship

Source: Internet
Author: User

Initialize List

Class Name:: Class name (parameter 1, parameter 2,... Parameter n): data member 1 (parameter 1), data member 2 (parameter 2),..., data member n (parameter N)

{

...

}

Rule 1, initializing the list for data member initialization

Rule 2, initializing the list for the initialization of the parent class

1#include <iostream>2 using namespacestd;3 4 classA5 {6 Private:7     inti;8  Public:9AintII): I (ii)//Initialize listTen     { OneStd::cout <<"a::a ()"<<Std::endl; A     } -~A () -     { theStd::cout <<"~a::a ()"<<Std::endl; -     } -     voidprint () -     { +Std::cout <<"A::p rint ()"<< I <<Std::endl; -     } +  A     voidPrintinti) at     { -Std::cout << i <<Std::endl; - print (); -     } -  -     void Set(intII) in     { -i =II; to     } + }; -  the classD | PublicA * { $  Public:Panax NotoginsengB (): A ( the) -     { theStd::cout <<"b::b ()"<<Std::endl; +     } A~B () the     { +Std::cout <<"b::~b ()"<<Std::endl; -     } $     voidprint () $     { -Std::cout <<"B::p rint ()"<<Std::endl; -     } the     voidf () -     {Wuyi         Set( -); the print (); -     } Wu }; -  About voidMain () $ { - b b; -B.Set(Ten); - b.print (); A B.f (); +B.A::p rint ( $); the      -System"Pause"); $}

When a constructor is defined for a derived class and a base class, the compiler calls the constructor of the base class first, if there is an object member that executes the constructor of the object member, and finally the constructor of the derived class. The destructor is in the exact opposite order of the constructor function.

Object-Oriented Programming-c++_ class 16 subclass parent Relationship

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.