arrays - Help with Object Oriented Programming with C++ -
i need figuring out if possible. still new please forgive me. have array of object want method manipulate me. i'm not sure how best put in words use example.
#include <iostream> using namespace std; class fruit { private: int amount; public: void eat(); }; void fruit::eat() { //manipulate amount apples @ both indexes here } int main() { fruit apples[2]; fruit pie; pie.eat(); return 0; }
i want change amount @ both indexes in apples array using eat() function. how go doing this. i'm sorry if seems stupid.
well question many of answers don't seem correct, because doesn't make sense fruit eating fruit. mean "fruit eating fruit"? please explain?
a possible , reasonable class design should this:
class person { public: void eat(edible *item) {} void drink(drinkable *item) {} void sleep(double duration) {} //... }; class edible { public: virtual ~edible() {} virtual double get_calories() = 0; //pure virtual function //... } class fruit : public edible { public: virtual double get_sweetness() = 0; //pure virtual function //... } class apple : public fruit { public: //define pure virtual functions } class banana : public fruit { public: //define pure virtual functions } std::vector<edible*> items; items.push_back(new apple()); items.push_back(new apple()); items.push_back(new banana()); items.push_back(new banana()); person nawaz; for(int = 0 ; < items.size() ; i++ ) nawaz.eat(items[i]); nawaz.sleep(8 * 60 * 60); //8 hours!
Comments
Post a Comment