Created
November 13, 2012 17:11
-
-
Save Jack2/4067051 to your computer and use it in GitHub Desktop.
[C++]virtual_example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Food { | |
public : | |
virtual void SetPrice(int myprice) = 0; //순수 가상함수 | |
int GetPrice(){ | |
return price; | |
} | |
protected : | |
int price; | |
}; | |
class Fruit : public Food{ | |
public : | |
void SetPrice(int myprice){ | |
price = myprice - 20; | |
} | |
}; | |
class Fish : public Food { | |
public : | |
void SetPrice(int myprice){ | |
price = myprice / 2; | |
} | |
}; | |
int main() | |
{ | |
Food *pFood; | |
Fruit myFruit; | |
Fish myFish; | |
pFood = &myFruit; | |
pFood -> SetPrice(100); | |
cout << "myFruit의 Price = " << pFood -> GetPrice() << endl; | |
pFood = &myFish; | |
pFood -> SetPrice(100) ; | |
cout << "myFish의 Price = " << pFood -> GetPrice() << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment