Hi All,
Below is the code of implementation of aggregation in c++. In this code class bar object will be contained in class a but
class a won't act as a owner of class bar and due to this bar object is deleted after class a's life time ends.
Now i would like to know as i never experienced aggregation before :
(1) How bar object before deletion can be linked with other classes as class 'a; still doesn't own the bar class object .
(2) Do we need to provide the helper function to delete the bar object in case if it interacts with other classes . So that it can be deleted after last interaction.
(3) As Class a don't own the class bar then can still assocation be used instead of aggregation by passing the bar object from class a's any method.
#include <iostream>
using namespace std;
class bar
{
public:
void show()
{
cout<<"Travel the world";
}
~bar(){cout<<"b's destructor"<<endl;}
};
class a
{
bar *b;
int * p;
public:
a(bar *b,int* p):b( b),p(p)
{
b->show();
}
~a(){
cout<<"a's destructor"<<endl;
}
};
int main()
{
int *p= new int (10);
bar* obj1= new bar;
{
a obj(obj1,p);
delete p;
}
delete obj1;// obj1 still exist after obj is out of the scope
return 1;
}