Thread: about non-virtual func. in derived class

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    about non-virtual func. in derived class

    I have a question about polymorphism.
    This is code
    Code:
    #include <iostream> 
    using namespace std; 
    
    class Pet {
     public: virtual void speak() const ; 
    };
    void Pet::speak() const { cout << "Hi" << endl; }
    
    class Dog : public Pet { 
    public: void speak() const { cout<<"hi too";} 
    void eat() const { cout<<"eat"; }
     }; 
    
    int main() { 
    Dog simba; Pet* p=&simba; p->speak();//I can call dog::speak by pet pointer 
    //p->eat(); but I can not call dog::eat() by pet. 
    }

    here how can i solve the problem.I mean i want to enhance my derived class and call my functions from base class pointer?

    Is there a solution?
    Thanks...

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    No. Change your pointer type or do a cast from Pet* to Dog* before accessing the function.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ...or you can make your eat function virtual in your pet class.
    Sent from my iPadŽ

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    That might be an even better solution, but I'm not sure what OP is trying to do.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Given the topic, I'd guess my solution is something he considered. In any event, I haven't seen many polymorphism examples with a single derived class.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    just a question , why do you put const on a void function ,
    dont see the point in this but someone tell me if i am wrong

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Anddos
    just a question , why do you put const on a void function ,
    dont see the point in this but someone tell me if i am wrong
    So it can operate on a const object. There are no members changed in it, so it's valid.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Just to expand on SlyMaelstrom's reply - the const qualifier has nothing to do with the return type of the function, be it "void", "int" or some other type. You can also have a const function which returns a const int, then you'd use the const keyword twice in the function declaration.

    this example might be a good illustration (this deliberately won't compile.)
    Code:
    #include <iostream>
    using namespace std;
    
    class MyClass
    {
       int i;
    public:
       MyClass() : i(5) {}
       int geti() { return i;  }
           // geti() isn't const, it is free to modify the object.
    
    };
    
    int main()
    {
       const MyClass MyObj;
       int x = MyObj.geti(); // ERROR!!  geti() may potentially modify MyObj.
       cout << x;
    }
    the compiler needs "const" after geti() as a promise that you are not going to modify MyObj.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. base and derived class
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 03:11 PM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM