I am trying to run the following C++ code to understand class inheritance using MS Visual Studio 15. After I build and run the code, I get a message saying MS VS has stopped working. I'd really appreciate if someone can help me understand what I am doing wrong.
#include<cstdio>
#include<string>
#include<conio.h>
using namespace std;
// BASE CLASS
class Animal {
private:
string _name;
string _type;
string _sound;
Animal() {};
protected:
Animal(const string &n, const string &t, const string &s) :_name(n), _type(t), _sound(s) {};
public:
void speak() const;
};
void Animal::speak() const {
printf("%s, the %s says %s.\n", _name, _type, _sound);
}
// DERIVED CLASSES
class Dog :public Animal {
private:
int walked;
public:
Dog(const string &n) :Animal(n, "dog", "woof"), walked(0) {};
int walk() { return ++walked; }
};
int main(int argc, char ** argv) {
Dog d("Jimmy");
d.speak();
printf("The dog has been walked %d time(s) today.\n", d.walk());
return 0;
_getch();
}