This snippet serves as an illustration of the class inheritance functionality (in this case, only public inheritance). It implements a virtual zoo, in which the visitor can interact with the animals. Download.
//Knowledgedump.org - Illustration of inheritance for classes in C++, with a virtual zoo. #include <iostream> #include <string> //Top class - every animal in the zoo can potentially be fed, has a nickname and species name. class animal { public: //Constructor needs to fetch the argument feedable from the other classes, //otherwise the function feed() wouldn't work for subclasses that inherited the function. animal(std::string name, std::string cl_name, bool fable); void feed(); //Function for feeding an animal in the zoo. private: std::string nickname, classname; bool feedable; //feedable determines, whether the animal can be fed. }; //Some animals are mammals, which can potentially be petted or make a sound. //mammal inherits the public section of animal, which is added to its own public section (public inheritance) class mammal :public animal { public: mammal(std::string name, std::string cl_name, bool fable, bool pable, bool hable); void pet(); void hear(); private: std::string nickname, classname; bool feedable, pettable, hearable; //Can you feed/pet/listen to the mammal or not? }; //Some animals are birds, which can not be petted, but possibly make a sound. class bird :public animal { public: bird(std::string name, std::string cl_name, bool fable, bool hable); void hear(); private: std::string nickname, classname; bool feedable, hearable; }; //Fish don't make a sound and can't be petted, but still can potentially be fed (inherited from top class animal). class fish :public animal { public: fish(std::string name, std::string cl_name, bool fable); private: std::string nickname, classname; bool feedable; }; //Goats are mammals, which can be fed, petted and make sounds that can be heard. //The class goat inherits the public section of mammal, which also contains the public section of animal. class goat :public mammal { public: goat(std::string name); //Constructor only sets nickname. Other attributes are fixed for all goats. private: std::string nickname; //The following attributes need to be declared as static. //Otherwise, they couldn't be passed to the upper class mammal, when calling mammal class methods. static const bool feedable = true; static const bool pettable = true; //static bools and integers can be defined inside the classes. static const bool hearable = true; static const std::string classname; //static strings need to be declared and then defined outside of the class! }; //Bears are mammals that can't be petted or fed, but make a sound. class bear :public mammal { public: bear(std::string name); private: std::string nickname; static const bool feedable = false; static const bool pettable = false; static const bool hearable = true; static const std::string classname; }; class donkey :public mammal { public: donkey(std::string name); private: std::string nickname; static const bool feedable = true; static const bool pettable = true; static const bool hearable = true; static const std::string classname; }; class hawk :public bird { public: hawk(std::string name); private: std::string nickname; static const bool feedable = false; static const bool hearable = true; static const std::string classname; }; class eagle :public bird { public: eagle(std::string name); private: std::string nickname; static const bool feedable = false; static const bool hearable = true; static const std::string classname; }; class duck :public bird { public: duck(std::string name); private: std::string nickname; static const bool feedable = true; static const bool hearable = true; static const std::string classname; }; class goldfish :public fish { public: goldfish(std::string name); private: std::string nickname; static const bool feedable = true; static const std::string classname; }; class shark :public fish { public: shark(std::string name); private: std::string nickname; static const bool feedable = false; static const std::string classname; }; class cod :public fish { public: cod(std::string name); private: std::string nickname; static const bool feedable = true; static const std::string classname; }; //Definition of animal class constructor. animal::animal(std::string name, std::string cl_name, bool fable) { nickname = name; classname = cl_name; feedable = fable; } //Function can be called by all classes that inherit it from the animal class. void animal::feed() { if (feedable) { std::cout << "You feed " << classname << " " << nickname << "." << std::endl; } else { std::cout << "You can't feed " << classname << " " << nickname << "." << std::endl; } } //Constructor of mammal class. mammal::mammal(std::string name, std::string cl_name, bool fable, bool hable, bool pable) : animal(name, cl_name, fable) { //mammal passes some parameters to animal for the sub-object construction. nickname = name; classname = cl_name; feedable = fable; hearable = hable; pettable = pable; } void mammal::pet() { if (pettable) { std::cout << "You pet " << classname << " " << nickname << "." << std::endl; } else { std::cout << "You can't pet " << classname << " " << nickname << "." << std::endl; } } void mammal::hear() { if (hearable) { std::cout << "You listen to " << classname << " " << nickname << "." << std::endl; } else { std::cout << classname << " " << nickname << " makes no sound." << std::endl; } } //bird class constructor. bird::bird(std::string name, std::string cl_name, bool hable, bool fable) : animal(name, cl_name, fable) { nickname = name; classname = cl_name; hearable = hable; feedable = fable; } void bird::hear() { if (hearable) { std::cout << "You listen to " << classname << " " << nickname << "." << std::endl; } else { std::cout << classname << " " << nickname << " makes no sound." << std::endl; } } //fish constructor. fish::fish(std::string name, std::string cl_name, bool fable) : animal(name, cl_name, fable) { nickname = name; classname = cl_name; feedable = fable; } //goat hands down its static parameters to mammal, which in turn will hand them down to animal. //This is required, if we want to use the goat parameters, when calling member functions of mammal or animal. goat::goat(std::string name) : mammal(name, classname, feedable, hearable, pettable) { nickname = name; } bear::bear(std::string name) : mammal(name, classname, feedable, hearable, pettable) { nickname = name; } donkey::donkey(std::string name) : mammal(name, classname, feedable, hearable, pettable) { nickname = name; } hawk::hawk(std::string name) : bird(name, classname, hearable, feedable) { nickname = name; } eagle::eagle(std::string name) : bird(name, classname, hearable, feedable) { nickname = name; } duck::duck(std::string name) : bird(name, classname, hearable, feedable) { nickname = name; } goldfish::goldfish(std::string name) : fish(name, classname, feedable) { nickname = name; } shark::shark(std::string name) : fish(name, classname, feedable) { nickname = name; } cod::cod(std::string name) : fish(name, classname, feedable) { nickname = name; } //Outside definition of the static const strings in our classes. const std::string goat::classname = "goat"; const std::string bear::classname = "bear"; const std::string donkey::classname = "donkey"; const std::string hawk::classname = "hawk"; const std::string eagle::classname = "eagle"; const std::string duck::classname = "duck"; const std::string goldfish::classname = "goldfish"; const std::string shark::classname = "shark"; const std::string cod::classname = "cod"; //Small test of the zoo. int main() { bear bear1("bruno"); shark shark1("bruce"); hawk hawk1("herald"); donkey donkey1("olef"); duck duck1("donald"); bear1.pet(); shark1.feed(); hawk1.feed(); hawk1.hear(); bear1.hear(); donkey1.pet(); donkey1.feed(); duck1.feed(); duck1.hear();donkey1.hear(); return 0; }
You can't pet bear bruno. You can't feed shark bruce. You can't feed hawk herald. You listen to hawk herald. You listen to bear bruno. You pet donkey olef. You feed donkey olef. You feed duck donald. You listen to duck donald. You listen to donkey olef.