当前位置:网站首页>类和对象:中
类和对象:中
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 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。
- 注意:给的默认值不是初始化,是给的缺省值
边栏推荐
猜你喜欢

【Acwing】第62场周赛 题解

Shell common script: Nexus batch upload local warehouse script

(26)Blender源码分析之顶层菜单的关于菜单

数据分析(一)——matplotlib

Document management and tools in the development process
SQL27 View user details of different age groups

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

推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】

什么是动态规划,什么是背包问题
![[Reading Notes -> Data Analysis] 02 Data Analysis Preparation](/img/e7/258daf851746cb043f301437ee3bbe.png)
[Reading Notes -> Data Analysis] 02 Data Analysis Preparation
随机推荐
面试题:实现死锁
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
@JsonFormat(pattern="yyyy-MM-dd") time difference problem
thymeleaf iterates the map collection
【MATLAB项目实战】LDPC-BP信道编码
SQL注入 Less54(限制次数的SQL注入+union注入)
硬件设备计算存储及数据交互杂谈
Unity - by casting and cloning method dynamic control under various UGUI create and display
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
Difference between first and take(1) operators in NgRx
Interview Blitz 69: Is TCP Reliable?Why?
字符编码和浮点型计算精度丢失问题
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
date命令
Shell常用脚本:Nexus批量上传本地仓库脚本
Handwritten a simple web server (B/S architecture)
SQL injection Less42 (POST type stack injection)
[QNX Hypervisor 2.2用户手册]9.15 suppress
SQL注入 Less46(order by后的注入+rand()布尔盲注)