C++ Inheritance Constructor -
i trying create mammal object phone call speak function animal class. may know part wrong? new inheritance.
#include <iostream> #include <string> using namespace std ; enum color { green, blue, white, black, brownish } ; class animal { public : animal() : _name("unknown") { cout << "constructing animal object " << _name << endl ; } animal (string n , color c) { cout << "animal name: " << n << " color: " << c << endl; }; ~animal() { cout << "destructing animal object " << _name << endl ; } void speak() const { cout << "animal speaks " << endl ; } void move() const { } private : string _name; color _color ; }; class mammal : public animal { public: mammal (string n, color c) : animal (n, c) { cout << "animal name: " << n << " color: " << c << endl; } ~mammal() {} void eat() const { cout << "mammal eat " << endl ; } }; int main() { mammal m.speak(); animal b("lion", green); cout << "program exiting …. " << endl ; homecoming 0; }
i not understand why cannot create object m phone call speak function in animal.
you have split statement
mammal m.speak();
in 2 statements
mammal m; m.speak();
and have define default constructor class mammal.
for example
mammal() = default;
or
mammal() {}
c++
No comments:
Post a Comment