Skip to content

Instantly share code, notes, and snippets.

@Jack2
Created November 13, 2012 17:11
Show Gist options
  • Save Jack2/4067051 to your computer and use it in GitHub Desktop.
Save Jack2/4067051 to your computer and use it in GitHub Desktop.
[C++]virtual_example
#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