当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
SQL injection Less38 (stack injection)
Mysql environment installation under Linux (centos)
WindowInsetsControllerCompat简单使用
助力数字政府建设,中科三方构建域名安全保障体系
TFC CTF 2022 WEB Diamand WriteUp
To help the construction of digital government, the three parties of China Science and Technology build a domain name security system
How to import a Golang external package and use it?
Difference Between Stateless and Stateful
周总结
Keil nRF52832下载失败
Daily--Kali opens SSH (detailed tutorial)
【读书笔记->数据分析】02 数据分析准备
Input and output optimization
消息队列存储消息数据的MySQL表格
类和对象:上
IJCAI2022 | 代数和逻辑约束的混合概率推理
开源好用的 流程图绘制工具 drawio
日常--Kali开启SSH(详细教程)
thymeleaf iterates the map collection
Matlab / ArcGIS 处理GPM全球月均降水数据








