추상 클래스 [디딤돌 C++]
//추상 클래스
//Program.cpp
#include <iostream>
#include <string>
using namespace std;
class Musician
{
string name;
public:
Musician(string name)
{
this->name = name;
}
virtual void Play()=0; //순수 가상 메서드(추상 메서드)
void Introduce()
{
cout<<name<<" 음악가: 안녕"<<endl;
}
string GetName()const
{
return name;
}
};
class Pianist:public Musician
{
public:
Pianist(string name):Musician(name)
{
}
virtual void Play()
{
cout<<GetName()<<" 연주하다."<<endl;
cout<<"딩동댕 ♩♪♬"<<endl;
}
};
class Drummer:public Musician
{
public:
Drummer(string name):Musician(name)
{
}
virtual void Play()
{
cout<<GetName()<<" 연주하다."<<endl;
cout<<"두두둥~~~"<<endl;
}
};
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);
return 0;
}
void StartConcert(Musician *musician)
{
musician->Introduce();
musician->Play();
}
void StartConcert(Musician &musician)
{
musician.Introduce();
musician.Play();
}
* 디딤돌 C++ 34. 추상 클래스에서
'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 |