Design and inheritance of c ++ classes

Source: Internet
Author: User

1. the compiler will generate default constructor, copy constructor, assign value function, and destructor for the class. Of course, the default constructor simply assigns values to the object members.

2. If a member object in the class contains a member that needs to be dynamically allocated memory through new, you need to redefine the copy constructor, the heavy-load value assignment operator, the destructor, And the constructor with parameters, default constructor.

3. When constructing a derived class, the base class constructor is called first (according to the constructor type of the derived class, the corresponding form of the base class constructor is called), and then the derived Type constructor is called.

4. When the lifecycle of a class ends, the class will first call the destructor of the derived class, and then call the destructor of the base class.

5. constructor and destructor cannot be inherited, and each of them processes the assignment and structure of each member.

6. Have a deep understanding of the beginning and end of an object's life and the position of the object in the memory (on the stack and stack ?) The memory layout of object members is the key to understanding the above four points and preventing Memory leakage.

The following code will conduct an experiment on the above six points and elaborate on the basic class files people. H, people. CPP, the derived class files woman. H, woman. cpp, and the main function file testmain. cpp.

# Ifndef people_h # define people_hclass people {PRIVATE: char * Name; public: people (); // The default constructor people (const char * s ); // const people (const people & P) with parameters; // copy constructor people & operator = (const people & P); // assign a value function ~ People () ;};# endif

  

# Include "people. H "# include <stdio. h> # include <string. h> People: people () // default constructor {printf ("calls the default constructor people () \ n "); name = new char [strlen ("hello") + 1]; // allocated to the stack strcpy (name, "hello");} People: People (const char * s) // constructor with parameters {printf ("People (const char * s) \ n" called with parameters); name = new char [strlen (s) + 1]; strcpy (name, S);} People: People (const people & P) {printf ("the replication constructor people (const people & P \ n"); name = new char [Strlen (P. name) + 1]; strcpy (name, P. name);} People & People: Operator = (const people & P) {printf ("Call the value assignment function operator = \ n"); If (this = & P) return * This; else {Delete name; name = new char [strlen (P. name) + 1]; strcpy (name, P. name); return * This;} People ::~ People () {printf ("destructor called ~ People () \ n "); Delete name ;}

  

# Ifndef woman_h # define woman_h # include "people. H "class woman: Public people {PRIVATE: char * company; // Add a pointer member, allocate new memory, re-Modify the constructor and destructor int age; public: WOMAN (); // default constructor woman (const char * C, int A, const char * s); // a parameter constructor woman (const woman & W ); // copy the constructor woman & operator = (const woman & W); // reload the value assignment operator ~ WOMAN () ;};# endif

  

# Include "woman. H "# include <stdio. h> # include <string. h> woman: Woman () {printf ("the default constructor woman () \ n") is called; char * s = "world "; company = new char [strlen (s) + 1]; strcpy (Company, S); age = 99;} WOMAN: Woman (const char * C, int, const char * s): People (s) {printf ("the parameter constructor woman (const char * s, int A) \ n" is called "); company = new char [strlen (c) + 1]; strcpy (Company, c); age = A;} WOMAN: Woman (const woman & W): People (W) // copy constructor of the derived class Number definition. Because a derived class can only access its own private members, you need to call the replication constructor people (const people & P) of the base class) {// note that the parameter type of the copy constructor of the base class is people, and woman of the derived class is passed here, the base class uses the base class part of the derived type to construct the base class Object printf ("the copy constructor woman (const woman & W) \ n "); company = new char [strlen (W. company) + 1]; strcpy (Company, W. company); age = W. age;} WOMAN & Woman: Operator = (const woman & W) // This Is Not A constructor, therefore, you cannot directly call the base class constructor to assign a value to a base class member in the derived class object {printf ("the value assignment function woman: Operator = (const woman & W) is called) \ n "); If (Th Is ==& W) return * This; // assign else {delete company to yourself; // Delete the occupied memory space people: Operator = (w ); // call the value assignment operation of the base class to complete the value assignment of the base class object in the derived class object. The method is strange. Remember Company = new char [strlen (W. company) + 1]; strcpy (Company, W. company); return * This ;}} woman ::~ WOMAN () {printf ("The Destructor is called ~ WOMAN () \ n "); Delete company ;}

  

 

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.