커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
16강 질문
C++ 문법 뽀개기 v2
기타
북마크
정*환
댓글
5
추천
0
조회수
19
조회수
19
답변 완료

#include <iostream>

using namespace std;


class Person

{

  protected:

  string m_name;

   

  public:

  Person(string name)

  {

    m_name=name;

  }

   

  virtual void sayhello()

  {

    cout << "hello i'm " << m_name << endl;

  }

};


class Musician : public Person

{

  public:

  Musician(string m_name) : Person(m_name)

  {

  }

   

  void sayhello()

  {

    cout << "hi i'm " << m_name << endl;

    cout << "i'm a musician" << endl;

  }

   

  void playsomething()

  {

    cout << "lalala" << endl;

  }

};


class Baker : public Person

{

  public:

  Baker(string m_name) : Person(m_name)

  {

  }

   

  void sayhello()

  {

    cout << "hi i'm " << m_name << endl;

    cout << "i like bread" << endl;

  }

};


int main()

{

  Person *p1 = new Musician("jeff");

  p1 -> sayhello();

  //p1 -> playsomething();

  cout << endl;

   

  Person *p2 = new Person("jane");

  p2 -> sayhello();

  cout << endl;

   

  p1 = new Baker("yumi");

  p1 -> sayhello();

   

  delete p1;

  delete p2;

   

  return 0;

}


위 스크립트에서 결과에 "lalala"도 나오게 하려면 어떻게 해야 하나요?

그리고 마지막에 delete p1과 p2는 왜 있는 것인가요?

취소
 공유
취소
댓글 0
댓글 알림
나의얼굴