형식의 다형성 [디딤돌 C++]
//형식의 다형성
//Program.cpp
#include <iostream>
#include <string>
using namespace std;
class Musician
{
string name;
public:
Musician(string name)
{
this->name = name;
}
void Play()
{
cout<<name<<" 연주하다."<<endl;
}
};
class Pianist:public Musician
{
public:
Pianist(string name):Musician(name)
{
}
};
class Drummer:public Musician
{
public:
Drummer(string name):Musician(name)
{
}
};
void StartConcert(Musician *musician);
void StartConcert(Musician &musician);
int main()
{
Pianist *pianist1 = new Pianist("피아노맨1");
Drummer *drummer1 = new Drummer("두둥맨1");
StartConcert(pianist1);
StartConcert(drummer1);
delete pianist1;
delete drummer1;
Pianist pianist2("피아노맨2");
Drummer drummer2("두둥맨2");
StartConcert(pianist2);
StartConcert(drummer2);
}
void StartConcert(Musician *musician)
{
musician->Play();
}
void StartConcert(Musician &musician)
{
musician.Play();
}
* 디딤돌 C++ 32. 형식의 다형성에서
'C++ > 디딤돌 C++' 카테고리의 다른 글
추상 클래스 [디딤돌 C++] (0) | 2016.04.14 |
---|---|
메서드의 다형성 [디딤돌 C++] (0) | 2016.04.14 |
메서드의 다형성 개요[디딤돌 C++] (0) | 2016.04.14 |
형식의 다형성 개요[디딤돌 C++] (0) | 2016.04.14 |
무효화한 멤버 사용하기 [디딤돌 C++] (0) | 2016.04.14 |