CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2018
    Posts
    158

    call derived class function

    Code:
    #include <iostream>
    using namespace std;
    
    class A{
           protected:
                int x; int y;
            public:
                A(){ x=1, y=2; };
                void stampa () {cout << x << '\t' << y << endl; };
    };
    
    class B: public A
    {
            int y;
        public:
            B() { x=3; y=4; };
    };
    
    int main(){
       B* obj = new B; 
       obj->stampa(); 
    }

    Before stampa was called, A base class has x=3, y=2 while B derived class has x=3, y=4
    class B inherits stampa() function so when I invoke obj->stampa(), from B class, because I print y value of class A ?

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

    Re: call derived class function

    Simply, stampa() is part of class A and shows A's values.

    Add stampa() to class B then you get x=3, y = 4
    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)

  3. #3
    Join Date
    May 2018
    Posts
    158

    Re: call derived class function

    Quote Originally Posted by 2kaud View Post
    Simply, stampa() is part of class A and shows A's values.

    Add stampa() to class B then you get x=3, y = 4
    Thanks for your answer.
    But B is derived class so I thought stampa() belongs to B class too, because both X and stampa() are inherited from A class.
    Where I'm making mistake ? I'd like to understand my reasoning.

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

    Re: call derived class function

    B can use A public stampa() as B is public derived from base A. But as coded stampa() is in class A and uses A's values. For stampa() to use B's values it also needs to be part of B.

    How are you learning C++?
    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)

  5. #5
    Join Date
    May 2018
    Posts
    158

    Re: call derived class function

    Quote Originally Posted by 2kaud View Post
    But as coded stampa() is in class A and uses A's values.
    Thanks to your explanation, everything has finally become clear to me.
    It's scope problem, but executed code is inside class where method is called.

    How are you learning C++?
    These are Italian university exercises for exams, now I'm learning derived class.

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

    Re: call derived class function

    You may find this site of interest

    https://www.learncpp.com/
    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)

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