Thread: Whilst playing with abstract classes...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    245

    Whilst playing with abstract classes...

    Whilst the following code works perfectly well, I'm wondering if this is the best way to do it, and also, although Cfish is the base class of both Csalmon and Cpike, shouldn't it require a cast to assign one of those to the Cfish ?

    Code:
    class Cfish
    {
    public:
    	virtual int feed (void) =  0;
    };
    
    class Cpike : public Cfish
    {
    public:
    	int feed (void);
    };
    
    class Csalmon : public Cfish
    {
    	public:
    		int feed (void);
    };
    
    int Cpike::feed(void)
    {
    	printf ("Pike feeding.\n");
    	return 1;
    }
    
    int Csalmon::feed(void)
    {
    	printf ("Salmon feeding.\n");
    	return 1;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Cfish *fish;
    	
    	fish = new Csalmon; // why no cast needed here?
    	fish->feed();
    	delete fish;
    	
    	fish = new Cpike; // or here?
    	fish->feed();
    	delete fish;
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You are just doing an up-cast so there is no problem when you go "up" in the hierachy. If you try to down-cast you will need to use dynamic_cast or a c-style cast ( bad ).
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You need no cast because of polymorphism. A Csalmon * will work everywhere a Cfish * does, because a Csalmon is a tye of Cfish. Your Cfish pointer is polymorphic -- it can point to an object of ANY type that is derived from Cfish, and casts are never needed.

    However, and in your little example it is not needed, but you should ALWAYS provide a virtual destructor for any base class. Say, for example, that Csalmon allocated memory and needs to free it in a destructor. In your code, it would be leaked because, as written, delete fish; will call *Cfish's* destructor, not Csalmon's. Any time you want the option for polymorphism (e.g. you want to use a CBase * to point at CDerived objects) you should have a virtual destructor. Always.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with abstract classes
    By DavidP in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 03:03 PM
  2. Operators of abstract classes
    By Thanuja91 in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2007, 05:30 AM
  3. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 09:39 AM
  4. Replies: 7
    Last Post: 03-10-2004, 04:10 PM
  5. Purpose of Abstract Classes
    By luckygold6 in forum C++ Programming
    Replies: 15
    Last Post: 04-29-2003, 06:24 PM