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. Inheritance classes

Inheritance classes

Scheduled Pinned Locked Moved C / C++ / MFC
oopquestion
3 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

    I have the following code. Why does not my x is displayed. I do not understand, X is returned by the getX() function wich is called from the derived class SecondClass() through an object of type FirstClass(). Initialization of X was done using constructor function FirstClass(int x). I did not want to use the initialization function like setX ().

    #include using namespace std;

    class FirstClass
    {
    private:
    int x;
    public:
    FirstClass()
    {
    cout << "\n Default constructor FirstClass()." << endl;
    }
    FirstClass(int x)
    {
    cout << "\n Constructor FirstClass()." << endl;
    this->x = x;
    cout << "\n X = " << x << endl;
    }
    int getX()
    {
    return x;
    }
    };

    class SecondClass:protected FirstClass
    {
    private:
    int y;
    public:
    SecondClass(int x):FirstClass(x)
    {
    cout << "\n Constructor SecondClass()." << endl;
    }
    void printX(FirstClass& obj)
    {
    cout << "\n X = " << obj.getX() << endl;
    }
    };

    int main()
    {
    FirstClass box1;
    SecondClass box2(100);
    box2.printX(box1);
    return 0;
    }

    D A 2 Replies Last reply
    0
    • K kinderu

      I have the following code. Why does not my x is displayed. I do not understand, X is returned by the getX() function wich is called from the derived class SecondClass() through an object of type FirstClass(). Initialization of X was done using constructor function FirstClass(int x). I did not want to use the initialization function like setX ().

      #include using namespace std;

      class FirstClass
      {
      private:
      int x;
      public:
      FirstClass()
      {
      cout << "\n Default constructor FirstClass()." << endl;
      }
      FirstClass(int x)
      {
      cout << "\n Constructor FirstClass()." << endl;
      this->x = x;
      cout << "\n X = " << x << endl;
      }
      int getX()
      {
      return x;
      }
      };

      class SecondClass:protected FirstClass
      {
      private:
      int y;
      public:
      SecondClass(int x):FirstClass(x)
      {
      cout << "\n Constructor SecondClass()." << endl;
      }
      void printX(FirstClass& obj)
      {
      cout << "\n X = " << obj.getX() << endl;
      }
      };

      int main()
      {
      FirstClass box1;
      SecondClass box2(100);
      box2.printX(box1);
      return 0;
      }

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #2

      You have two instances of FirstClass here - one inside the instance of SecondClass and one is standalone. You used the default constructor for the standalone instance, which does not initialize x. You then printed this uninitialized value. Please note that a class declaration is not the definition of a class instance!

      If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

      1 Reply Last reply
      0
      • K kinderu

        I have the following code. Why does not my x is displayed. I do not understand, X is returned by the getX() function wich is called from the derived class SecondClass() through an object of type FirstClass(). Initialization of X was done using constructor function FirstClass(int x). I did not want to use the initialization function like setX ().

        #include using namespace std;

        class FirstClass
        {
        private:
        int x;
        public:
        FirstClass()
        {
        cout << "\n Default constructor FirstClass()." << endl;
        }
        FirstClass(int x)
        {
        cout << "\n Constructor FirstClass()." << endl;
        this->x = x;
        cout << "\n X = " << x << endl;
        }
        int getX()
        {
        return x;
        }
        };

        class SecondClass:protected FirstClass
        {
        private:
        int y;
        public:
        SecondClass(int x):FirstClass(x)
        {
        cout << "\n Constructor SecondClass()." << endl;
        }
        void printX(FirstClass& obj)
        {
        cout << "\n X = " << obj.getX() << endl;
        }
        };

        int main()
        {
        FirstClass box1;
        SecondClass box2(100);
        box2.printX(box1);
        return 0;
        }

        A Offline
        A Offline
        Albin George
        wrote on last edited by
        #3

        your box2.printX(box1); statement is printing the garbage of box1::x. Note there are no initialization for FirstClass::x in its default constructor. FirstClass box1; will call default constructor for FirstClass . x is garbage here. SecondClass box2(100); will initialize the SecondClas::FirstClass::x to 100. box2.printX(box1); this will try to print the x in object box1, which is garbage.

        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