0
C++ Classes Code Issue
Problem : There are two classes Base and derived I want To set the variable in Base Class Then access the variable value from derived class My Code Not Working correctly it gives value 0 instead of what i enter https://code.sololearn.com/c9hWuIRQzr9Q/?ref=app
8 ответов
+ 1
Should be working now
https://code.sololearn.com/cxaK71gMaely/?ref=app
Inheritance should be (almost always) public
+ 1
At first I create an object of the derived class storage. Then i create a base class (insurgency) reference and link the created storage object to it (this is possible, as a storage object also is a insurgency object -> inheritance).
Now on the storage object all moethods of Insurgency and Storage can be called, on the insurgency reference only Insurgency methods can be called (as it does not know, that it holds even a derived version of it)
Hope that is understandable.
If you just assign it, without a reference (or pointer):
Storage st{};
Insurgency in = st;
It will be copied and therefore two objects exist now. If one (e.g. in) is changed, the other one (e.g. st) stays the same.
0
The problem is, you are using two different objects In and se. Each of them get their own variables. You change the value of In's PolicyNum and then print se's one, which is still 0.
0
Michi I know this
But when I use a single object for both operations it gives me error
Check the code again
0
Michi thanks it's working perfectly but plz explain the steps in main()
0
Saad Mughal first use public inheritance like this
class Storage:public Insurance
Then use the Storage object se to call SetInsurancePolicy,,
that is,se.SetInsurncePolicy();
0
Anthony Maina explain working of {...};