构造函数综合小练习1
来源:互联网 发布:linux进程查看命令 top 编辑:程序博客网 时间:2024/05/21 08:59
知识点:1.匿名对象生命周期 2. 拷贝构造函数 3. 构造函数,析构函数的调用顺序
#include "iostream"using namespace std;
class Triangle{public: Triangle(int a, int b, int c) { this->a_ = a; this->b_ = b; this->c_ = c; cout << "Triangle() construct " << "a=" << this->a_ << ", b=" << this->b_ << ",c=" << this->c_ << endl; } ~Triangle() { cout << "Triangle() deconstruct " << "a=" << this->a_ << ", b=" << this->b_ << ",c=" << this->c_ << endl; } int Get_a() { return this->a_; }protected:private: int a_; int b_; int c_;};class MyTriangle{public: MyTriangle() :tri1_(1, 2, 3), tri2_(4, 5, 6), m(100) { cout << "MyTriangle 构造函数" << endl; } ~MyTriangle() { cout << "MyTriangle析构函数" << endl; } MyTriangle(const MyTriangle & obj) :tri1_(7, 8, 9), tri2_(10, 11, 12), m(100) { cout << "MyTriangle拷贝构造函数" << endl; }protected:public: Triangle tri1_; //Triangle没有对应的无参构造函数,c++编译器不知道如何构造 Triangle tri2_; const int m;};int DoWork(MyTriangle myTriangle){ cout <<"DoWork---成员变量a_数据是:"<< myTriangle.tri1_.Get_a() << endl; return 0;}void Test(){ MyTriangle triAngle; DoWork(triAngle); }//没有组织接纳的匿名对象int AnonymousObj(){ cout<<"AnonymousObjTest begin"<<endl; Triangle(400, 500, 600); //匿名对象的生命周期,这种情况下没有被接收,产生即会消亡。 cout << "AnonymousObjTest end" << endl;; return 0;}//有组织接收的匿名对象int AnonymousObjAccepted(){ cout << "AnonymousObjTest begin" << endl; Triangle tri = Triangle(100, 200, 300);//匿名对象直接提升为tri,不会调用拷贝构造函数 cout << "AnonymousObjTest end" << endl;; return 0;}int main(){ Test(); AnonymousObj(); AnonymousObjAccepted(); cin.get(); return 0;}
0 0
- 构造函数综合小练习1
- 构造函数综合小练习2
- Java中构造函数重构+数组小练习
- 函数积分图(综合练习)
- 构造函数练习
- 构造函数练习
- c#构造函数练习
- 黑马程序员--学习笔记--一个字符串、数组、函数综合运用的小练习
- Test__IO流综合应用的小练习
- 综合练习(链表、函数、结构体)
- fopen()函数小练习
- 指针函数小练习
- 随机函数小练习
- JQ综合练习(1)
- 构造函数 课堂练习3:
- 构造函数的demo练习
- 综合练习
- 综合练习
- 使用队列让线程同步
- 线程之间的通信
- 错误代码:E/FragmentManager(1337): No view found for id 0x7f030005 (com.example.mnews:layout/menu_frame)
- 一个双引号,愁白头
- 同源策略
- 构造函数综合小练习1
- 自定义缓存工具类----sharedPreferences
- get_optlong用法(linux解析命令行参数)
- leetcode刷题,总结,记录,备忘 260
- Combinations
- 将qint8类型的数组放进float类型的数组里面
- Android 编程下两种方式注册广播的区别
- Python注意事项
- MyEclipse 2015 Stable 2.0破解步骤图