ArticleDirectory
- 2.2 destructor
- 2.3 copy constructor
- Code 4.1
- 4.2 running results
- 4.3 result Parsing
- Code 5.1
- 5.2 running results
- 5.3 Description
1. References
Reference 1: constructor and destructor calling sequence in C ++ inheritance and dynamic binding of virtual functions
Reference 2: Call time and call sequence of constructor, copy constructor, and destructor
Reference 3: Calling sequence of C ++ constructor and destructor
2. Introduction to constructor, destructor, and copy constructor 2.1 Constructor
- The constructor cannot return values.
- When the default constructor is used, the system automatically calls the default constructor to initialize the object. The default constructor initializes all data members to zero or empty.
- When an object is created, the system automatically calls the constructor.
2.2 destructor
- The Destructor has no parameters or return values. It cannot be overloaded. That is to say, only one destructor can be defined in a class.
- If no destructor is defined in a class, the system automatically generates a default destructor, which is an empty function and does nothing.
- Call conditions: 1. an object defined in the function body. When the function execution ends, the destructor of the class of the object will be automatically called. 2. an object dynamically built using the new operator is released using the delete operator.
2.3 copy constructor
A copy constructor is actually a constructor. It has all the features of a General constructor, and its name is the same as its class name. The copy constructor has only one parameter, which is a reference to a similar object. It is called in three cases:
- When initializing another object of the class with a known object of the class;
- The parameter of a function is a Class Object. When a function is called to combine the parameter with an actual parameter;
- The return value of a function is a Class Object. After the function is executed, the caller is returned.
3. the Calling sequence objects of constructor and destructor are constructed from the bottom up. When an object is created, the base class constructor is called first, then, call the constructor of the next derived class, and so on until it reaches the constructor of the class with the most derivation times. The constructor always calls the constructor of its base class before executing the constructor, if no special description is provided, the default constructor of the direct base class is called. In object structure, the order is the opposite. 4. instance 14.1 Code View code
#include <iostream>
using namespace std;
class point
{
private:
int x, y; // Data member
public:
point (int xx = 0, int yy = 0) // Constructor
{
x = xx;
y = yy;
cout << "The constructor is called" << endl;
}
point (point & p); // Copy constructor, the parameter is the reference
~ point () {cout << "The destructor is called" << endl;}
int get_x () {return x;} // method
int get_y () {return y;}
};
point :: point (point & p)
{
x = p.x; // Assign the disguised form of object p to the current member variable.
y = p.y;
cout << "The copy constructor is called" << endl;
}
void f (point p)
{
cout << p.get_x () << "" << p.get_y () << endl;
}
point g () // The return type is point
{
point a (7,33);
return a;
}
void main ()
{
point a (15,22);
point b (a); // To construct an object, use the copy constructor.
cout << b.get_x () << "" << b.get_y () << endl;
f (b);
b = g ();
cout << b.get_x () << "" << b.get_y () << endl;
}
4.2 Operation result
4.3 Results analysis The constructor is called // point a (15,22);
The copy constructor is called // point b (a); the first call to the copy constructor: when using a known object of the class to initialize another object of the class
15 22 // cout << b.get_x () << "" << b.get_y () << endl; The copy constructor is called // f (b); The second call to the copy constructor: function The formal parameter is the object of the class, when calling the function to combine the formal parameter and the actual parameter
15 22 // Member of void f (point p) function output object b
The destructor is called // f (b); the first case of the destructor is called: the object defined in the function body, when the function execution ends, the destructor of the class of the object will be automatically called
The constructor is called // b = g (); point a (7,33) in the function body; create object a
The copy constructor is called // b = g (); the third call to the copy constructor, the value of copy a is assigned to b: the return value of the function is an object of the class, and the caller is returned after the function is executed
The destructor is called // copy the destructor corresponding to the constructor
The destructor is called // b = g (); the destructor of the object a in the function body
7 33
The destructor is called // the destructuring of the main function body b object
The destructor is called // Destructuring of the main function body a object 5. Example 2 5.1 Code View Code
#include <iostream>
using namespace std;
// Base class
class CPerson
{
char * name; // Name
int age; // age
char * add; // Address
public:
CPerson () {cout << "constructor-CPerson!" << endl;}
~ CPerson () {cout << "deconstructor-CPerson!" << endl;}
};
// derived class (student class)
class CStudent: public CPerson
{
char * depart; // Department of student
int grade; // grade
public:
CStudent () {cout << "constructor-CStudent!" << endl;}
~ CStudent () {cout << "deconstructor-CStudent!" << endl;}
};
// derived class (teacher class)
// class CTeacher: public CPerson // Inherit CPerson class, two-layer structure
class CTeacher: public CStudent // Inherit CStudent class, three-layer structure
{
char * major; // teacher major
float salary; // Teacher's salary
public:
CTeacher () {cout << "constructor-CTeacher!" << endl;}
~ CTeacher () {cout << "deconstructor-CTeacher!" << endl;}
};
// Experiment main program
void main ()
{
// CPerson person;
// CStudent student;
CTeacher teacher;
}
5.2 Operation result
5.3 Description In Example 2, CPerson is the parent class of CStudent, and CStudent is also the parent class of CTeacher, then when creating a CTeacher object, first call the base class is the constructor of CPerson, and then follow the hierarchy, layer by layer Down. ps: 2012-4-12
In java, to instantiate an object of a subclass, first call the parameterless constructor of the parent class. If the parent class does not explicitly define a constructor, then the default constructor is called, which is automatically generated by the compiler. If the parent class explicitly defines a constructor, the compiler will no longer generate a default default constructor for the parent class.
Suppose that a parent-defined constructor is defined in the parent class, but no parameterless constructor is defined. At this time, when a subclass object is instantiated, a compilation error occurs because the subclass first calls the parent class's parameterless constructor. , But the parent class is not explicitly defined, and the compiler does not generate a default constructor for the parent class. At this time, you can solve this error by explicitly defining a parameterless constructor in the parent class.
Both this () and super () can be used to call constructors, and this () is used to call other constructors within the same class, such as first defining a constructor Student (name, age) in the Student class, In addition, a constructor Student (name, age, school) is defined, then the first constructor can be called in the form of this (name, age) in the second constructor, pay attention to this (name, age) Must be written on the first line of the second constructor. Super is used to call the parent class constructor from the subclass constructor. For example, the parent class Person has a constructor Person (String name, int age), and the subclass has a constructor Student (String name, int age, String school, String grade), then you can pass super (name , age) to call the parent constructor.
Recommendation: It is best to explicitly define a parameterless constructor for each class.
Examples
Person class
View Code
public class Person {
private String name = "";
private int age = 0;
public Person () {
System.out.println ("Person class parameterless constructor");
}
public Person (String name, int age) {
this.name = name;
this.age = age;
System.out.println ("Person class constructor with 2 parameters");
}
}
Student class
View Code
public class Student extends Person {
private String school;
private String grade;
public Student () {
System.out.println ("Student class parameterless constructor");
}
public Student (String name, int age, String school) {
super (name, age);
this.school = school;
System.out.println ("Student class constructor with 3 parameters");
}
public Student (String name, int age, String school, String grade) {
this (name, age, school);
this.grade = grade;
System.out.println ("Student class constructor with 4 parameters");
}
}
Test class
View Code
public class Test {
public static void main (String [] args) {
System.out.println ("No parameter example:");
Student st1 = new Student ();
System.out.println ("---------------------------");
System.out.println ("3 parameter example:");
Student st2 = new Student ("zhangshan", 25, "mit");
System.out.println ("---------------------------");
//
System.out.println ("4 parameter example:");
Student st3 = new Student ("lisi", 24, "mit", "Graduate");
}
}
Example running results:
Example without parameters:
Person class parameterless constructor
Student class parameterless constructor
---------------------------
3 Examples of parameters:
Person class constructor with 2 parameters
Student class constructor with 3 parameters
---------------------------
4 Examples of parameters:
Person class constructor with 2 parameters
Student class constructor with 3 parameters
Student class constructor with 4 parameters