virtual functions

This is a discussion on virtual functions within the C++ Programming forums, part of the General Programming Boards category; I have one large class with member functions that do very similar things -- for example, like finding the height ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    virtual functions

    I have one large class with member functions that do very similar things -- for example, like finding the height of a tree but depend on which type of tree, etc. Now I am trying to clean up my code, so that I use more than one class: one class for each tree.

    So if I want to use the same name for a function in both classes but have different implementations I would create virtual functions?

    Also, when I do that I keep getting an error that says cannot define member function ‘a_Tree::find_height’ within ‘b_Tree’, where a_Tree and b_Tree are two different classes...

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, you'd use virtual functions in a base class:
    Code:
    class TreeBase
    {
    public:
        virtual DrawTree() = 0;  // Pure virtual function, must be overridden.
    };
    
    class MapleTree  :  public TreeBase
    {
    public:
        virtual DrawTree() { ... }
    };
    
    class PineTree  :  public TreeBase
    {
    public:
        virtual DrawTree() { ... }
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    In my base class I have a function print() that prints the heights of all trees. Print() calls find_height() for my base class and derived classes. But find_height() is a virtual function, so it only calls the base class find_height() and outputs a compile error on the others... how do I make print() understand which find_height function I am referring to?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The entire purpose of virtual functions is that it knows which find_height you refer to, so if you get different results, you're doing something wrong. Show some code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Are trees really so different that each type needs its own class?? I don't think so. Member variables in the tree class could very well differentiate the type of the tree. Drawing trees is not different enough from tree to tree to warrant a new class or new method per tree.

    I would think something like this would suffice:

    Code:
    struct TreeData
    {
        int height;
        int num_branches;
        int num_leaves;
        ...
        ...
    };
    
    class ITree
    {
       public:
          virtual ~ITree() { }
          
          virtual void drawTree() = 0;
          virtual void setTreeType(const TreeData &data) = 0;
          virtual TreeData getTreeType() const = 0;
    };
    
    class Tree
    {
        public:
           Tree();
           virtual ~Tree();
          
           virtual void drawTree();
           virtual void setTreeType(const TreeData &data);
           virtual TreeData getTreeType() const;
    };
    You could derive from Tree and use it as a base class for a generic implementation of a tree and then call it from the derived to implement new types of trees but I think that's overkill.

    Just my two cents.
    Last edited by VirtualAce; 10-18-2008 at 12:58 PM.

  6. #6
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Bubba: I have it working with one class it is just ugly. I have two find_height() functions -- one for the first tree; the other for the second tree. I also have two top() functions, etc.

    Here is a general example of what I have that gives errors when I use virtual functions:
    Code:
    class r_tree
    {
          public:
              //... 
               virtual int find_height(int node*);
               void print()
          private:
               //...
    };
    
    class b_tree : public r_tree
    {
           public:
                int find_height(int, int, node*)
           private:
               //...
    };
    
    .
    .
    .
    
    void r_tree::print()
    {
           int h, h2;
           h = find_height(list);
           h2 = find_height(num, num, list2);  //error
    }

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    All versions of a virtual function must have the same signature. The extra int parameters are not allowed.

    And "int node*" is just nonsense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Also even though the current standard for C++ does not force you to use virtual in front of all functions that are inherited as virtual from the base I consider it a good idea. It is very sad that this issue was not addressed in earlier standards.

    Code:
    class A
    {
       public:
       ...
       virtual void Foo();
    };
    
    class B:public A
    {
       public:
         ...
         void Foo();
    };
    Foo is virtual in B but it is not apparent without looking back at the base class header file. Since I believe the new standard will enforce this it would be good idea to get into the habit of using virtual in front of all functions in a derived that are inherited from the base as virtual.

    Virtual is telling the compiler and the programmer that the function can be overridden in derived classes provided it has the exact same signature. If the signatures are different the compiler has no way of knowing which function to call. If you see virtual with an = 0 at the end of the signature that is telling you and the compiler that function MUST be defined in the derived and also indicates you cannot instantiate the base. These are called pure virtual functions. If the base class is composed entirely of pure virtual functions then it is a pure virtual base or an abstract base class. Usually these also will have no member variables with all public pure virtual functions. This is essentially an interface even though C++ does not natively have any keyword to denote interfaces like C# and Java do.
    Last edited by VirtualAce; 10-18-2008 at 02:27 PM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If the base class is composed entirely of pure virtual functions then it is a pure virtual base or an abstract base class.
    Most people refer even to a class with just a single pure virtual function as "abstract".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Yes it is. Sorry if that was unclear. For me there is a difference between an abstract class and an abstract class intended to be used as an interface. However they are both considered abstract.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 10:48 AM
  2. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 03:13 PM
  3. Thread creation on virtual functions
    By gustavosserra in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2004, 08:03 AM
  4. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21