Morgan Stanley Interview Question
Software Engineer / Developerswe can use so called "virtual constructor idiom", i.e. virtual function that creates a new object inside and returns the pointer on is.
In case of Exemplar we simply create a new object, in case of Clone we copy the existing object to the new one:
class Baseclass
{
public:
Baseclass(){}
virtual Baseclass* clone();
virtual Baseclass* exemplar();
};
Baseclass* Baseclass::clone()
{
return new Baseclass(*this);
}
Baseclass* Baseclass::exemplar()
{
return new Baseclass();
}
class Derivedclass : private Baseclass
{
public:
Derivedclass(){}
virtual Derivedclass* clone();
virtual Derivedclass* exemplar();
};
Derivedclass* Derivedclass::clone()
{
return new Derivedclass(*this);
}
Derivedclass* Derivedclass::exemplar()
{
return new Derivedclass();
}
In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).
so cloning and overriding is sort of virtual function in java.
check - javadecodedquestions.blogspot.com
What is Exemplars?
- Anonymous May 30, 2010