CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    about virtual functions and polymorphism

    if we do a virtual functions(polymorphism) why we need re-declare the functions(when we create a new class derived from other)?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: about virtual functions and polymorphism

    Did you try to define a virtual functions in the derived class without declaring it there?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2013
    Posts
    55

    Re: about virtual functions and polymorphism

    ---

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about virtual functions and polymorphism

    Quote Originally Posted by VictorN View Post
    Did you try to define a virtual functions in the derived class without declaring it there?
    Code:
     class a
    { 
         public:
              virtual void print(){};
    
    };
    
    class b: public a
    { 
    
    };
    
    void b::print()
    {
         cout << "hi";
    }
    error: print not member of class b.
    so how can i create polymorphism without re-declare the functions?
    Last edited by Cambalinho; May 2nd, 2014 at 03:16 PM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: about virtual functions and polymorphism

    Have a look at this simple example
    Code:
    #include <iostream>
    using namespace std;
    
    class a
    {
    public:
    	virtual void print(){ cout << "a print\n"; }
    
    };
    
    class b : public a
    {
    public:
    	virtual void print(){ cout << "b print\n"; }
    
    };
    
    void display(a *cl) { cl->print(); }
    
    int main()
    {
    	a *pa = new a;
    	b *pb = new b;
    
    	display(pa);
    	display(pb);
    }
    Different class pointers are passed to the same function display() and display() calls the appropriate function depending upon the class passed at run time. This is known as late binding (as opposed to early binding which is decided at compile time).

    See http://stackoverflow.com/questions/5...ymorphism-in-c
    http://www.cplusplus.com/doc/tutorial/polymorphism/
    http://www.learncpp.com/cpp-tutorial...rived-objects/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: about virtual functions and polymorphism

    Quote Originally Posted by Cambalinho View Post
    Code:
     class a
    { 
         public:
              virtual void print(){};
    
    };
    
    class b: public a
    { 
    
    };
    
    void b::print()
    {
         cout << "hi";
    }
    error: print not member of class b.
    so how can i create polymorphism without re-declare the functions?
    You declared it in a, but implemented it in b. You need to declare and implement it in the base class. If you need to change the behavior in the derived class, you need to declare and implement it again there too.

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: about virtual functions and polymorphism

    Quote Originally Posted by GCDEF View Post
    You declared it in a, but implemented it in b. You need to declare and implement it in the base class. If you need to change the behavior in the derived class, you need to declare and implement it again there too.
    so is there anyway for create functions in base, and change the behavior(if i need) in derived class without re-declare them?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: about virtual functions and polymorphism

    You can create an abstract base class which has at least one pure virtual function (declared but with no body). Then have the definition(s) for the virtual function(s) in the derived classes.
    See http://msdn.microsoft.com/en-us/libr...=vs.60%29.aspx
    http://stackoverflow.com/questions/1...tual-explained

    From my earlier simple example
    Code:
    #include <iostream>
    using namespace std;
    
    class a
    {
    public:
    	virtual void print() = 0;
    
    };
    
    class b : public a
    {
    public:
    	virtual void print(){ cout << "b print\n"; }
    
    };
    
    class c : public a
    {
    public:
    	virtual void print(){ cout << "c print\n"; }
    
    };
    
    void display(a *cl) { cl->print(); }
    
    int main()
    {
    	b *pb = new b;
    	c *pc = new c;
    
    	display(pb);
    	display(pc);
    }
    Class a is an abstract class and has no body for the function declaration. Class b and c derived from class a contain the required function body. Note that as class a is an abstract class you can't now instantiate from it. So
    Code:
    a *pa = new a;
    is not correct and will generate a compiler error.

    However, if you want to be able to instantiate from the base class then you will need to define the function in the base class and to redefine in each derived class where a different function implmentation is neded.

    In this example
    Code:
    #include <iostream>
    using namespace std;
    
    class b
    {
    public:
    	virtual void print(){ cout << "b print\n"; }
    
    };
    
    class c : public b
    {
    public:
    	virtual void print(){ cout << "c print\n"; }
    
    };
    
    class d : public b
    {
    public:
    
    };
    
    void display(b *cl) { cl->print(); }
    
    int main()
    {
    	b *pb = new b;
    	c *pc = new c;
    	d *pd = new d;
    
    	display(pb);
    	display(pc);
    	display(pd);
    }
    although class d is derived from class b, d doesn't have a redefinition of print and so the base definition from class b is used.
    Last edited by 2kaud; May 3rd, 2014 at 10:41 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: about virtual functions and polymorphism

    Quote Originally Posted by Cambalinho View Post
    so is there anyway for create functions in base, and change the behavior(if i need) in derived class without re-declare them?
    No, but I don't see why that would be a problem.

  10. #10
    Join Date
    Aug 2013
    Posts
    55

    Re: about virtual functions and polymorphism

    Quote Originally Posted by Cambalinho View Post
    so is there anyway for create functions in base, and change the behavior(if i need) in derived class without re-declare them?
    What's so horrible about having to specify the signature of the function you override in the derived class?

    If you want to minimize the typing you can even put the implementation directly in the definition, like.

    Code:
    class a { 
    public:
        virtual void print() {};
    };
    
    class b : public a { 
    public:
        void print() {cout << "hi";} // implementation in the class definition
    };
    Here you supply the absolute minimum amount of information that's theoretically possible, namely where the function belongs and what it's supposed to do. No redundancy whatsoever.

    Another advantage is that the function becomes a candidate for inlining so this practice can even lead to faster code. This suggests the function shouldn't be too big (even though I'm certain a compiler wouldn't bloat the code by inlining a huge function in zillions of places.)
    Last edited by zizz; May 5th, 2014 at 07:51 AM.

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: about virtual functions and polymorphism

    if you want to create a new function implementation in the derived class, then you need to redeclare what you'll be reimplementing.

    THe only way out of that is not using the normal virtual function system, but creating function pointers members in base (and initialize them in the constructor),
    then change the function pointers in derived constructor, they'll have to be global/local scope if you don't want to change the header though.

    The above is basically how virtual function works as well, but with all the nice compiler assistance to do the nitty gritty.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured