CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2011
    Posts
    153

    learning about publicly and privately derived classes and access specifiers

    Hi

    Please have a look on the code below. The statement in red is giving all those errors. I'm learning inheritance. So, it means whatever I'm trying to do isn't allowed but what is so bad about it that it's illegal to do? Could you please help with it? Thank you.

    Code:
    // pubpriv.cpp
    // tests publicly- and privately-derived classes
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    ////////////////////////////////////////////////////////////////
    class A                  //base class
    {
       private:
          int privdataA;
       protected:
          int protdataA;
       public:
          int pubdataA;
    };
    ////////////////////////////////////////////////////////////////
    class B : public A       //publicly-derived class
    {
       public:
          int pubdataB = pubdataA;
       public:
          void funct()
             {
             int a;
             //a = privdataA;
             a = protdataA;
             a = pubdataA;
             }
    };
    ////////////////////////////////////////////////////////////////
    
    class C : private A
    {
       public:
          void funct()
             {
             int a;
             //a = privdataA;
             a = protdataA;
             a = pubdataA;
             }
    };
    ////////////////////////////////////////////////////////////////
    
    int main()
    {
       int a;
    
       A objA;
       objA.pubdataA = 5;
       B objB;
    
       //a = objB.privdataA;
       //a = objB.protdataA;
       a = objB.pubdataB;
    
       C objC;
       //a = objC.privdataA;
       //a = objC.protdataA;
       //a = objC.pubdataA;
    
       cout << a << endl;
    
       system("pause;");
       return 0;
    }
    Errors:
    Code:
    23|error: 'A::pubdataA' cannot appear in a constant-expression|
    23|error: ISO C++ forbids initialization of member 'pubdataB'|
    23|error: making 'pubdataB' static|
    23|error: ISO C++ forbids in-class initialization of non-const static member 'pubdataB'|
    ||=== Build finished: 4 errors, 0 warnings ===|

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: learning about publicly and privately derived classes and access specifiers

    Initialization like this:
    Code:
    class B : public A       //publicly-derived class
    {
       public:
          int pubdataB = pubdataA;  // the syntax is not C++, members allowed to be initialized in c-tor only
    must be
    Code:
    class B: public A
    {
    public:
    	int pubdataB;
    	B(): A(), pubdataB(A::pubdataA){}  // explicit initialization in c-tor
    Best regards,
    Igor

  3. #3
    Join Date
    Jun 2008
    Posts
    592

    Re: learning about publicly and privately derived classes and access specifiers

    This is now possible with the new standard of c++ now called c++11 which was just finalized in August this year. c++11 is still being implemented in compilers. Gcc 4.7 and Clang 3.0 supports this new feature.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  4. #4
    Join Date
    Mar 2011
    Posts
    153

    Re: learning about publicly and privately derived classes and access specifiers

    Thank you, Mr. Igor, and Joeman for the information.

    Quote Originally Posted by Igor Vartanov View Post
    Initialization like this:
    Code:
    class B : public A       //publicly-derived class
    {
       public:
          int pubdataB = pubdataA;  // the syntax is not C++, members allowed to be initialized in c-tor only
    must be
    Code:
    class B: public A
    {
    public:
        int pubdataB;
        B(): A(), pubdataB(A::pubdataA){}  // explicit initialization in c-tor
    I understand what you are saying, therefore I have changed the listing but didn't really follow your suggested syntax for c-tor because I'm not familiar with that.

    Please have a look on the parts in red. I wanted "a" to display value of "5" because this is the value pubdataA had:
    Code:
    objA.pubdataA = 5;
    . Where am I going wrong? Please help me with it.

    Code:
    // pubpriv.cpp
    // tests publicly- and privately-derived classes
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    ////////////////////////////////////////////////////////////////
    class A                  //base class
    {
       private:
          int privdataA;
       protected:
          int protdataA;
       public:
          int pubdataA;
    };
    ////////////////////////////////////////////////////////////////
    class B : public A       //publicly-derived class
    {
       public:
          int pubdataB;
       public:
          B() : pubdataB(A::pubdataA)
          {/*empty body*/}
          void funct()
             {
             int a;
             //a = privdataA;
             a = protdataA;
             a = pubdataA;
             }
    };
    ////////////////////////////////////////////////////////////////
    
    class C : private A
    {
       public:
          void funct()
             {
             int a;
             //a = privdataA;
             a = protdataA;
             a = pubdataA;
             }
    };
    ////////////////////////////////////////////////////////////////
    
    int main()
    {
       int a;
    
       A objA;
       objA.pubdataA = 5;
       B objB;
    
       //a = objB.privdataA;
       //a = objB.protdataA;
       a = objB.pubdataA;
    
       C objC;
       //a = objC.privdataA;
       //a = objC.protdataA;
       //a = objC.pubdataA;
    
       cout << a << endl;
    
       system("pause;");
       return 0;
    }
    Code:
    2359128
    Press any key to continue . . .

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: learning about publicly and privately derived classes and access specifiers

    You miss the fact that B objB definition never means it has something common with A objA. The B to A inheritance establishes relation among classes (types) but not objects (class instances). So when you assign some value to objA.pubdataA (change objA internals), objB internals remain untouched. Which seems quite logical.
    Last edited by Igor Vartanov; December 4th, 2011 at 09:27 AM.
    Best regards,
    Igor

  6. #6
    Join Date
    Mar 2011
    Posts
    153

    Re: learning about publicly and privately derived classes and access specifiers

    Thank you, Mr. Igor.

    So, what am I doing in this c-tor?
    Code:
    public:
          B() : pubdataB(A::pubdataA)
          {/*empty body*/}
    Could you please tell me? What's the purpose of assigning an instance of B a data member of class A which is a base class? Thank you

    Best wishes
    H

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: learning about publicly and privately derived classes and access specifiers

    The purpose is to do exactly what I understood from your code:
    Code:
    class B : public A       //publicly-derived class
    {
       public:
          int pubdataB = pubdataA;
    At B class instance initialization assign to its member B::pubdataB the value of its A::pubdataA inherited member. According to your later comments it seems I got your idea completely wrong. Now, to make a more successful iteration in this discussion, you need to explain yourself more thorough I believe.
    Best regards,
    Igor

  8. #8
    Join Date
    Mar 2011
    Posts
    153

    Re: learning about publicly and privately derived classes and access specifiers

    Thank you for the reply.

    What I was trying to do wasn't possible. Suppose you see someone's code written the way I have it. To me, this part
    Code:
    B() : pubdataB(A::pubdataA)
          {/*empty body*/}
    would only make sense when A:: pubdataA is some static or static const member of class A, e.g.
    Code:
    static const int pudataA = 5;
    because otherwise A:: pubdataA would contain some garbage and there is no point in assigning a garbage value to some other data member.

    What do you think? Please help me know. Thank you for the help.

    Best wishes
    H

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: learning about publicly and privately derived classes and access specifiers

    A::pubdataA in the scope of class that inherits to A means: the scope for searching pubdataA variable must be A. And compiler will see whether the member is static or not. In your case it's not, but still accessible from B because of public inheritance.

    I wonder, were you able to find a good book on C++ basics?
    Best regards,
    Igor

  10. #10
    Join Date
    Mar 2011
    Posts
    153

    Re: learning about publicly and privately derived classes and access specifiers

    Quote Originally Posted by Igor Vartanov View Post
    A::pubdataA in the scope of class that inherits to A means: the scope for searching pubdataA variable must be A. And compiler will see whether the member is static or not. In your case it's not, but still accessible from B because of public inheritance.

    I wonder, were you able to find a good book on C++ basics?
    Thank you, Mr. Igor.

    Yes, I have a book. Don't know if it's a good one. I believe my questions are now less theoretical; at least now I first write a code myself, then ask questions about it. In the past, I was going in reverse direction - first asking theoretical questions, then writing a code. Thanks a lot for the help.

    With best wishes
    H

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: learning about publicly and privately derived classes and access specifiers

    because otherwise A:: pubdataA would contain some garbage and there is no point in assigning a garbage value to some other data member.
    Well, you can see in my snippet that B c-tor calls A c-tor first, and that one can initialize pubdataA to be no garbage. So there is a point, if you do the things right.
    Last edited by Igor Vartanov; December 5th, 2011 at 03:58 PM.
    Best regards,
    Igor

  12. #12
    Join Date
    Mar 2011
    Posts
    153

    Re: learning about publicly and privately derived classes and access specifiers

    Hi again

    If I really want to get rid of garbage value, then I would simply do this:
    Code:
    public:
          B() : pubdataB(0)
          {/*empty body*/}
    . To me, as a beginner,
    Code:
    pubdataB(A::pubdataA)
    would make sense when pubdataA is a static member.

    Thank you.

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: learning about publicly and privately derived classes and access specifiers

    Look, I gave you the code just because of replicating yours. To me as a not beginner the code makes sense not much too. I just answered your question.

    While you did not answer mine. What was your idea of int pubdataB = pubdataA;? If you answer this, maybe I will suggest some very different code.

    BTW, the code may be pubdataB(pubdataA) alright. Does this look better for a beginner?
    Last edited by Igor Vartanov; December 6th, 2011 at 02:41 AM.
    Best regards,
    Igor

  14. #14
    Join Date
    Mar 2011
    Posts
    153

    Re: learning about publicly and privately derived classes and access specifiers

    Quote Originally Posted by Igor Vartanov View Post
    Look, I gave you the code just because of replicating yours. To me as a not beginner the code makes sense not much too. I just answered your question.

    While you did not answer mine. What was your idea of int pubdataB = pubdataA;? If you answer this, maybe I will suggest some very different code.

    BTW, the code may be pubdataB(pubdataA) alright. Does this look better for a beginner?
    Hi

    I did answer you but perhaps it wasn't a direct answer. Whatever I was trying to do wasn't possible as you, probably unknowingly, told me. See, in the post quoted below, I myself appear confused as to what this code otherwise might mean. It's clear now and you have helped me. That's all matters. Thank you.

    Best wishes
    H

    Quote Originally Posted by heights View Post
    Thank you, Mr. Igor.

    So, what am I doing in this c-tor?
    Code:
    public:
          B() : pubdataB(A::pubdataA)
          {/*empty body*/}
    Could you please tell me? What's the purpose of assigning an instance of B a data member of class A which is a base class? Thank you

    Best wishes
    H

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