0
** My question is how to implement runtime polymorphism in multilevel inheritance?
Conditions:- There are one base class i.e., STUDENT and one derived class STUDY but it's the parent class of IDENTITY (FINAL CLASS) ... STUDENT have 2 member function one is get() that take input from users and disp() that display the input details.... STUDY have 2 member function one is get() that take input from users and disp() that display the input details... ( class STUDY : public STUDENT {.... } ) ...... IDENTITY have 1 member function that is disp() that output the all details including STUDENT AND STUDY disp().... This is my code, please solve this: https://code.sololearn.com/c09hYwIo8yoM/?ref=app
5 odpowiedzi
+ 1
You are inheriting from 'Student' twice in 'Identity'
- first when you specify it in the inheritance list
- second when you inherit from it through the 'Study' class (since it also inherits from 'Student').
This results in ambiguity, since the compiler doesn't know which 'Student' to use when you refer to its members - the one you inherited, or the one that was inherited from 'Study'.
The simplest fix to this is to remove the extra 'Student' inheritance. Inheriting from 'Study' is enough.
`class Identity : public Study { ... };`
You could also use virtual inheritance, which ensures that the derived class inherits from the base class exactly once
(you would need to add `virtual` while inheriting 'Student' in both 'Study' and 'Identity')
class Study : public virtual Student { ... };
class Identity : public virtual Student, public Study { ... }
See virtual inheritance:
https://en.m.wikipedia.org/wiki/Virtual_inheritance
+ 1
SUDIP HOWLADER
(1/2)
You mean why the output of disp() is gibberish?
That's because you did not call get() on ptr after assigning it to the Identity variable ('t'). You only called get() on the Student and Study variables ('f' and 's').
Keep in mind that 'ptr' is just a pointer and 'f', 's' and 't' are 3 different variables in different places in the memory. When you assign 'ptr' to &f and call
ptr->get()
you are NOT making changes to 'ptr', you are *making changes to 'f' through the pointer 'ptr'*.
When you assign 'ptr' to 's', it's now pointing to a different variable, and the previous
ptr->get()
call did not have any effects on 's'
+ 1
SUDIP HOWLADER
(2/2)
So you need to call get() on 'ptr' after you assign it to &t. You will also need to call Student::get() since Identity only inherits get() from Study, and that only takes input in the 'Study' members
ptr = &t;
ptr -> Student::get();
ptr -> get();
The better thing to do here would be to call Student::get() from inside Study::get(). Currently Study inherits from Student, but never initializes it's variables with input. Doing this would make sense because when Study::get() is called, the whole of the object would be initialized. Also, then you would only need to call
ptr->get()
and Study::get() will take care of calling Student::get() as well as take input and initialize it's own members.
0
Thank you.. After your suggestion I change my code little bit... But there's one problem.. This program don't show any error but class Identity disp() function not show... Why? Can you explain it ...
0
Thank you... For guide me...