Adobe Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 3 vote

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 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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..

- Anonymous June 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why not make the constructor private? It server the need.

- Anon June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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.

- Pavan Dittakavi June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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....

- Himanshu June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- eugene.yarovoi June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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...

- Himanshu June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does it matter if it exists? It's still a pure virtual method if it does. And yes, it does: stackoverflow. com/questions/1219607/why-do-we-need-a-pure-virtual-destructor-in-c

- eugene.yarovoi June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Anonymous June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if someone doesn't use new to build the object?

- eugene.yarovoi June 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And make the destructor protected so that the object can't be allocated in stack...couldn't a subclass still allocate the base class object on the stack?

- eugene.yarovoi July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Jagadish July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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??

- kamal July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No. This is exactly the approach discussed in the top-voted solution. There are still some problems with it. See the discussion there. The basic point is that subclasses are still able to instantiate instances of the base class.

- eugene.yarovoi July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Raj August 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but Derived classes can still instantiate the base class. And you can still use new.

See the discussion in some of the other answers for this question -- we covered this idea already.

- eugene.yarovoi August 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- Anonymous June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

using pure virtual function/concept is restriction here :)

- Anon June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is it possible virtual destructor ??

- ajay June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

moreover destructor is also a method, so making it pure virtual defeats the purpose of this question.

- suhaskulkarni100 September 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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.

- Anonymous June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

use abstract keyword to make a abstract class.
Eg:
class Test abstract
{

}
Class Test will be an abstract class.

- kuber October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

C++ doesn't have such a keyword.

- eugene.yarovoi October 23, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More