C ++ Study Notes (5) -- Call Sequence of constructors of the base class, derived class, and destructor

Source: Internet
Author: User
This blog (http://blog.csdn.net/livelylittlefish) posted by the author (three two one, small fish) related research, learning content of the notes made, welcome to the majority of friends correct! Define a base class and two Derived classes, define their objects respectively, and observe the following code for calling the construction and destructor:

/**//*********************************** *************************************
* The construction of the base class and derived class, and the sequence in which the Destructor is called
**************************************** ********************************/
# Include <iostream. h>

// Base class
Class cperson
...{
Char * Name; // name
Int age; // age
Char * Add; // address
Public:
Cperson ()... {cout <"constructor-cperson! ";}
~ Cperson ()... {cout <"deconstructor-cperson! ";}
};

// Derived class (Student Class)
Class cstudent: Public cperson
...{
Char * depart; // the student's Department
Int grade; // grade
Public:
Cstudent ()... {cout <"constructor-cstudent! ";}
~ Cstudent ()... {cout <"deconstructor-cstudent! ";}
};

// Derived class)
Class cteacher: Public cperson
...{
Char * Major; // instructor Major
Float salary; // instructor's salary
Public:
Cteacher ()... {cout <"constructor-cteacher! ";}
~ Cteacher ()... {cout <"deconstructor-cteacher! ";}
};

// Experiment main program
Void main ()
...{
Cperson person;
Cstudent student;
Cteacher teacher;
} Running result:

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.