当前位置:网站首页>类和对象:中
类和对象:中
2022-07-31 23:44:00 【懒惰的bit】
类的6个默认成员函数
- 如果一个类中什么成员都没有,简称为空类。
- 空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。
默认成员函数:用户没有显式实现,编译器会自动生成的成员函数称为默认成员函数。

构造函数
- 构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象。
构造函数的特性
- 函数名与类名相同。
- 无返回值的概念
- 对象实例化时编译器自动调用对应的构造函数。
- 构造函数可以重载。
- 不传参也可以调用
#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;
}- 在使用类的过程中,我们常常需要对类进行初始化,自己对类进行初始化有点麻烦,而且我们有可能会忘记初始化,C++中就出现了默认构造函数
#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;
}- 创建A对象的时候就进行了初始化,也可以自己传值进行初始化,
#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;
}- 这里编译不会通过的,无参的构造函数,半缺省/全缺省的构造函数,默认构造函数都叫构造函数,它们只能出现其中的一个,
#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;
}
- 这种就是错误的使用构造函数,没有合适的默认构造函数可用
关于编译器生成的默认成员函数
#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++中规定默认生成构造函数
- a:内置类型成员不做处理
- b:自定义类型成员回去调用他的默认构造函数
- C++中的这个设计就会导致一个问题,
- 比如说在这段代码中,生成d1的时候就会调用它的默认构造函数,而_year和_month和_day都是内置类型,只用_t是自定义类型,
- 对于_t这个对象又会去调用它的默认构造函数,_hour和 _minute和 _second都是内置类型,但是又会因为内置类型成员不做处理。导致最后调用了默认构造函数之后还是随机值

- C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。
- 注意:给的默认值不是初始化,是给的缺省值
边栏推荐
猜你喜欢

Network security - crack WiFi through handshake packets (detailed tutorial)

数据分析(一)——matplotlib

Flink 1.13(八)CDC

cobaltstrike
Dry goods | 10 tips for MySQL add, delete, change query performance optimization

SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development

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

【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀

/etc/sysconfig/network-scripts configure the network card

游戏安全03:缓冲区溢出攻击简单解释
随机推荐
基于mysql的消息队列设计
2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
LeetCode 第 304 场周赛
/etc/sysconfig/network-scripts 配置网卡
【MATLAB项目实战】LDPC-BP信道编码
「SDOI2016」征途 题解
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
The difference between /usr/local/bin and /usr/bin
[QNX Hypervisor 2.2用户手册]9.15 suppress
TFC CTF 2022 WEB Diamand WriteUp
Design of Fire and Anti-theft System Based on Single Chip GSM
IPD process terminology
Keil nRF52832 download failed
UOS统信系统 - WindTerm使用
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
Weekly Summary
什么时候可以使用 PushGateway
MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
Learn about C# anonymous methods