Ah ~ The soft exam is coming ~ Fear ~ Review. ^_^ Constructor is the key and difficulty in C ++. Constructors are used to create objects.The default constructor is displayed only when no display is prompted for initialization. The default constructor can be used for the following declaration: Example Test_A; // Example is a class. // This statement declares an object Test_A. Note: When and only when there is no Constructor. C ++ automatically adds the default constructor. Here is a hypothesis. suppose you have provided a non-default constructor. this constructor is not defined. the above statement: Example Test_A; will cause an error. the cause of the error is: Creating uninitialized objects is not allowed. from this sentence, we can also see the usefulness of the constructor. to put it bluntly, point constructors are used to create and initialize object objects. If a programmer defines a non-default constructor, how can he make the preceding statement legal? Two methods to define the default constructor: 1. Define all parameters of an existing constructor. Example: example (int a = 0, int B = 0, int c = 0 ); 2. Define a constructor without parameters. (This is actually a function overload. The constructor overload is automatically added to C ++) Example: Example () { // You can initialize the members. Int a = 0, B = 0, c = 0; } This is a standard writing method. Of course you can write it like this: Example (): a (0), B (0), c (0) {}// it looks handsome. The two are equivalent. Only one default constructor can exist in a class. That is to say, the above two methods cannot be performed simultaneously. Note: The constructor does not return the data type or even void. That is to say, the constructor does not declare the type. Good program style: when defining the default constructor, you should initialize the object to ensure that there is a known. Reasonable. Valid value. The default constructor usually provides an implicit initial value to all members. Any of the above methods can be used to declare object variables without the need to initialize the display. Example: Example Test_A; Example Test_ B = Example (); Example * ptr = new Example; For the first method: it cannot be defined like this: Assume that a, B, and c are private members of the class. so. example (int a = 0, int B = 0, int c = 0) is incorrect. because function parameters cannot represent class members. it is the value assigned to the class member. therefore. function parameters cannot be the same as class members. Good program style: to avoid this situation, you can add the m _ prefix before the class members to distinguish. m_a, m_ B, m_c; For Example: Example Test_C ("concrete conglomerate"); // This statement calls a non-default constructor, that is, the constructor that accepts the parameter; Example Test_D (); // This statement states that Test_D is a function that returns the Example object. Example Test_E ;//This statement calls the default constructor. Do not use parentheses. When we define an object, for example, Explam Test_C. At this time, the default constructor will be called. In <<On to C ++>This book is also quite detailed: [Quote start]How to define constructor member function In class definitions, default constructor functions stand apart from other member functions in three ways: 1. The default constructor function's name is the same as the name of class 2. The default constructor function has no return-value data type 3. The default constructor function cannot have a parameter. [Quote end]Now following a class: Const double pi = 3.14; Class Test_A { Public: Double radius, length; Test_A () {radius = 2.0; length = 4.0 ;} // Constructor with two parameters Test_A (double r, double l) {radius = r; length = l ;} Double volume () {return pi * radius * length ;} } Qustions: 1. Is it worng? 2. Where is the private member? --- Next anonymous constructor and copy constructor and so on. To be continue ..
This article is from the "egg" blog and cannot be reproduced!