top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible to forbid a method in the class to get inherited in child class?

+3 votes
178 views
Is it possible to forbid a method in the class to get inherited in child class?
posted May 1, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

See the following program where we are forcing Dog to provide its own Greeting:
Credit: http://stackoverflow.com/questions/36967347/forbid-inheritance

#include <iostream>
#include <string>

class Animal {
public:
    virtual void SetColor(const std::string & col)
    {
        colour = col;
    }

    virtual void Greeting(const std::string & name) = 0;

protected:
    std::string colour;
};

class Dog : public Animal {

    virtual void Greeting(const std::string & name) override
    {
        std::cout << "Hi I'm a dog. My name is " << name << ". I was forced to provide this function.\n";

    }
};

int main(void)
{
    Animal *ptr = new Dog();
    ptr->Greeting("Fluffy");
    delete ptr;
    return 0;
}
answer May 1, 2016 by Rajan Paswan
Similar Questions
+2 votes

Let's say I have an exe-file (for example computer game) and need to forbid to run it until certain date or time during the day. Any 'manipulations' with file are allowed.

Could you, please, offer me a simple way of how to encode/decode such a file mostly in C or C++?

+1 vote

I am looking for an example with explanation that elaborate the functioning of capacity method.

+3 votes

We using C++ in the media layer and we want to raise the priority of our _AUDIO PROCESSING_ thread, to make sure the audio works well even the CPU is high (totally %98 on an slow Phone).

I have searched on google and find to use _setpriority( PRIO_PROCESS, 0, priority); but _setpriority function is change the process priority instead of the thread.

I know there is Java API to do that, but we need an C++ API. So does there any way to raise an single thread priority from C++ code?

...