-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab_solution_12.cpp
46 lines (35 loc) · 896 Bytes
/
Lab_solution_12.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <memory>
using namespace std;
class Company
{
std::string m_name;
std::shared_ptr<Company> m_partner; // initially created empty
public:
Company(const std::string &name): m_name(name)
{
cout << "Company Construct " << m_name <<endl;
}
~Company()
{
cout << "Company Destruct " << m_name <<endl;
}
bool setPartner(std::shared_ptr<Company> &c)
{
if (!c)
return false;
m_partner = c;
std::cout << m_name << " is now partnered with " << c->m_name << "\n";
return true;
}
};
int main()
{
cout << "Begin of main()" << endl;
shared_ptr<Company> companyPtr1 = make_shared<Company>("Amazon");
shared_ptr<Company> companyPtr2 = make_shared<Company>("Google");
companyPtr1.get()->setPartner(companyPtr2);
companyPtr2.get()->setPartner(companyPtr1);
cout << "End of main()" << endl;
return 0;
}