C++ Polymorphism | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ Polymorphism

class A { public: A(){}; virtual void L() const = 0; } class B : public A { public: B() : A() { }; void L() const override { // Do Stuff; } void Other() { // Other Stuff; } } A* U = new B; A->L(); // This Call Works A->Other(); // This Call is not working can anyone explain. Please

1st Aug 2021, 10:33 AM
Eyob
Eyob - avatar
2 Answers
0
Could I ask you why are you calling Other() from A class(parent)? A class doesn't have Other method in this code.
1st Aug 2021, 10:53 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
For Example class A { public: A(){}; virtual void L() const = 0; virtual void Other() = 0; } class B : public A { public: B() : A() { }; void L() const override { // Do Stuff; } void Other() override { // Other Stuff; } } class C : public A { public: // Now, Here I don't Need to Implement "Other()" But I do need L() // So to avoid implementing Other() in this class I remove it from Abstract // class and implement it in Class B alone since It Make sense inside class B // And I Do Need Class A functionality here (Kind of "Not all of them") }
1st Aug 2021, 11:14 AM
Eyob
Eyob - avatar