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