当前位置:网站首页>类和对象:中
类和对象:中
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 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。
- 注意:给的默认值不是初始化,是给的缺省值
边栏推荐
- "APIO2010" Patrol Problem Solution
- Daily--Kali opens SSH (detailed tutorial)
- 游戏安全03:缓冲区溢出攻击简单解释
- SQL injection Less42 (POST type stack injection)
- Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix
- 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#
- 基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
- 什么是动态规划,什么是背包问题
- Weekly Summary
- When can I use PushGateway
猜你喜欢
新产品如何进行网络推广?
推荐系统:常用评价指标总结【准确率、精确率、召回率、命中率、(归一化折损累计增益)NDCG、平均倒数排名(MRR)、ROC曲线、AUC(ROC曲线下的面积)、P-R曲线、A/B测试】
【ACM】2022.7.31训练赛
【1161. 最大层内元素和】
I don't know what to do with sync issues
什么是客户画像管理?
Network security - crack WiFi through handshake packets (detailed tutorial)
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#
[1161. The maximum sum of elements in the layer]
面试突击69:TCP 可靠吗?为什么?
随机推荐
10大主流3D建模技术
[QNX Hypervisor 2.2用户手册]9.16 system
TFC CTF 2022 WEB Diamand WriteUp
什么是客户画像管理?
cobaltstrike
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
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
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
(26)Blender源码分析之顶层菜单的关于菜单
「APIO2010」巡逻 题解
Interview Question: Implementing Deadlocks
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#
【1161. 最大层内元素和】
Components of TypeScript
HTC using official firmware as bottom bag made ROM brush card bag tutorial
日常--Kali开启SSH(详细教程)
Flutter教程之 01配置环境并运行demo程序 (教程含源码)
周总结
Keil nRF52832下载失败
"SDOI2016" Journey Problem Solution