简体   繁体   English

c++中的作文

[英]Composition in c++

I know the basic idea of composition that the composition has "have" relationship.But when it came to implement the idea of composition something went wrong,Till now i didn't figure out the object and constructor call of another class in the base class. Please help me on this regard.我知道组合有“有”关系的组合的基本思想。但是当实现组合的想法时出了点问题,直到现在我还没有弄清楚class中另一个class的object和构造函数调用. 请在这方面帮助我。

Composition means that the contained class object does not exist beyond the lifetime of the outer class(which contains it) object.组合意味着所包含的 class object 在外部类(包含它)object 的生命周期之外不存在。

Online Sample :在线样品

#include <iostream>

class MyClass
{
    public:
        MyClass(){std::cout<<"\nMyClass";}
        ~MyClass(){std::cout<<"\n~MyClass";}
};

class MySecClass
{
    MyClass obj;
    public:
        MySecClass(){std::cout<<"\nMySecClass";}
        ~MySecClass(){std::cout<<"\n~MySecClass";}
};

int main()
{
    MySecClass obj;
    return 0;
}

Output: Output:

MyClass我的课
MySecClass我的安全类
~MySecClass ~MySecClass
~MyClass ~我的班级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM