In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about what classes and objects are in C++ language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Constructor (constructor) 1. Brief introduction to constructor
In C++ language, the constructor is a special member function with the same class name.
When the class object is created, the constructor is called automatically to complete the initialization of the class object. The class object itself is a variable, and the members of the objects created on the stack and heap are initialized to random values; for objects created in the static store, the members of the objects are initialized to 0.
2. Definition of constructor
The syntax for the constructor declaration is as follows:
Classname (parameters)
A constructor with no parameters is called a no-parameter constructor. When there is no constructor defined in the class (including the copy constructor), the compiler provides a no-parameter constructor by default, and its function body is empty. If a constructor is already defined in the class (including the copy constructor), the compiler no longer provides the default no-parameter constructor.
# include using namespace std;class Person {public: const char* name; int age; void print () {printf ("My name is% s, iTunm is% d years old.\ n", name,age);}}; int main (int argc, char* argv []) {Person pincedard / compiler provides the default no-argument constructor p.name = "Bauer"; p.age = 30; Person p1 = p / / the compiler provides the default copy constructor p1.print (); return 0;}
In the above code, the compiler provides the default no-parameter constructor and copy constructor.
# include using namespace std;class Person {public: const char* name; int age; Person (const Person& another) {name = another.name; age = another.age;} void print () {printf ("My name is% s, Isimm is% d years old.\ n", name,age);} Int main (int argc, char * argv []) {/ / error: no matching function for call to 'Person::Person ()' Person parebank / the compiler no longer provides the default no-parameter constructor p.name = "Bauer"; p.age = 30; Person p1 = p; p1.print (); return 0;}
In the above code, a copy constructor is defined in the class, the compiler will no longer provide the default no-parameter constructor, the constructor call cannot be found when creating the Person object, and the compiler reports an error.
3. Rules of constructor
The rules for the constructor are as follows:
A. it is called automatically when the object is created to complete the initialization related work.
B, no return value, same as the class name, no parameter by default, can be overloaded, and the default parameter can be used.
C. once the constructor is displayed, the C++ compiler no longer implements the default constructor for the class.
The call of the constructor is called according to the matching rules of the overloaded function.
4. Initialize the parameters of the constructor classname::classname (): member1 (v1), member2 (v2),... {/ / code}
The initialization order of the members in the initialization list of the constructor is the same as the declaration order of the members in the class, regardless of the position of the members in the initialization list. The initialization list is executed before the function body of the constructor.
# include using namespace std;class Value {private: int iTractPublic: Value (int value) {I = value; cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.