Goldman Sachs Interview Question for Developer Program Engineers






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

make the class final

- deepak November 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

It can be done as follows:

class A
{
private:
A(){}
public:
static A* createObject(){return new A();}
};

main()
{
A* a=A::createObject();
}



This way the class is stopped from being inherited and still can be instantiated.

- DK July 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please explain..how it will work??

- Prashant February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@prashant

He made the constructor of the class private. Now if any of the child class try to create an object of the base class they will not be able to do it. For the second condition (i.e we should be able to make an object of base class) he made a static function which is creating an object of the base class.
Thus both the conditions are fulfilled.

- Spock July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Define the c-or protected

- AL June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Define a class with private constructors and a friend class.
Define the friend class inherited from the previous class.
Now this friend class can not be inherited but its objects can be created.
We can call constructor via its objects because it is a friend class object but
inherited class objects can not call the constructor so no inheritance possible.

- Random guy June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@random guy what we can do in Java??

- kumar June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

final

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

if contructors are private class can not be inherited

- kamal August 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a class with a private destructor cannot be inherited but its object can be made.

- abhireforce June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

class A
{
public:
static A ACons();
protected:
private:
A();
};


A::A()
{
cout<<"In A constrcutor"<<endl;
}

A A::ACons()
{
new A();
}

class B
{
public:
B();
private:
};

B::B()
{
cout<<"In B constrcutor"<<endl;
}


int main()
{
A a=A::ACons();
cout<<"Address of A is"<<&a<<endl;
// A b=new (A::ACons());
getchar();
return 0;
}

- Rajesh Manem July 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am happy to see the solution, but I feel this method only one object can be created, further calls to Acons will only lead to references. Correct me if I am wrong, I will check this in ide anyway

- Pradeep July 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sale DK bose isse acha protected bana leta..farzi dimaag lagata hai...mkl

- Anonymous July 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As I understand we need to make a final class(As in Java), whose object we can create , but cann't be inherited.THis can be achive using virtual inheritence .here the code

// Final class

class final;
class dummy_class  
{
      friend class final;      
      private:
              dummy_class(){};
};

class final :virtual public dummy_class
{
};

int main()
{
 final f;
 return 1;
}

So in above code we are able to create a object of final
now try to inherit it

// Final class

class final;
class dummy_class
{
friend class final;
private:
dummy_class(){};
};


class final :virtual public dummy_class
{
};
class error_class:public final
{
};

int main()
{
final f;
error_class e;
return 1;
}

So when u create a object of inherit class it will throw an error.

- sukhpal July 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain, what role virtual keyword is playing in this case?

- SN July 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If constructor is private, then also can inherit it using public specifier. So DK is correct to make the constructor private.

- Saurabh September 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, if Constructor is protected then it can be inherited.

- Saurabh September 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, Constructor is protected then it can be inherited.

- Saurabh September 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

class NotToBeInherited;
class Base
{
friend class NotToBeInherited;
};

class NotToBeInherited : virtual Base
{

};

Now calss NotToBeInherited is not inheritable but we can construct object of this class. :)

- KD September 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Base
{
private:
Base();
friend class Final;
};

//Since Final is friend of Base, it can access private Constructor.
//So it can be created on stack.
class Final : public Base
{
public:
Final();
};

class Child : public Final
{
public:
Child(); // You can't do this since it Child don't have access to Base constructor
}

- Anonymous October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

best way is to declare the class as final

final class_name
{
//definition of class
}

now when you inherit it it will give error but you can create its object :-)

- jagdish January 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a sealed class where u can not inherit the class but can create an object for the class

Public sealed class A()
{
private int sum()
{
return a+b;
}
}

- Anonymous May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

make the class as final so we can not create the object to class.

- Shrikant Badiger September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

A {
protected:
A();
};

B : A {
}

B cannot be inherited!

- AL June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice tricks!

Would you pls explain why it's not possible to inherit B in your given example? Thanks in advance.

- anon June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actally in c++ the default type is private.so here the access specifier of baseclass is not specified in derived class while inheriting the baseclass. so by default it is private and so the members of all the base class will not accessible by derived class B........

- rekha July 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

making the ctor protected and then inheriting the base class privately won't solve the problem..the constructor wd be called implicitly from the derived class ctor even if its made protected

- newbie September 11, 2011 | 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