#c++ overriding multiple times with derrived classes

15 messages · Page 1 of 1 (latest)

compact egret
#

I have Component class and I inherited it with Text class now l want to create costumize MyTextClass but l must override functions that l got from Component class

class Component{
public:
      virtual void onClick(){}
};

class Text: public Component{
public:
    virtual void onClick() override{}
};

class MyTextComponent: public Text{
public:
    void onClick() override{cout<<"they clicked me!"<<endl;}
};```
glad thistleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

ruby crater
#

Yes, what's your problem?

compact egret
#

MyTextComponent doesnt override

compact egret
ruby crater
#

What's the problem?

compact egret
#

l want the override this function void onClick()

compact egret
compact egret
ruby crater
#

What is the error?

molten remnant
#

the code works for me

#

;compile

#include <iostream>
using namespace std;

class Component{
public:
      virtual void onClick(){}
};

class Text: public Component{
public:
    virtual void onClick() override{}
};

class MyTextComponent: public Text{
public:
    void onClick() override{cout<<"they clicked me!"<<endl;}
};

int main() {
    MyTextComponent obj;
    obj.onClick();

    return 0;
}
junior cragBOT
#
Program Output
they clicked me!
compact egret
#

dude in my code ,the code works if l remove onClick function from Text Class

compact egret