codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
class Fruit { public: Fruit() : m_color("fruit-colored"), m_peeled(false) {} virtual ~Fruit() {} virtual void eat(); virtual void peel(); void setColor(const std::string &color) { m_color = color; } const std::string &color() const { return m_color; } protected: std::string m_color; bool m_peeled; }; void Fruit::eat() { std::cout << "Now eating this " << m_color << " fruit!\n"; } void Fruit::peel() { if (!m_peeled) m_peeled = true; } class Orange : public Fruit { public: Orange() : Fruit() { m_color = "orange"; } void eat(); }; void Orange::eat() { if (m_peeled) { Fruit::eat(); } else { std::cout << "Can't eat orange before peeling it!\n"; } } int main() { Fruit *fruit1 = new Fruit; Fruit *fruit2 = new Orange; Fruit *fruit3 = new Orange; fruit1->eat(); fruit2->eat(); fruit2->peel(); fruit2->eat(); std::cout << fruit2->color(); delete fruit1; delete fruit2; delete fruit3; return 0; }
Private
[
?
]
Run code
Submit