当前位置:网站首页>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
边栏推荐
猜你喜欢

Drawing process of hand-drawn map of scenic spots

浏览器下载快捷方式到桌面(PWA)

力扣二叉树

南方科技大学:Xiaoying Tang | AADG:视网膜图像分割领域泛化的自动增强

TFC CTF 2022 WEB Diamand WriteUp

Shell common script: Nexus batch upload local warehouse script

Matlab/ArcGIS processing GPM global monthly precipitation data

一文带你了解 Grafana 最新开源项目 Mimir 的前世今生

面试突击69:TCP 可靠吗?为什么?

【Acwing】第62场周赛 题解
随机推荐
Binary tree traversal non-recursive program -- using stack to simulate system stack
信奥学习规划 信息学竞赛之路(2022.07.31)
Named Entity Recognition - Model: BERT-MRC
/etc/sysconfig/network-scripts 配置网卡
面试突击69:TCP 可靠吗?为什么?
一体化步进电机在无人机自动机场的应用
什么是动态规划,什么是背包问题
二叉树遍历非递归程序 -- 使用栈模拟系统栈
C# Rectangle基本用法和图片切割
基于单片机GSM的防火防盗系统的设计
编写方法将一个数组扁平化并且去重和递增排序
景区手绘地图的绘制流程
ICML2022 | 深入研究置换敏感的图神经网络
Difference Between Stateless and Stateful
《ArchSummit:时代的呐喊,技术人听得到》
博弈论(Depu)与孙子兵法(42/100)
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
TFC CTF 2022 WEB Diamand WriteUp
How to import a Golang external package and use it?
Thinking and Implementation of Object Cache Service