
#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는 왜 있는 것인가요?
