当前位置:网站首页>Classes and Objects: Medium
Classes and Objects: Medium
2022-08-01 00:03:00 【lazy bit】
类的6个默认成员函数
- 如果一个类中什么成员都没有,简称为空类.
- 空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数.
默认成员函数:用户没有显式实现,编译器Member functions that are automatically generatedCalled the default member function.
构造函数
- 构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象.
构造函数的特性
- 函数名与类名相同.
- No concept of return value
- 对象实例化时编译器自动调用对应的构造函数.
- 构造函数可以重载.
- It can also be called without passing parameters
#include<iostream>
using namespace std;
typedef int DataType;
class Date
{
public:
void Init(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date A;
A.Init(2022,7,27);
A.Print();
return 0;
}
- 在使用类的过程中,我们常常需要对类进行初始化,Initializing the class yourself is a bit cumbersome,And we risk forgetting to initialize,C++The default constructor appears in
#include<iostream>
using namespace std;
typedef int DataType;
class Date
{
public:
Date()
{
_year = 2022;
_month = 7;
_day = 27;
}
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date A;
A.Print();
return 0;
}
- 创建Aobject is initialized,You can also pass a value to initialize it yourself,
#include<iostream>
using namespace std;
typedef int DataType;
class Date
{
public:
Date()
{
_year = 1900;
_month = 1;
_day = 1;
}
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
return 0;
}
- Compilation will not pass here,无参的构造函数,半缺省/全缺省的构造函数,Default constructors are called constructors,它们Only one of them can appear,
#include<iostream>
using namespace std;
typedef int DataType;
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
return 0;
}
- This is the wrong use of constructors,没有合适的默认构造函数可用
关于编译器生成的默认成员函数
#include<iostream>
using namespace std;
typedef int DataType;
class Time
{
public:
/*Time()
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}*/
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0;
}
- 在C++The default generated constructor is specified in
- a:内置类型成员不做处理
- b:The custom type member goes back and calls his default constructor
- C++This design in can cause a problem,
- For example in this code,生成d1will call its default constructor,而_year和_month和_day都是内置类型,只用_t是自定义类型,
- 对于_tThis object will then call its default constructor,_hour和 _minute和 _second都是内置类型,But because of内置类型成员不做处理.As a result, it is still a random value after the default constructor is finally called
- C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值.
- 注意:The default value given is not initialization,is the default value given
边栏推荐
猜你喜欢
类和对象:上
Google Earth Engine——Error: Image.clipToBoundsAndScale, argument ‘input‘: Invalid type的错误解决
Matlab / ArcGIS 处理GPM全球月均降水数据
面试突击69:TCP 可靠吗?为什么?
编译型语言和解释型语言的区别
Drawing process of hand-drawn map of scenic spots
手写一个简单的web服务器(B/S架构)
[MATLAB project combat] LDPC-BP channel coding
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
Likou Binary Tree
随机推荐
thymeleaf迭代map集合
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
2022年最新重庆建筑八大员(电气施工员)模拟题库及答案
Thinking and Implementation of Object Cache Service
Difference Between Stateless and Stateful
精心总结十三条建议,帮你创建更合适的MySQL索引
NIO programming
类和对象:上
Named Entity Recognition - Model: BERT-MRC
基于mysql的消息队列设计
Keil nRF52832 download failed
Carefully organize 16 MySQL usage specifications to reduce problems by 80% and recommend sharing with the team
NIO编程
Automated machine learning pycaret: PyCaret Basic Auto Classification LightGBM
ICML2022 | 深入研究置换敏感的图神经网络
pycaret源码分析:下载数据集\Lib\site-packages\pycaret\datasets.py
谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
C# Rectangle基本用法和图片切割
南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强
[MATLAB project combat] LDPC-BP channel coding