Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. protected access specifier

protected access specifier

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kinderu
    wrote on last edited by
    #1

    Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300); Here Y = 200, X = 300; X is initialized by calling the constructor BaseClass(x) FirstClass inherits BaseClass with protected access specifier.

    #include
    using namespace std;

    class BaseClass
    {
    private:
    int x;
    public:
    BaseClass()
    {
    cout << "\n BaseClass(default) called." << endl;
    }
    BaseClass(int x)
    {
    cout << "\n BaseClass(parameters) called." << endl;
    this->x = x;
    }
    int getX()
    {
    return x;
    }
    };

    class FirstClass:protected BaseClass
    {
    private:
    int y;
    public:
    FirstClass(int y, int x):BaseClass(x)
    {
    cout << "\n FirstClass(parameters) called." << endl;
    this->y = y;
    }
    int getY()
    {
    return y;
    }
    };

    int main()
    {
    BaseClass obj1;
    BaseClass obj2(100);
    cout << "\n X = " << obj2.getX() << endl;
    FirstClass obj3(200, 300);
    cout << "\n Y = " << obj3.getY() << endl;
    return 0;
    }

    L D 2 Replies Last reply
    0
    • K kinderu

      Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300); Here Y = 200, X = 300; X is initialized by calling the constructor BaseClass(x) FirstClass inherits BaseClass with protected access specifier.

      #include
      using namespace std;

      class BaseClass
      {
      private:
      int x;
      public:
      BaseClass()
      {
      cout << "\n BaseClass(default) called." << endl;
      }
      BaseClass(int x)
      {
      cout << "\n BaseClass(parameters) called." << endl;
      this->x = x;
      }
      int getX()
      {
      return x;
      }
      };

      class FirstClass:protected BaseClass
      {
      private:
      int y;
      public:
      FirstClass(int y, int x):BaseClass(x)
      {
      cout << "\n FirstClass(parameters) called." << endl;
      this->y = y;
      }
      int getY()
      {
      return y;
      }
      };

      int main()
      {
      BaseClass obj1;
      BaseClass obj2(100);
      cout << "\n X = " << obj2.getX() << endl;
      FirstClass obj3(200, 300);
      cout << "\n Y = " << obj3.getY() << endl;
      return 0;
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See Member Access Control (C++)[^].

      K 1 Reply Last reply
      0
      • L Lost User

        See Member Access Control (C++)[^].

        K Offline
        K Offline
        kinderu
        wrote on last edited by
        #3

        I know the protected access specifier. In my code I need a function into FirstClass() to call function getX() from BaseClass, and I tried but I did not succeed.

        L 1 Reply Last reply
        0
        • K kinderu

          I know the protected access specifier. In my code I need a function into FirstClass() to call function getX() from BaseClass, and I tried but I did not succeed.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You have been given the answer to this twice already in your original post two days ago[^]. You are trying to access an uninitialised variable in an object (obj1). Build your program and step through the code with the debugger and you will be able to see exactly what happens.

          1 Reply Last reply
          0
          • K kinderu

            Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300); Here Y = 200, X = 300; X is initialized by calling the constructor BaseClass(x) FirstClass inherits BaseClass with protected access specifier.

            #include
            using namespace std;

            class BaseClass
            {
            private:
            int x;
            public:
            BaseClass()
            {
            cout << "\n BaseClass(default) called." << endl;
            }
            BaseClass(int x)
            {
            cout << "\n BaseClass(parameters) called." << endl;
            this->x = x;
            }
            int getX()
            {
            return x;
            }
            };

            class FirstClass:protected BaseClass
            {
            private:
            int y;
            public:
            FirstClass(int y, int x):BaseClass(x)
            {
            cout << "\n FirstClass(parameters) called." << endl;
            this->y = y;
            }
            int getY()
            {
            return y;
            }
            };

            int main()
            {
            BaseClass obj1;
            BaseClass obj2(100);
            cout << "\n X = " << obj2.getX() << endl;
            FirstClass obj3(200, 300);
            cout << "\n Y = " << obj3.getY() << endl;
            return 0;
            }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            kinderu wrote:

            Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300);

            Taken at face value, this really makes no sense. The member variable x belongs to the BaseClass class. So, are you wanting to access x in the context of obj3, or from within BaseClass, FirstClass, or main()? As it stands right now, your code does indeed assign x a value, but then you have no code to print it (like you do for y).

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            K 1 Reply Last reply
            0
            • D David Crow

              kinderu wrote:

              Can anyone tell me how can I access the x in the following statement: FirstClass obj3(200, 300);

              Taken at face value, this really makes no sense. The member variable x belongs to the BaseClass class. So, are you wanting to access x in the context of obj3, or from within BaseClass, FirstClass, or main()? As it stands right now, your code does indeed assign x a value, but then you have no code to print it (like you do for y).

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              K Offline
              K Offline
              kinderu
              wrote on last edited by
              #6

              this code is what I want to achieve

              #include using namespace std;

              class BaseClass
              {
              private:
              int x;
              public:
              BaseClass()
              {
              cout << "\n BaseClass(default) called." << endl;
              }
              BaseClass(int x)
              {
              cout << "\n BaseClass(parameter) called." << endl;
              this->x = x;
              }
              int getX()
              {
              return x;
              }
              };
              class FirstClass:protected BaseClass
              {
              private:
              int y;
              public:
              FirstClass(int y, int x):BaseClass(x)
              {
              cout << "\n FirstClass(parameter) called." << endl;
              this->y = y;
              }
              int getY()
              {
              return y;
              }
              int getXX()
              {
              return getX();
              }
              };

              int main()
              {
              BaseClass obj1;
              BaseClass obj2(100);
              cout << "\n X = " << obj2.getX() << endl;
              FirstClass obj3(200, 300);
              cout << "\n Y = " << obj3.getY() << endl;
              cout << "\n X = " << obj3.getXX() << endl;
              return 0;
              }

              D L 2 Replies Last reply
              0
              • K kinderu

                this code is what I want to achieve

                #include using namespace std;

                class BaseClass
                {
                private:
                int x;
                public:
                BaseClass()
                {
                cout << "\n BaseClass(default) called." << endl;
                }
                BaseClass(int x)
                {
                cout << "\n BaseClass(parameter) called." << endl;
                this->x = x;
                }
                int getX()
                {
                return x;
                }
                };
                class FirstClass:protected BaseClass
                {
                private:
                int y;
                public:
                FirstClass(int y, int x):BaseClass(x)
                {
                cout << "\n FirstClass(parameter) called." << endl;
                this->y = y;
                }
                int getY()
                {
                return y;
                }
                int getXX()
                {
                return getX();
                }
                };

                int main()
                {
                BaseClass obj1;
                BaseClass obj2(100);
                cout << "\n X = " << obj2.getX() << endl;
                FirstClass obj3(200, 300);
                cout << "\n Y = " << obj3.getY() << endl;
                cout << "\n X = " << obj3.getXX() << endl;
                return 0;
                }

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                So what's the problem?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                1 Reply Last reply
                0
                • K kinderu

                  this code is what I want to achieve

                  #include using namespace std;

                  class BaseClass
                  {
                  private:
                  int x;
                  public:
                  BaseClass()
                  {
                  cout << "\n BaseClass(default) called." << endl;
                  }
                  BaseClass(int x)
                  {
                  cout << "\n BaseClass(parameter) called." << endl;
                  this->x = x;
                  }
                  int getX()
                  {
                  return x;
                  }
                  };
                  class FirstClass:protected BaseClass
                  {
                  private:
                  int y;
                  public:
                  FirstClass(int y, int x):BaseClass(x)
                  {
                  cout << "\n FirstClass(parameter) called." << endl;
                  this->y = y;
                  }
                  int getY()
                  {
                  return y;
                  }
                  int getXX()
                  {
                  return getX();
                  }
                  };

                  int main()
                  {
                  BaseClass obj1;
                  BaseClass obj2(100);
                  cout << "\n X = " << obj2.getX() << endl;
                  FirstClass obj3(200, 300);
                  cout << "\n Y = " << obj3.getY() << endl;
                  cout << "\n X = " << obj3.getXX() << endl;
                  return 0;
                  }

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  That should all work; what happens when you run this code?

                  K 1 Reply Last reply
                  0
                  • L Lost User

                    That should all work; what happens when you run this code?

                    K Offline
                    K Offline
                    kinderu
                    wrote on last edited by
                    #9

                    When I run the code shows: BaseClass(default) called. BaseClass(parameter) called. X = 100 BaseClass(parameter) called. FirstClass(parameter) called. Y = 200 X = 300 wich is ok

                    L 1 Reply Last reply
                    0
                    • K kinderu

                      When I run the code shows: BaseClass(default) called. BaseClass(parameter) called. X = 100 BaseClass(parameter) called. FirstClass(parameter) called. Y = 200 X = 300 wich is ok

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Excellent, you have solved your problem.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups