Thread: unable to instantiate derived class!!!

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    unable to instantiate derived class!!!

    i have made Animal an abstract base class,
    iam supposed to be able to create Dog, Cat and Snake objects,
    but there is an error.
    please help
    Code:
    #include"stdafx.h"
    #include<iostream>
    #include<string>
    using namespace std;
    class Animal
    {
    	string name;
    protected:
    	Animal(const string& nameP) :name(nameP){}
    public:
    	virtual string getSound() = 0;
    	string getName() const { return name; }
    };
    class Dog :public Animal
    {
    public:
    	Dog(const string& nameP) :Animal(nameP){}
    	string getSound()const{ return "woof"; }
    };
    class Cat :public Animal
    {
    public:
    	Cat(const string& nameP) :Animal(nameP){}
    	string getSound()const{ return "meow"; }
    };
    class Snake :public Animal
    {
    public:
    	Snake(const string& nameP) :Animal(nameP){}
    	string getSound()const{ return "hiss"; }
    };
    void report(const Animal& animal);
    int main()
    {
    	Dog rocky("Rocky");
    	Cat sylvester("Sylvester");
    	Snake eikens("Eikens");
    	Animal* array[] = { &rocky, &sylvester, &eikens };
    	for (auto x : array)
    		report(*x);
    }
    void report(const Animal& animal)
    {
    	cout << animal.getName() << " says\t" << animal.getSound() << endl;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to declare getSound as const in the abstract base class.

    By the way, your abstract base class should have a virtual destructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you use the keyword virtual in the derived classes as well, even though technically it isn't required. But it makes it clear that they are virtual functions.
    Also, if you're going to override a base class virtual function, use the override keyword. It will ensure that you are actually overriding a function instead of hiding it. Example:

    Code:
    virtual string getSound() const override { return "woof"; }
    Plop in that override keyword and you get a compile error saying you didn't actually override any base class function getSound (because of the missing const).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 11:17 AM
  2. Replies: 4
    Last Post: 11-15-2008, 06:11 PM
  3. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  4. How can I make the base class unable to instantiate?
    By dxfoo in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 09:57 AM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM