2

I have created one Interface class and want to force derived class to must implement this function.(moto of interface class).

class ICommand
{
public:
    virtual void Printit() = 0;
};

Now one derived class:

class DerivedCommand : public ICommand
{
public:
    void Printit() {printf("hi\n");};
    void thisisnew()
    {
        printf("new\n");
    }
};

And in the main func I want to access one newly added the Derived class func through Interface class pointer, But i cann't as per c++ theory I read.

ICommand *p = new DerivedCommand();
//DerivedCommand *pD = new DerivedCommand();
p->Printit();
//p->thisisnew(); //'thisisnew' : is not a member of 'ICommand'

Now here my question is, If I want to force derived class to implement one/two func (as in interface class) and also add it's own func, and want to use new func through interface class pointer - can we do that?

I have only theoretical knowledge in C++, so if i am wrong here plz correct. thanks, for help

0 Answers0