class Animal {
string _name;
string _type;
string _sound;
// private constructor prevents construction of base class
Animal(){};
protected:
// protected constructor for use by derived classes
Animal(const string & n, const string & t, const string & s)
: _name(n), _type(t), _sound(s) {}
public:
void speak() const;
const string & name() const { return _name; }
const string & type() const { return _type; }
const string & sound() const { return _sound; }
};
void Animal::speak() const {
printf("%s the %s says %s\n", _name.c_str(), _type.c_str(), _sound.c_str());
}
// Dog class - derived from Animal
class Dog : public Animal {
int walked;
public:
Dog(string n) : Animal(n, "dog", "woof"), walked(0) {};
int walk() { return ++walked; }
};
// Cat class - derived from Animal
class Cat : public Animal {
int petted;
public:
Cat(string n) : Animal(n, "cat", "meow"), petted(0) {};
int pet() { return ++petted; }
};
// Pig class - derived from Animal
class Pig : public Animal {
int fed;
public:
Pig(string n) : Animal(n, "pig", "oink"), fed(0) {};
int feed() { return ++fed; }
};
int main(int argc, char ** argv) {
Dog d("Rover");
Cat c("Fluffy");
Pig p("Arnold");
d.speak();
c.speak();
p.speak();
cout << d.name() << " the dog has been walked " << d.walk() << " times" << endl;
cout << c.name() << " the cat has been petted " << c.pet() << " times" << endl;
cout << p.name() << " the pig has been fed " << p.feed() << " times" << endl;
}
//This is the code from eclipse,once i copy and paste in visual studio 2013 it's working,but if i try to enter
//everything manualy -create class/constructors,i stack on the creatring constractor
//function line-Animal(const string & n, const string & t, const string & s)
// and so on,can some one explaine how to manualy enter everything in visual studio 2013.Thank you.
aluhnev 0 Junior Poster in Training
Lp_baez 0 Light Poster
aluhnev 0 Junior Poster in Training
NathanOliver 429 Veteran Poster Featured Poster
aluhnev 0 Junior Poster in Training
NathanOliver 429 Veteran Poster Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.