public 상속

#include<iostream>
using namespace std;
class Asset{
   int money;
public:
   Asset() { money = 0; }
   void inc_money(int m) { money += m; }
   void show_asset() { 
      cout << "money : " << money << endl;
   }
};

class Richman : public Asset
{
   int gold;
public:
   Richman() { gold = 0; }
   void inc_gold(int g) { gold += g; }
   void gold_to_money() { 
       inc_money(gold);
       gold = 0;
   }
};

int main()
{
  Richman R1;

  R1.inc_money(100);
  R1.show_asset();

  R1.inc_gold(200);
  R1.gold_to_money();

  R1.show_asset();
  return 0;
}

결과는

money : 100

money : 300

 

public Asset을 상속받은 Richman 클래스는 부모인 Asset의 public 멤버에는 바로 접근할 수 있다. 하지만
Asset클래스의 private 멤버변수로 지정된 money에 접근하려면 Asset의 public 멤버를 활용해서 접근해야 한다.


➡️  public으로 상속받았을 때는 부모의 public에는 R1.inc_money()처럼 직접 접근 가능

      하지만 부모의 private은 직접 접근은 안되고 부모의 public 멤버함수를 활용해 접근해야 한다.

 

 

private 상속

#include<iostream>
using namespace std;
class Asset{
   int money;
public:
   Asset() { money = 0; }
   void inc_money(int m) { money += m; }
   void show_asset() { 
      cout << "money : " << money << endl;
   }
};

class Richman : private Asset
{
   int gold;
public:
   Richman() { gold = 0; }
   void inc_gold(int g) { gold += g; }
   void gold_to_money() { 
       inc_money(gold);
       gold = 0;
   }
   void show() {
     show_asset();
   }
};

int main()
{
  Richman R1;

  //R1.inc_money(100); 직접 접근 불가능!!
  //R1.show_asset(); 직접 접근 불가능!!

  R1.inc_gold(200);
  R1.gold_to_money(); // 내부에서 부모의 public멤버를 호출하여 접근

  R1.show();
  return 0;
}

결과는

money : 200

 

private은 은닉성 때문에  private으로 상속받았을 때는 부모의 public 멤버, private 멤버 모두 직접 접근할 수 없다.

➡️   private으로 상속 받았을 때는 부모의 public 멤버여도 R1.inc_money()처럼 직접 접근 불가능

      부모에 접근하려면 직접 접근이 아닌 R1.gold_to_money()처럼 부모의 public 멤버함수를 내부에서
      호출하는 형태로
접근해야 함

 

 

protected 멤버 상속

private과 public 상속은 위에서와 같이 좀 불편한 경우들이 생긴다.
하지만 protected 상속을 이용하면 외부에서는 접근 불가능하지만 상속받은 자식에서는 부모에 접근이 가능하게 된다.

#include<iostream>
using namespace std;
class Asset{
protected:   // protected 멤버
   int money;
public:
   Asset() { money = 0; }
   void show_asset() { 
      cout << "money : " << money << endl;
   }
   void inc_money(int m) { money += m; }
};

class Richman : public Asset
{
   int gold;
public:
   Richman() { gold = 0; }
   void inc_gold(int g) { gold += g; }
   void gold_to_money() { 
       money += gold;     // money가 protected 멤버라 자식에서 이렇게 직접 접근 가능
       gold = 0;
   }
   void show() {
     cout << "money : " << money << endl;
   }
};

int main()
{
  Richman R1;

  R1.inc_gold(200);
  R1.gold_to_money();

  R1.show();
  return 0;
}

결과

money : 200

 

부모를 상속받은 자식 클래스에서 money에 직접 접근이 가능하여 소스코드가 훨씬 간결해졌다.

 

 

상속 관계에서 생성자, 소멸자 호출 순서

#include<iostream>
using namespace std;
class parentClass {
public:
  parentClass() { 
     cout << "parentClass 생성자 " << endl;
  }
  ~parentClass() {
     cout << "parentClass 소멸자 " << endl;
  }
};
class childClass : public parentClass {
public:
  childClass() {
     cout << "childClass 생성자 " << endl;
  }
  ~childClass() {
     cout << "childClass 소멸자 " << endl;
  }
};

int main()
{
  childClass C;

  return 0;
}

결과

parentClass 생성자

childClass 생성자

childClass 소멸자

parentClass 소멸자

 

부모가 먼저 생성되고, 이후 상속받은 자식이 생성되고
자식이 제거된 후, 부모가 제거된다.

+ Recent posts