Results 1 to 4 of 4

Thread: C++ virtual functions

  1. #1
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Exclamation C++ virtual functions

    Sir,
    I have a query about this vitual function. Look at this example:-
    Qt Code:
    1. using namespace std;
    2.  
    3. #include <iostream>
    4. class Animal {
    5. public:
    6. void eat() { //I have removed the virtual keyword here.
    7. cout << "I eat like a generic animal.\n";
    8. }
    9. // polymorphic deletes require a virtual base destructor
    10. //virtual ~Animal() {
    11. //}
    12. };
    13. class Wolf : public Animal {
    14. public:
    15. void eat() {
    16. cout << "I eat like a wolf!\n";
    17. }
    18. };
    19. class Fish : public Animal {
    20. public:
    21. void eat() {
    22. cout << "I eat like a fish!\n";
    23. }
    24. };
    25. class GoldFish : public Fish {
    26. public:
    27. void eat() {
    28. cout << "I eat like a goldfish!\n";
    29. }
    30. };
    31. int main() {
    32. Animal animal;
    33. Wolf wolf;
    34. Fish fish;
    35. GoldFish goldfish;
    36. animal.eat();
    37. wolf.eat();
    38. fish.eat();
    39. goldfish.eat();
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    The output is :-
    I eat like a generic animal.
    I eat like a wolf!
    I eat like a fish!
    I eat like a goldfish!

    Instead of this:-

    I eat like a generic animal.
    I eat like a generic animal.
    I eat like a generic animal.
    I eat like a generic animal.

    Why is this happening? Is there something wrong? If it is possible then what is the use of this virtual function. Please answer me.

    Thank You.
    Last edited by wysota; 23rd May 2012 at 09:00. Reason: missing [code] tags

  2. #2
    Join Date
    Apr 2012
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: C++ virtual functions

    1. All is ok. When you override(overload) non-virtual function in derived classes it hides the same function in base class.
    2. You haven't any virtual function so you haven't virtual table and compiler doesn't know anything about functions from base class.
    3. Add word "virtual" to the class Animal then in main() func create derived classes in heap

    Qt Code:
    1. int main(int argc, char* argv[])
    2. {
    3. Animal* animal = new Animal();
    4. Animal* wolf = new Wolf();
    5. Animal* fish = new Fish();
    6. Animal* goldfish = new GoldFish();
    7. animal->eat();
    8. wolf->eat();
    9. fish->eat();
    10. goldfish->eat();
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Output should be

    I eat like a generic animal.
    I eat like a wolf!
    I eat like a fish!
    I eat like a goldfish!
    Last edited by AlekseyOk; 23rd May 2012 at 09:00.

  3. The following user says thank you to AlekseyOk for this useful post:

    sonulohani (23rd May 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: C++ virtual functions

    Virtual methods have nothing to do with this situation. If you use your objects through pointers to the base class then the difference between virtual and non-virtual methods will start to show up.

    Qt Code:
    1. int main() {
    2. Animal *animal = new Animal;
    3. Wolf *wolf = new Wolf;
    4. Fish *fish = new Fish;
    5. GoldFish *goldfish = new GoldFish;
    6. animal->eat();
    7. wolf->eat();
    8. fish->eat();
    9. goldfish->eat();
    10. cout << "Now through base class:" << endl;
    11. animal->eat();
    12. ((Animal*)wolf)->eat();
    13. ((Animal*)fish)->eat();
    14. ((Animal*)goldfish)->eat();
    15. delete animal;
    16. delete wolf;
    17. delete fish;
    18. delete goldfish;
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by result
    I eat like a generic animal.
    I eat like a wolf!
    I eat like a fish!
    I eat like a goldfish!
    Now through base class:
    I eat like a generic animal.
    I eat like a generic animal.
    I eat like a generic animal.
    I eat like a generic animal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    sonulohani (23rd May 2012)

  6. #4
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: C++ virtual functions

    Thank You. I understood, what you've specified here.

    Thank You. I understood what you've specified here.

Similar Threads

  1. Problem with virtual functions
    By eekhoorn12 in forum General Programming
    Replies: 4
    Last Post: 8th April 2010, 14:52
  2. QSharedDataPointer and Data with virtual functions
    By niko in forum Qt Programming
    Replies: 0
    Last Post: 2nd February 2010, 09:23
  3. Replies: 3
    Last Post: 17th February 2009, 05:23
  4. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26
  5. why triggerUpdate() is not virtual?
    By gadnio in forum Qt Programming
    Replies: 3
    Last Post: 12th January 2006, 16:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.