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: