0

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.

Youmair
  • 47
  • 2
  • 2
  • 5
  • What do base classes have to do with composition? – sbi Apr 10 '12 at 08:29
  • Are you after the syntax for calling member object constructors? Then this is what you need: http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – sbi Apr 10 '12 at 08:39

1 Answers1

1

Composition means that the contained class object does not exist beyond the lifetime of the outer class(which contains it) 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:

MyClass
MySecClass
~MySecClass
~MyClass

Alok Save
  • 202,538
  • 53
  • 430
  • 533