Adobe Interview Question
Country: India
Interview Type: In-Person
but then, thse subclasses will be able to create objects of that class..and abstract class means no object of that class should be created..
@Anon, If you mark the constructor as private, if you extend that class,..then you wont be able to instantiate the derived class as well.
So, i think there is no way to implement abstract class without using pure virtual functions since
1. if we make the constructor private then derived classes cannot be instantiated....they too become abstract class.
2. if we make constructor protected, then derived class will be able to create (instantiate ) objects of base class....which is ofcourse we dont want....
any other way would be higly appreciated....
Seems that way to me too, unless C++ has some sort of trickery for doing it. Nothing's coming to mind right now.
Generally most classes that are logically thought of as being abstract have one or more abstract methods, so this isn't a huge problem in most situations.
does there exists a concept called "pure virtual destructor" in c++ according to standard....can anyone post a reliable link here...since i have studied in some books that pure virtual destructor does not exists...bt i am able to implement it on my gcc...so much confused right now...
Overload the operator new in the class and throw exception from there. And make the destructor protected so that the object can't be allocated in stack.
If there is a derived class from the above base class then it will redefine the operator new so that the base class new will not be called.
With this case we can't make the object of base class .. the only thing is the compiler will not give any error but at runtime if somebody try to make object of that exception will be thrown.
We can make a class as a abstract without pure virtual function by making constructor and virtual destructor as protected.
Example Code:
#include <iostream>
using namespace std;
class cBaseClass
{
public:
virtual void Print();
protected:
cBaseClass(){cout<<"cBaseClass Constructor"<<endl;};
virtual ~cBaseClass(){cout<<"cBaseClass Destructor\n"<<endl;};
};
void cBaseClass::Print()
{
cout << "cBaseClass Print" << endl;
}
class cDerivedClass:public cBaseClass
{
public:
cDerivedClass(){cout<<"cDerivedClass Constructor\n";};
~cDerivedClass(){cout<<"cDerivedClass Destructor\n";};
void Print();
};
void cDerivedClass::Print()
{
cout << "cDerivedClass Print" <<endl;
}
int main() {
//abstract abc; //If you Try to create an object compiler reports an error message
cBaseClass *lObj=new cDerivedClass();
lObj->Print();
return 0;
}
Output:
cBaseClass Constructor
cDerivedClass Constructor
cDerivedClass Print
We can make the base class by protecting the constructor... this way it can not be instantiated. Now if you derive a class from it, you wont be able to instantiate it as well. To overcome this problem, we can make derived class constructor public. Now everything look fine. isn't it??
We can make the base class destructor as protected .
#include<iostream>
using namespace std;
class Base
{
int a;
protected:
~Base()
{
}
void BaseClass()
{
cout<<"Base Class";
}
public:
Base()
{
cout<<"Base Const";
}
};
class Der : public Base
{
public:
Der():Base()
{
}
};
int main()
{
//Base b; //we cant create Base class object , it gives error.
Der d;
}
We can make destructor pure virtual, there by the base class can't be instantiated , at the same time the destrutor can't be overridden..it need not be implemented in derived class and acts like an abstract class
I was going to make a retort like #define "new MyAbstractClass" screwyou_mwwwahaha_syntax_error, but it doesn't look like there's a way for a macro to do a search and replace on two words at once. Which makes sense, because macros parse things on a token-by-token basis. And it's also possible someone would instantiate the class without using new.
Making all the constructors protected is one way to prevent people from instantiating the class without subclassing it first. Of course there have to be no factory static functions, etc. too.
- eugene.yarovoi June 25, 2012