Advertisement
Guest User

CS32 Class Composition question

a guest
Jan 17th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class A;
  6. class B;
  7.  
  8. class A
  9. {
  10. public:
  11.     A()
  12.         :b(this)
  13.     {
  14.         n = "test";
  15.         return;
  16.     }
  17.     void print_str()
  18.     {
  19.         cout << this->n;
  20.     }
  21. private:
  22.     string n;
  23.     B b;
  24. };
  25.  
  26.  
  27. class B
  28. {
  29. public:
  30.     B(A* ptr_A)
  31.     {
  32.         m_ptr_A = ptr_A;
  33.     }
  34.     ~B()
  35.     {
  36.         m_ptr_A->print_str();
  37.     }
  38. private:
  39.     A* m_ptr_A;
  40. };
  41.  
  42.  
  43.  
  44.  
  45. int main()
  46. {
  47.     A a;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement