当前位置:网站首页>类和对象:中
类和对象:中
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 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。
- 注意:给的默认值不是初始化,是给的缺省值
边栏推荐
- Payment module implementation
- SQL injection Less47 (error injection) and Less49 (time blind injection)
- 了解下C# 匿名方法
- 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
- 什么时候可以使用 PushGateway
- Interview Question: Implementing Deadlocks
- SQL injection Less54 (limited number of SQL injection + union injection)
- MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据
- Keil nRF52832下载失败
- C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
猜你喜欢
不知道该怎么办的同步问题

手写一个简单的web服务器(B/S架构)

【MATLAB项目实战】LDPC-BP信道编码

Shell常用脚本:Nexus批量上传本地仓库脚本

Drawing process of hand-drawn map of scenic spots

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#

嵌入式开发没有激情了,正常吗?

【读书笔记->数据分析】02 数据分析准备

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

消息队列消息存储设计(架构实战营 模块八作业)
随机推荐
Drawing process of hand-drawn map of scenic spots
SQL注入 Less38(堆叠注入)
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
「SDOI2016」征途 题解
Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
信奥学习规划 信息学竞赛之路(2022.07.31)
IPD流程专业术语
一行代码解决CoreData托管对象属性变更在SwiftUI中无动画效果的问题
Input and output optimization
Advanced Algebra _ Proof _ Any matrix is similar to an upper triangular matrix
thymeleaf iterates the map collection
(26)Blender源码分析之顶层菜单的关于菜单
了解下C# 匿名方法
编写方法将一个数组扁平化并且去重和递增排序
date命令
日常--Kali开启SSH(详细教程)
编译型语言和解释型语言的区别
/etc/sysconfig/network-scripts configure the network card
新产品如何进行网络推广?
@JsonFormat(pattern="yyyy-MM-dd") time difference problem