C++ 17. 형식의 다형성

-형식의 다형성

#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(void)

{

        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->Play();

}

 

void StartConcert(Musician &musician)

{

        musician.Play();

}

 

728x90

'C++ > 설명' 카테고리의 다른 글

C++ 19.하향 캐스팅  (0) 2016.04.19
C++ 18. 무효화  (0) 2016.04.19
C++ 17. 형식의 다형성  (0) 2016.04.19
C++ 16.메소드의 다형성  (0) 2016.04.19
C++ 15.다중 상속  (0) 2016.04.19
C++ 14.상속 일반화 개요  (0) 2016.04.19

이 글을 공유하기

댓글(0)

Designed by JB FACTORY