Thread: using Polymorphism

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    18

    using Polymorphism

    let say we have class Animal and class Dog that derived from Animal

    Code:
    class Animal
    {
    private :
         int age;
    public:
         Animal();
          .......
    };
    
    class Dog:public Animal
    {
    private :
        string wayOfBirth;
    public
         dog()
         void setBirth(const string& birth) ;
          string getBirth() const;
    };
    now let say i will creat an Animal pointer that will point to new Dog
    Animal* bluh = new Dog();

    - can this pointer access (void setBirth(const string& birth); )
    bluh->setBirth(string)................?
    if not how can i access it?

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    1. Define it as a pure virtual member function in Animal.

    Code:
    class Animal
    {
      ....
    virtual void setBirth(const string& birth) = 0;
      ....
    };
    OR

    2. Cast the pointer-to-Animal bluh to a pointer-to-Dog.

    Code:
    ((Dog*) bluh)->setBirth( "blah" );

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    ((Dog*) bluh)->setBirth( "blah" );
    Yeuch! this is c++ not c.
    Code:
    (static_cast<Dog*>(bluh))->setBirth( "blah" );
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    18
    thanks
    it works

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Yeuch! this is c++ not c.
    If it were C, we wouldn't have classes, now would we? ;-)
    not sure how you solved it, but if you made setBirth a virtual function of Animal, it does not have to be a pure virtual (unless of course you want it that way)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If setBirth makes sense for an Animal, make it a virtual function in Animal as has been said. However, if setBirth does not make sense for an Animal in general, then that is probably not the best design.

    If you have code that needs to call setBirth only if the Animal is a Dog, then that code should only be using Dog pointers.

    There are some rare cases where you must cast a base class pointer to the derived class. In those cases you should use dynamic_cast if you aren't sure whether the cast will succeed, or static_cast if you know it must. In general, casting down the inheritance hierarchy is a sign of poor design.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 05:06 PM
  2. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 04:41 AM
  3. Question on polymorphism
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2008, 09:05 AM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM