当前位置:网站首页>Constructor of member function
Constructor of member function
2022-07-28 04:30:00 【Magic tea】
Catalog
The properties of constructors :
There can only be one constructor :
The concept of constructors :
A constructor is a special member function , Same name as class name , It is automatically called by the compiler when creating class type objects , Ensure that each data member has an appropriate initial value , And only called once in the object's life cycle .
The properties of constructors :
(1) The function name is the same as the class name .
(2) No return value .
(3) The compiler automatically calls the corresponding constructor .
(4) Constructor can overload .
Let's take an example directly here
#include<iostream>
using namespace std;
class Data
{
public:
Data()
{
cout << "Date()" << this << endl;
}
void InitData(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void PrintfData()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Data d1,d2;
d1.InitData(2022,5,21);
d1.PrintfData();
return 0;
}
So the result is :

There can only be one constructor :
Parameterless constructors and fully default constructors are called default constructors , And the default constructor can only have one .
Let's take a wrong case :
#include<iostream>
using namespace std;
class Data
{
public:
Data()
{
cout << "Date()" << this << endl;
}
Data()
{
_year = year;
_month = month;
_day = day;
}
void InitData(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void PrintfData()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Data d1
return 0;
}
In the above code , There are two default constructors , Because constructors without parameters and full default constructors are considered as default constructors , So , Now there are two constructors , The compiler doesn't know which constructor to call , So , You're going to report a mistake , So we can delete one .
About compiler generated default member functions , A lot of people have doubts : In the case that we don't implement constructors , The compiler generates the default constructor . But it seems that the default constructor is useless ? Object calls the compiler generated default constructor , But the object year/month/_day, Still random . In other words, the default constructor generated by the compiler is useless here ?
answer :C++ Divide types into built-in types ( Basic types ) And custom types . A built-in type is a type that has been defined by the syntax : Such as int/char..., Custom type is what we use class/struct/union Self defined type , Look at the following program , I found that the compiler will generate a default constructor for custom type members _t Call its default member function
class Time
{
public:
Time()
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// Basic types ( Built in type )
int _year;
int _month;
int _day;
// Custom type
Time _t;
};
int main()
{
Date d;
return 0;
}What does that mean , The compiler doesn't care int,char This basic type , Instead, I will ignore custom types

This is the result of the output
边栏推荐
- VAE generation model (with VAE implementation MNIST code)
- Zhejiang University and other recent review papers on deep learning new drug design
- CMake使用基础汇总
- Reading of the paper "attentional encoder network for targeted sentimental classification"
- Kotlin——函数
- Important SQL server functions - other functions
- Reading the paper "learning span level interactions for aspect sentimental triple extraction"
- Information system project manager (2022) - key content: Knowledge Management (15)
- Elementary level of C language -- while, for, do while
- 空间复杂度计算超全整理!!(一起手撕复杂度计算
猜你喜欢

Elementary level of C language -- while, for, do while

idea2022更改本地仓库,配置阿里云中央仓库

Reading of the paper "attentional encoder network for targeted sentimental classification"

Glusterfs file is not mounted, permission: R-S

Use Baidu developer tool 4.0 to build a dedicated applet IDE

Information system project manager (2022) - key content: Project Contract Management (13)

Information system project manager (2022) - key content: Project Procurement Management (12)

【YOLOv5实战5】基于YOLOv5的交通标志识别系统-YOLOv5整合PyQt5
![[untitled]](/img/e9/4b00244b67af5ddaa3f35baa1ac968.png)
[untitled]

idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet
随机推荐
【sylar】框架篇-Chapter22-辅助模块
Shanghai Telecom released public computing services and signed the action plan of "Joint Innovation Center for intelligent computing applications" with Huawei and other partners
Idea2022 change the local warehouse and configure Alibaba cloud central warehouse
Efficient practice intelligent analysis of massive log data in aviation industry
gerrit操作-回退掉某个patch_set
Kingbasees Security Guide for Jincang database -- 5.1. database transmission security
[yolov5 practice 5] traffic sign recognition system based on yolov5 -yolov5 integration pyqt5
[day03] process control statement
重要的 SQL Server 函数 - 其他函数
Information system project manager (2022) - key content: Project Risk Management (11)
pl/sql之各参数详解(“箱子模型“)
Docking with Hang Seng express ― dolphin DB NSQ plug-in tutorial
idea2022更改本地仓库,配置阿里云中央仓库
Information system project manager (2022) - key content: intellectual property rights and standards and specifications (22)
Simple summary of Modbus Protocol
【sylar】实战篇-基于 redis 的参数查询服务
[coding and decoding] Huffman coding and decoding based on Matlab GUI [including Matlab source code 1976]
Harmony's Application on the shelves reported an error. The solution of "please use the API of the released version to develop the application and apply for listing"
The State Administration of market supervision exposes typical cases of food safety
【实战】使用 Web Animations API 实现一个精确计时的时钟