English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
++In this article, you will learn C ++of the constructor type in it. You will learn what a constructor is, how to create it, and C
The constructor is a special type of member function that is createdObjectIt will be automatically initialized.
The compiler identifies the given member function as a constructor by its name and return type.
The constructor has the same name as the class and has no return type. Similarly, the constructor is always public.
... .. ... class temporary { private: int x; float y; public: // Calculate and display the area of the rectangle. temporary(): x(5), y(5y =5) { // Constructor body } ... .. ... }; int main() { Temporary t1; ... .. ... }
The above program shows the defined constructor, which has no return type and the name is the same as the class.
In the above code, temporary() is a constructor.
When creating an object of the class, the constructor temporary will be automatically called, and x will be initialized to5,然后y初始化为5y =5.
Then y is initialized to
You can also initialize the data members inside the constructor body in the following way. However, this method is not recommended. { temporary() 5; x = 5y =5; } // .
using the constructor10Assuming you are dealing with
0 Person objects, and the default value of the data member age is 0. Manually initializing all objects will be a very tedious task.
On the contrary, you can define a constructor that initializes age to 0. Then, what you need to do is create a Person object, and the constructor will automatically initialize age.
These situations often occur when dealing with object arrays.
The constructor method
#include <iostream> using namespace std; class Area { private: int length; int breadth; public: // Calculate and display the area of the rectangle. Area(): length(5), breadth(2Constructor void GetLength() { ){} cin >> length >> breadth; } cout << "Enter the length and width: "; * int AreaCalculation() { return (length void DisplayArea(int temp) { breadth);} } }; int main() { Area A1, A2; int temp; A1cout << "Area: " << temp; temp = A1.AreaCalculation(); A1.DisplayArea(temp); .GetLength(); temp = A2.AreaCalculation(); A2.DisplayArea(temp); return 0; }
cout << endl << "The default area when no values are obtained from the user is:" << endl;
In this program, create the class Area to handle functions related to area. It has two data members length and breadth.5Defined an initialization of length2and breadth to
The constructor.
We also have three additional member functions GetLength(), AreaCalculation(), and DisplayArea(), which respectively obtain the length from the user, calculate the area, and display the area.1When creating object A2and A5and2.
Due to the relationship with the constructor, the lengths (length) and (breadth) widths of these two objects are initialized separately to1Then, call the member function GetLength(), which gets the length of object A1The length (length) and (breadth) width values. This changes the object A
Then, calculate the length (length) and (breadth) width of object A by calling the AreaCalculation() function1Store the area in the variable temp, and finally display it.
For object A2No data is required from the user. Therefore, the length (length) and (breadth) width are kept5and2.
Then, calculate and display A2The area is10.
Output Result
Enter the length and width separately: 6 7 Area: 42 The default area when no values are obtained from the user is: Area: 10
The constructor can also be similar toFunction overloading.method overloading.
Overloaded constructors have the same name (the name of the class), but different numbers of parameters.
The specific constructor to be called is determined by the number and type of parameters passed.
Since there are multiple constructors, parameters should also be passed when creating an object.
// Source code demonstrates the working principle of overloaded constructors #include <iostream> using namespace std; class Area { private: int length; int breadth; public: // Parameterless Constructor Area(): length(5), breadth(2{ // There is a constructor with two parameters Area(int l, int b): length(l), breadth(b){} void GetLength() { cout << "Please enter the length and width: "; cin >> length >> breadth; } int AreaCalculation() { return length} * breadth;} void DisplayArea(int temp) { cout << "Area: " << temp << endl; } }; int main() { Area A1, A2(2, 1); int temp; cout << "Default area when no parameters are passed. << endl; temp = A1.AreaCalculation(); A1.DisplayArea(temp); cout << "("2,1when passed as a parameter, the area is. << endl; temp = A2.AreaCalculation(); A2.DisplayArea(temp); return 0; }
For object A1when no parameters are passed when creating an object.
Therefore, the constructor without parameters will be called, which initializes the length (length) to5the width (breadth) is initialized to2Therefore, object A1The area will be10.
For object A2when creating an object,2and1as parameters.
Therefore, the constructor with two parameters will be called, which initializes the length (length) to l (in this case,2), and initialize the width (breadth) to b (in this case,1Therefore, object A2The area will be2.
Output Result
Default area when no parameters are passed. Area: 10 (2,1when passed as a parameter. Area: 2
An object can be initialized with another object of the same type. This is the same as copying the contents of one class to another.
In the above program, if you want to initialize an object A3, so that it contains the same value as A2To perform the same operation, you can do this:
.... int main() { Area A1, A2(2, 1); // Put A2content to A3 Area A3(A2); OR, Area A3 = A2; }
You might think that you need to create a new constructor to perform this task. However, no additional constructor is needed. This is because the copy constructor is already built into all classes by default.