当前位置:网站首页>类和对象:中
类和对象:中
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 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。
- 注意:给的默认值不是初始化,是给的缺省值
边栏推荐
- SQL injection Less54 (limited number of SQL injection + union injection)
- Design of Fire and Anti-theft System Based on Single Chip GSM
- 2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
- 消息队列存储消息数据的MySQL表格
- thymeleaf迭代map集合
- "SDOI2016" Journey Problem Solution
- The role of /etc/resolv.conf
- 什么是动态规划,什么是背包问题
- Flutter教程之四年开发经验的高手给的建议
- 助力数字政府建设,中科三方构建域名安全保障体系
猜你喜欢
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
SVN server construction + SVN client + TeamCity integrated environment construction + VS2019 development
Mysql environment installation under Linux (centos)
【ACM】2022.7.31训练赛
What is customer profile management?
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
网络安全--通过握手包破解WiFi(详细教程)
Binary tree non-recursive traversal
基于mysql的消息队列设计
Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix
随机推荐
消息队列存储消息数据的MySQL表格
2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
Recommendation system: Summary of common evaluation indicators [accuracy rate, precision rate, recall rate, hit rate, (normalized depreciation cumulative gain) NDCG, mean reciprocal ranking (MRR), ROC
What is customer profile management?
lua入门案例实战1234定义函数与标准函数库功能
SQL注入 Less46(order by后的注入+rand()布尔盲注)
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
SQL注入 Less38(堆叠注入)
#yyds干货盘点# 面试必刷TOP101:链表中环的入口结点
EntityFramework保存到SQLServer 小数精度丢失
什么时候可以使用 PushGateway
Unity - LineRenderer show a line
标段参数说明
NIO编程
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)
Daily--Kali opens SSH (detailed tutorial)
不知道该怎么办的同步问题
命名实体识别-模型:BERT-MRC
vim的基本使用-命令模式
Difference between first and take(1) operators in NgRx