c++函数分类以及调用
2021/9/30 14:10:43
本文主要是介绍c++函数分类以及调用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include<bits/stdc++.h> using namespace std; //1.构造函数分类和调用 //按参数分类 无参构造(默认) 有参构造 //类型分类 普通构造 拷贝构造 class Person { public: //构造函数 Person() { cout << "无参函数调用" << endl; } Person(int a) { age = a; cout << "有参函数调用" << endl; } //拷贝构造函数 Person(const Person &p) { cout << "拷贝函数调用" << endl; age = p.age;//拷贝 } ~Person() { cout << "析构函数调用" << endl; } int age; }; //调用 void test01() { //1.括号法 Person p;//默认构造函数调用 Person p2(10);//有参构造函数 Person p3(p2);//拷贝函数调用 //注意:调用默认构造函数不加:() cout << "p2.age:" << p2.age <<endl; cout << "p3.age:" << p3.age <<endl; //2.显示法 /* Person p1; Person p2 = Person(10); Person p3 = Person(p2);\ */ //Person(10)//匿名对象:当前行执行结束,系统回收 //注意:不要用拷贝构造函数初始化匿名对象 //3.隐式转换法 Person p4 = 10;//=== Person p4 = Person(10); Person p5 = p4;//拷贝构造 } int main() { test01(); system("pause"); }
这篇关于c++函数分类以及调用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-08测试人员在 Scrum 中的角色是什么?
- 2024-07-07Dify + TiDB Vector,快速构建你的AI Agent
- 2024-07-06有没有什么开源的py项目可以对图像进行分类-icode9专业技术文章分享
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享