当前位置:网站首页>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
边栏推荐
- Classes and Objects: Above
- 谷歌『云开发者速查表』;清华3D人体数据集;商汤『通用视觉框架』公开课;Web3极简入门指南;高效深度学习免费书;前沿论文 | ShowMeAI资讯日报
- 字符编码和浮点型计算精度丢失问题
- 浏览器下载快捷方式到桌面(PWA)
- SQL注入 Less42(POST型堆叠注入)
- SQL injection Less54 (limited number of SQL injection + union injection)
- 面试突击69:TCP 可靠吗?为什么?
- Design of Fire and Anti-theft System Based on Single Chip GSM
- Handwritten a simple web server (B/S architecture)
- NgRx 里 first 和 take(1) 操作符的区别
猜你喜欢
Kyoto University:Masaki Waga | 黑箱环境中强化学习的动态屏蔽
高等代数_证明_任何矩阵都相似于一个上三角矩阵
Shell常用脚本:Nexus批量上传本地仓库脚本
[MATLAB project combat] LDPC-BP channel coding
Interview assault 69: TCP reliable?Why is that?
NIO programming
一文概述:VPN的基本模型及业务类型
Kyoto University: Masaki Waga | Dynamic Masking for Reinforcement Learning in Black Box Environments
Shell common script: Nexus batch upload local warehouse script
《ArchSummit:时代的呐喊,技术人听得到》
随机推荐
Keil nRF52832下载失败
Named Entity Recognition - Model: BERT-MRC
消息队列消息存储设计(架构实战营 模块八作业)
一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
内核对设备树的处理
Matlab / ArcGIS 处理GPM全球月均降水数据
EntityFramework保存到SQLServer 小数精度丢失
/etc/sysconfig/network-scripts configure the network card
SQL注入 Less38(堆叠注入)
What is customer profile management?
Carefully summarize thirteen suggestions to help you create more suitable MySQL indexes
虹科分享|如何用移动目标防御技术防范未知因素
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
SQL注入 Less42(POST型堆叠注入)
类和对象:中
Keil nRF52832 download failed
精心总结十三条建议,帮你创建更合适的MySQL索引
WindowInsetsControllerCompat is simple to use
Notes on how to use zeno
【MATLAB项目实战】LDPC-BP信道编码