当前位置:网站首页>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 .
边栏推荐
- Verify the word limit of textarea input box. Only prompt but no submission limit
- Implement authentication code login and remember password (cookie)
- Yarn package management tool
- JS ask for the day of the year
- [leetcode weekly race record] record of the 80th biweekly race
- Review one flex knowledge point every day
- Taobao commodity historical price interface / commodity historical price trend interface code docking and sharing
- Problèmes et traitement du disque gbase 8a
- Software Architecture Overview knowledge
- Process and scheduled task management
猜你喜欢

5. Attribute selector

Invalid flex layout setting width

Sky background map, navigation page lovefanfan top

ADT Google browser plug-in ad Terminator

Screenshot of cesium implementation scenario

Print an array clockwise

1.SolidWorks各模块的学习顺序

Vscode define code block -- define cursor position

System analysis - detailed description

Animation through svg
随机推荐
Knowledge points related to system architecture 2
d3.js&nvd3. JS - how to set the y-axis range - d3 js & nvd3. js — How to set y-axis range
WARNING:tornado. access:404 GET /favicon. ICO (172.16.8.1) 1.84ms [with static file settings]
Problèmes et traitement du disque gbase 8a
Animation through svg
Custom exception class myexception
JS array using the reduce() method
Docker installing MySQL local remote connection docker container MySQL
System analysis - detailed description
JS string method
容器概念和云原生
6、 JS naming rules and specifications
Wrap dynamically created child elements in dynamically created structures
Invalid flex layout setting width
VBA format word (page, paragraph, table, picture)
Diversified tables through TL table row consolidation
Buuctf web (VII)
How to save the video of wechat video number locally?
Print an array clockwise
京东商品详情接口,京东详情页接口,宝贝详情页接口,商品属性接口,商品信息查询,商品详细信息接口,h5详情,京东APP详情,京东api接口,京东历史价格数据接口代码对接分享