dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: virtual function in derived class

Hybrid View

  1. #1
    Join Date
    May 2018
    Posts
    103

    virtual function in derived class

    Code:
    #include <iostream>
    using namespace std;
    
    class A {
    public:
           A()        {cout << "nuovo A" <<  endl; };
           void virtual f()=0;
           void g() {cout << "g di A" << endl; }
    };
    
    class B: public A {
    public:
           B()        {cout << "nuovo B" <<  endl; };
           void f()  {cout << "f di B" << endl; }
           void g() {cout << "g di B" << endl; }
    
    };
    
    class C: public A {
    public:
          C()	   {cout << "nuovo C" <<  endl; };
          void f()  {cout << "f di C" <<  endl; }
          void g() {cout << "g di C" << endl; }
    };
    
    class D: public C {
    public:
          D()	        {cout << "nuovo D" <<  endl; };
          void f(int x) {cout << x <<  endl; }
    };
    
    int main() {
        A* vet[3];
        vet[0]= new B;
        vet[1]= new C;
        vet[2]= new D;
        for (int j=0; j<3; j++) {
    			vet[j]->f(); 
    			vet[j]->g();
    	}
    }
    If I understood right, when I use virtual function I'm referencing to pointed object.
    vet[2] is A* pointer while the pointed object is D class one.
    Class base is A, D is derived from C, C is derived to A.
    A is abstract class because f is virtual function, so It's virtual both in all its derived class.
    OK my observations?

    so vet[2]->f() should call D::f but this doesn't occur because function wants a parameter and we pass any ones, so what's logic to invoke right function? It's chose the function of one of derived classes where correspond number and type of parameters ?
    I think this one the only missing piece to understand virtual functions.
    Thanks again.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: virtual function in derived class

    Your declaration of f in D is an overload, not an override. If I remember correctly, it even hides the virtual function f inherited from C.

    What's the "right function" to invoke for the D object in this context? If to you that is D::f, then you likely have a design issue: f in A wasn't designed to handle an int argument, so consequently any virtual call of f in its hierarchy cannot handle an int argument, including D. If to you that is C::f, then you should declare f in D (a using declaration will do in the past, but there might be better methods now), but you still run the risk of confusing readers.

    You probably could save yourself a great deal of trouble if you stopped mixing overriding and overloading such that you have to figure out what happens when. To do this, assuming you have a virtual function named foo in a base class, I suggest:
    • Do not overload foo with a non-virtual member function. You're probably going to forget which is which, so don't do it. There might be a use case for overloading foo with another virtual function named foo though.
    • When overriding foo in a derived class, declare foo with both virtual and override keywords. This way, if you make a typo error that would have resulted in an overload instead, the compiler will kindly inform you.
    • Don't declare a function named foo in a derived class except to override foo, i.e., don't overload foo there either.

  3. #3
    Join Date
    May 2018
    Posts
    103

    Re: virtual function in derived class

    Quote Originally Posted by laserlight View Post
    Your declaration of f in D is an overload, not an override. If I remember correctly, it even hides the virtual function f inherited from C.
    This is not project and It's not good programming but It's just exam exercise to understand overloading, virtual functions and classes.
    My target is to understand because It's invoked C::f, perhaps because C class is the base class of D one and It matches f function as both parameters numbers and parameters types ?

  4. #4
    Join Date
    Feb 2017
    Posts
    513

    Re: virtual function in derived class

    Quote Originally Posted by zio_mangrovia View Post
    This is not project and It's not good programming but It's just exam exercise to understand overloading, virtual functions and classes.
    When you have virtual functions in a base class you must put in a virtual destructor too. It's enough to have it in the top base class if there is an inheritance chain (like you have). If it's not there when you delete an object by way of a base class pointer then there will be undefined behavior and your program won't work properly.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,620

    Re: virtual function in derived class

    Quote Originally Posted by zio_mangrovia View Post
    Code:
    #include <iostream>
    using namespace std;
    
    ...
    int main() {
        A* vet[3];
        vet[0]= new B;
        vet[1]= new C;
        vet[2]= new D;
        for (int j=0; j<3; j++) {
    			vet[j]->f(); 
    			vet[j]->g();
    	}
    }
    If I understood right, when I use virtual function I'm referencing to pointed object.
    ...
    1. The is no any virtual function in your code snippet.
    2. Your code has memory leaks!
    Victor Nijegorodov

  6. #6
    Join Date
    May 2018
    Posts
    103

    Re: virtual function in derived class

    Quote Originally Posted by VictorN View Post
    1. The is no any virtual function in your code snippet.
    f is a virtual function see void virtual f()=0;
    If I understand right, f is inherited from its all derived classes.

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

    Re: virtual function in derived class

    In B and C, f() should be defined as override - as it overrides the base A f(). As g() doesn't (A g() is not virtual), then g() can't be defined as override. f(x) in D doesn't override anything - it is a specific function for D.

    Code:
    	void f() override { cout << "f di B" << endl; }
    With a minor display change, the output is:

    Code:
    nuovo A
    nuovo B
    nuovo A
    nuovo C
    nuovo A
    nuovo C
    nuovo D
    
    B
    f di B
    g di A
    
    C
    f di C
    g di A
    
    D
    f di C
    g di A
    Note that f() is polymorphic and g() isn't (as it isn't defined as virtual in the base class).

    For B*, f() invokes from B (polymorphic) and g() invokes from A (as not polymorphic and type of vet is A*).
    For C*, f() invokes from C (polymorphic) and g() invokes from A (as not polymorphic and type of vet is A*).
    For D*, f() invokes from C (polymorphic and D inherits from C and D has no f() ) and g() invokes from A (as not polymorphic and type of vet is A*).
    Last edited by 2kaud; January 21st, 2020 at 05:03 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++17 Compiler: Microsoft VS2019 (16.5.1)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)