Aricent Interview Question
Technical ArchitectsCountry: India
Interview Type: In-Person
That doesn't quite qualify as a non-instantiable (abstract) class, though. It's possible that this is what the interviewer wanted, but it's worth noting that child classes will be able to construct instances of the base class in contexts other than the child class constructors. For example, it would be possible to make a child class have a method that does something like "return new Base()", without any sort of enforcement of the idea that Base should never be instantiated directly.
I don't really have a better approach, in any case.
what about inherited a abstract class? Then the derived class will be a abstract class.
To define a function as abstract in C++ ( with the = 0; ) the function must be declared virtual as well to provide the ability to override the function in the child class. I can't find the line in the spec but, try it in VS and you will:
error C2253: 'Abstract::AbstractFunc' : pure specifier or abstract override specifier only allowed on virtual function
An abstract class has two features, cannot be created without being sub-classed and forces the child class to override a function.
My original suggestion above provided the first. Since the question does not want us to use virtual, then the second feature is not possible.
Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class.
for example: -
class base
{
public:
virtual void makevirtual()=0;
};
class derived:public base
{
};
here, derived has no virtual function and is still an abstract class
Define a class with only protected constructors. This way the only thing that can construct the object is a child class.
- maktech07 November 27, 2012