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.

Recommended Answers

All 5 Replies

Specify your question a little more. Are you asking how to start and create a c++ file in visual studio or if when you write this code an error appears when debugging it but not when you paste the same code?

The question is how to create class and enter that costructors,in visual studio,but not to type everything on one page.the problem is to create costructor with wizard.

Go to Project -> Add Class. That will create a new class and bring up the class wizzard.

Thats good ,i now,but i mean how to create that costructor-Animal(const string & n, const string & t, const string & s)I enter class name,than how to finish that line typing in wizzard? Thanks

Honestly I never use the wizzard. I just write the code.

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.