当前位置:网站首页>类和对象—6个默认成员函数
类和对象—6个默认成员函数
2022-07-30 09:58:00 【右列左行】
如果一个类中什么成员都没有,简称为空类。空类中什么都没有吗?并不是的,任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数。 --题记
第一站 构造函数与析构函数
目录
一、构造函数
1.定义
2 特性
4. 构造函数可以重载
class Date
{
public:
// 1.无参构造函数
Date()
{}
// 2.带参构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
void TestDate()
{
Date d1; // 调用无参构造函数
Date d2(2015, 1, 1); // 调用带参的构造函数
// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
Date d3();
}
class Date
{
private:
int _year;
int _month;
int _day;
};
void Test()
{
//没有定义构造函数,对象也可以创建成功,因此此处调用的是编译器生成的默认构造函数
Date d;
}
ps:下面这段代码能正常运行吗?
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;
};
// 以下测试函数能通过编译吗?
void Test()
{
Date d1;
}
答案是:不能
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;
}
8. 成员变量的命名风格
//我们一般都建议这样
//_data m_data
class Date
{
public:
Date(int year)
{
_year = year;
}
private:
int _year;
};
二、析构函数
1 概念
2 特性
class SeqList
{
public:
//构造函数
SeqList(int capacity = 10)//全缺省参数
{
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
~SeqList()//析构函数
{
if (_pData)
{
free(_pData); // 释放堆上的空间
_pData = NULL; // 将指针置为空
_capacity = 0;
_size = 0;
}
}
private:
int* _pData;
size_t _size;
size_t _capacity;
};
class String
{
public:
String(const char* str = "jack")
{
_str = (char*)malloc(strlen(str) + 1);
strcpy(_str, str);
}
~String()
{
cout << "~String()" << endl;
free(_str);
}
private:
char* _str;
};
class Person
{
private:
String _name;
int _age;
};
int main()
{
Person p;
return 0;
}
小技巧:不好观察进入类的过程时,在恰当的位置打断点
三、析构函数和构造函数的调用顺序
观察下面这组代码,你能判断出运行结果吗
#pragma warning(disable : 4996)
#include<iostream>
#include<assert.h>
using namespace std;
class A
{
public:
A(int a)
:_a(a)
{
cout << "A(int a)" << endl;
}
~A()
{
cout << "~A()" << endl;
}
private:
int _a;
};
A aa3(0);
int main()
{
static A aa0(0);
A aa1(1);
A aa2(2);
static A aa4(4);
return 0;
}
在之前我们有提到,变量的生命周期有存储位置决定,当然,要看这个程序的运行结果,我们还要补充一个知识点:先定义的后析构
定义的顺序不难理解,其实就是按照程序的顺序正常去走就OK,但是析构的话,就有个小知识点啦。
我们知道 局部变量在栈上创建,static修饰的变量在静态区创建,所以当函数调用结束时,局部变量被销毁,而当整个程序结束时,static修饰的变量才销毁。
不知道这样解释怎么样,下面这段代码或许可以帮助加深对这个知识点的理解
class A
{
public:
A(int a)
:_a(a)
{
cout << "A(int a)" << endl;
}
~A()
{
cout << "~A()" << endl;
}
private:
int _a;
};
void f()
{
static A aa0(0);
A aa1(1);
A aa2(2);
static A aa(4);
}
int main()
{
f();
f();//小心喔~
return 0;
}
边栏推荐
- Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- MFCC to audio, the effect should not be too funny >V
- 图像去噪——Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images
- 系统设计精选 | 基于FPGA的CAN总线控制器的设计(附代码)
- 唯物辩证法-条件论
- 105. Construct binary tree from preorder and inorder traversal sequence (video explanation!!)
- 2022年顶会accepted papers list
- [Qualcomm][Network] 网络拨号失败和netmgrd服务分析
- debian10 install djando
- 【C和指针第七章】可变参数列表
猜你喜欢
Flink_CDC construction and simple use
Quick Start Tutorial for flyway
Multithreading--the usage of threads and thread pools
A near-perfect Unity full-platform hot update solution
自适应控制——仿真实验一 用李雅普诺夫稳定性理论设计自适应规律
梅科尔工作室-看鸿蒙设备开发实战笔记六—无线联网开发
Re17:读论文 Challenges for Information Extraction from Dialogue in Criminal Law
Re21:读论文 MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
New in GNOME: Warn users when Secure Boot is disabled
梅科尔工作室-看鸿蒙设备开发实战笔记四——内核开发
随机推荐
Multithreading--the usage of threads and thread pools
GNOME 新功能:安全启动被禁用时警告用户
wsl操作
New in GNOME: Warn users when Secure Boot is disabled
(BUG record) No module named PIL
软考 系统架构设计师 简明教程 | 系统运行与软件维护
2022年顶会accepted papers list
线程池方式开启线程--submit()和execute()的区别
Understanding of deadlock
Quick Start Tutorial for flyway
jmeter接口压力测试(一)
容器技术 -- 简单了解 Kubernetes 的对象
通过构建一个顺序表——教你计算时间复杂度和空间复杂度(含递归)
图像去噪——Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images
一个近乎完美的 Unity 全平台热更方案
In 2022, the top will be accepted cca shut the list
jmeter接口压力测试-(二)
JCL learning
在机器人行业的专业人士眼里,机器人行业目前的情况如何?
Domino服务器SSL证书安装指南