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

3rd Jun 2020, 8:37 PM
Saad Saleem
Saad Saleem - avatar
8 Respostas
+ 1
Should be working now https://code.sololearn.com/cxaK71gMaely/?ref=app Inheritance should be (almost always) public
3rd Jun 2020, 9:35 PM
Michi
Michi - avatar
+ 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.
3rd Jun 2020, 9:48 PM
Michi
Michi - avatar
8th Jun 2020, 11:16 AM
Ayushi Patel
Ayushi Patel - avatar
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.
3rd Jun 2020, 8:46 PM
Michi
Michi - avatar
0
Michi I know this But when I use a single object for both operations it gives me error Check the code again
3rd Jun 2020, 8:52 PM
Saad Saleem
Saad Saleem - avatar
0
Michi thanks it's working perfectly but plz explain the steps in main()
3rd Jun 2020, 9:41 PM
Saad Saleem
Saad Saleem - avatar
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();
5th Jun 2020, 1:26 PM
Anthony Maina
Anthony Maina - avatar
0
Anthony Maina explain working of {...};
5th Jun 2020, 5:51 PM
Saad Saleem
Saad Saleem - avatar