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. Managed C++/CLI
  4. Inheritance classes

Inheritance classes

Scheduled Pinned Locked Moved Managed C++/CLI
oopquestion
2 Posts 2 Posters 3 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;
    }

    L 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;
      }

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

      Please do not post the same question in multiple forums.

      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