当前位置:网站首页>12. constructor explanation, explicit, initialization list
12. constructor explanation, explicit, initialization list
2022-06-13 08:50:00 【zzyzxb】
One : Constructors : In class , There is a special member function , His name is the same as the class name , When we create the object of the class .
This special member function will be automatically called by the system . This member function , Call " Constructors ".
Because the constructor will be called automatically by the system , So we can simply understand it as : The purpose of a constructor is to initialize the data members of a class object .
class time{
public:
int hour;
int minute;
int second;
public:
time(int tmphour,int tmpmin, int tmpsec){
hour = tmphour;
minute = tmpmin;
second = tmpsec;
cout << " The constructor with three arguments is called " << endl;
}
public: time(){
hour = 12;
minute = 13;
second = 52;
cout << " An empty constructor was called " << endl;
}
public: time(int number){
hour = number ;
minute = 13;
second = 52;
cout << " Single argument constructor called " << endl;
}
public:
time(int tmphour,int tmpmin, int tmpsec=52){
hour = tmphour;
minute = tmpmin;
second = tmpsec;
};
(1) Constructor has no return value , This is also the special feature of constructors .
(2) Constructor cannot be called manually , Otherwise, the compilation will go wrong .
(3) Under normal circumstances , The constructor should be declared as public , Because when we create an object , The system calls the constructor for us , This shows that the constructor is a public function ,
Can be used by the system ( outside ) call . Because the default member of a class is a private member , So we must explain that the constructor is a public function , Otherwise, the object of this class cannot be created directly
(4) If there are multiple parameters in the constructor , Then we need to bring these parameters when creating the object .
time mytime = time(12,13,52); create a class object
time mytime2(12,13,52);
time mytime3 = time{12,13,52};
time mytime4{12,13,52};
time mytime5 = {12,13,52};
Two : Multiple constructors : There can be multiple constructors in a class , It can provide a variety of initialization methods for the creation of class objects . The number of parameters and the type of parameters are different .
time mytime = time(); create a class object
time mytime2;
time mytime3 = time{};
time mytime4{};
time mytime5 = {};
Object Copy
time mytime6; You can call the constructor
The following four objects do not call the traditional constructor , Follow up notes , What they call is actually a copy constructor .
time mytime7 = mytime6;
time mytime8(mytime6);
time mytime9{mytime6};
time mytime10 ={mytime6};
3、 ... and : Function default parameters
(1) Default values can only be placed in function declarations , Unless the function has no function declaration .
(2) When a default value is specified in a function with multiple arguments , All default parameters must appear to the right of non default parameters ,
Once a parameter begins to specify a default value , All parameters on its right must specify default values .
time mytime11 = time(12,13); create a class object
time mytime12(12,13);
time mytime13 = time{12,13};
time mytime14{12,13};
time mytime15 = {12,13};
Four : Implicit transformation and explicit
Compiling system , Did a lot of things we didn't know and didn't know in private .
time mytime40 = 14 The compiling system must have a behavior , hold 14 This number Transform to form a time type , Single argument constructor called .
time mytime = (12,13,14,15,16); Single argument constructor called .
void func1(time t1)
{
}
func1(16);
time mytime16 = {16}; We think this is normal , With one parameter 16, It can let the system know which constructor is called , Implicit type conversion .
time mytime17 = 16; Ambiguous writing , There is an implicit conversion of temporary objects .
Can the system be enforced , Explicitly require that constructors cannot do implicit type conversion ?
Sure , If the constructor declaration has explicit, Then this constructor can only be used for initialization and explicit type conversion .
class time1{
public:
int hour;
int minute;
int second;
public:
explicit time(int tmphour,int tmpmin, int tmpsec){
hour = tmphour;
minute = tmpmin;
second = tmpsec;
cout << " The constructor with three arguments is called " << endl;
}
};
time1 mytime = time1(12,13,52);
time1 mytime2(12,13,52);
time1 mytime3 = time1{12,13,52};
time1 mytime4{12,13,52};
time1 mytime5 = {12,13,52}; Implicit type conversion . There is a problem with this .
(1) For a single argument constructor , Generally stated as explicit , Unless you have a special reason .
5、 ... and : constructor initializer list
class time2{
public:
int hour;
int minute;
int second;
public:
time(int tmphour,int tmpmin, int tmpsec)
:hour(tmphour),minute(tmpmin),second(tmpsec)
{
cout << " The constructor with three arguments is called " << endl;
}
};
(1) Look professional , On the tall .
(2) More efficient .
边栏推荐
- Form exercise 2
- Brief description of port, domain communication port and domain service
- d3.js&nvd3. JS - how to set the y-axis range - d3 js & nvd3. js — How to set y-axis range
- Explanation of JS event loop mechanism and asynchronous tasks
- H5 mobile terminal adaptation
- Namespace in TS (1)
- Differences and uses among cookies, localstorage, sessionstorage, and application caching
- 天猫商品详情接口,天猫商品优惠券接口,天猫api接口,天猫价格监控接口,天猫比价接口,品牌维权接口,天猫销量api接口,接口代码可对接数据分析业务,品牌维权,比价业务,行业分析业务接口代码分享
- Invalid flex layout setting width
- JS array method
猜你喜欢
0. Quelques doutes au sujet de SolidWorks
Svg text stroke effect
À propos des principes de chiffrement et de décryptage RSA
1.SolidWorks各模块的学习顺序
Explanation of JS event loop mechanism and asynchronous tasks
turf. JS usage
CentOS installing MySQL and setting up remote access
Cesium achieves sunny, rainy, foggy, snowy and other effects
JS - array de duplication in the array object case
[pychart pit stepping record] prompt after configuring remote operation, [errno 2] no such file or directory
随机推荐
Tmall product details interface, tmall product coupon interface, tmall API interface, tmall price monitoring interface, tmall price comparison interface, brand rights protection interface, tmall sales
Cesium view switching, locating, reading files, building data sources, entity control, model control, etc
Input prohibit copy and paste
How app inventor accesses resource files in assets directory
[notes] like the solution to the problem of slow query (index + explicitly specifying query fields)
3、 JS notes
Gbase 8A v95 vs v86 compression strategy analogy
PHP wechat special merchant incoming V3 packaging interface
Browser render passes
Docker installing MySQL local remote connection docker container MySQL
JS wrapper object
情绪处理技巧
Buuctf web (III)
DIY无人机(匿名拓控者P2+F330机架)
DIY UAV (anonymous controller p2+f330 rack)
GBase 常见网络问题及排查方法
JS array using the reduce() method
JS - simple ATM of the for cycle case
What is the difference between getfullyear() and getyear()
JS array method