当前位置:网站首页>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
边栏推荐
- 周总结
- The role of /etc/resolv.conf
- Pylint检查规则中文版
- Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
- SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
- TFC CTF 2022 WEB Diamand WriteUp
- zeno使用方法笔记
- SQL注入 Less38(堆叠注入)
- NIO编程
- Flutter教程之 01配置环境并运行demo程序 (教程含源码)
猜你喜欢

Shell common script: Nexus batch upload local warehouse script

TFC CTF 2022 WEB Diamand WriteUp

Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments

高等代数_证明_任何矩阵都相似于一个上三角矩阵

C# Rectangle基本用法和图片切割
![[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories](/img/7a/278ffada1cc660e7f5c2d7c66fa38e.png)
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories

力扣二叉树

cobaltstrike

zeno使用方法笔记

基于单片机GSM的防火防盗系统的设计
随机推荐
Interview Question: Implementing Deadlocks
编译型语言和解释型语言的区别
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
SQL injection Less46 (injection after order by + rand() Boolean blind injection)
LeetCode--打家劫舍问题
Thinking and Implementation of Object Cache Service
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
Web API Introduction and Types
硬件设备计算存储及数据交互杂谈
EntityFramework保存到SQLServer 小数精度丢失
开源好用的 流程图绘制工具 drawio
UOS - WindTerm use
自动化机器学习pycaret: PyCaret Basic Auto Classification LightGBM
Interview Blitz 69: Is TCP Reliable?Why?
Flutter教程之四年开发经验的高手给的建议
In 2022, the latest eight Chongqing construction members (electrical construction workers) simulation question bank and answers
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
The difference between /usr/local/bin and /usr/bin
Keil nRF52832下载失败
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法