Bloomberg LP Interview Question
Financial Software Developersi agree with desi and i must say what drwolf is also correct meaning thereby,
#. if yuo just make the constructor private then its enough for any deriving class to not make any subclass on that, and we don't need to do other things as mentioned by drwolf, but that doesnot say his answer is wrong, drwolf add extra steps to do the same, which can be avoided.
# yeh, the reference has to be declared as const else there is no point ...
@ Monkey
if you are not interested in discussing the questions plz do not make bogus posts regarding thread duplication...just do a "report duplicate thread" right from the homepage and you're done...
and don't waste your time by keeping track of duplicate threads, i guess you're not here to do that, right? or its the other way round? OMG...!!!
Final Class: (which can not be sub-classed further)
---------------------------------------------------
if you just make the constructor private, we can achieve 'final' effect, but no other code can create objects of the class. This is not intended. So, making constructor private alone will not achieve the goal. So, we need to so something else.
Final Method: (Which can not be over-written)
---------------------------------------------
In Java all the methods by default are virtual. So, the real question here is, in C++ how can we make a virtual function not being re-implemented again in sub-class. In C++ if a method is declared as virtual, it will be virtual in all the hierarchy of classes. How to stop this at some class in the hierarchy? In Java, it is achieved by the 'final' keyword and javac compiler check.
Final Data Member: (Whose value can not be changed)
----------------------------------------------------
This can be achieved by 'const' as desi mentioned.
Anyone who knows the exact answer for "final class" and "final method" implementation in c++??????????????
use virtual inheritance and friend class to solve this.
class FinalLock
{
private:
FinalLock(){};
~FinalLock(){};
friend Final;
};
class Final : public virtual FinalLock
{
};
class D : public Final
{
};
Final fObj; // OK
D dObj // error!! since FinalLock constructor is private
venkat, your idea is correct, but your program is not. The reason is stated in the above compilation error.
The constructor should not be private, but the destructor can be private.
class A {
private:
// A() {};
~A() {};
friend class Final;
};
class FinalA : public virtual A
{
};
class FinalB : public virtual A
{
};
int main() {
FinalA fa;
//FinalB fb; //Error in Compile
return 0;
}
1. Final keyword in Java means, we can not derive any class from final class but we can certainly create object of Final class.
2. We can implement Final in C++ like below. It allows you to create object of Final class but it does not allow you to derive new class.
class Final; // The class to be made final
class MakeFinal // used to make the Final class final
{
private:
MakeFinal() { cout << "MakFinal constructor" << endl; }
friend class Final;
};
class Final : virtual MakeFinal
{
public:
Final() { cout << "Final constructor" << endl; }
};
/*class Derived : public Final // Compiler error
{
public:
Derived() { cout << "Derived constructor" << endl; }
};
*/
int main(int argc, char **argv)
{
Final obj;
}
(1) final class: we cannot inherit from it, that is, not allowable for making subclass. ---> In c++, we make a same effect by declaring the class to private class. Even if we can inherit with private inheritance, there is no meaning because subclass cannot use or access any memeber in that class.
(2) final method: cannot overload it. In c++, we can declare the method as private. Similarily, the subclass cannot access the method.
(3) final variable: one time initialization constant variable. In c++, we can define a reference variable for the same effect as one time initialization and never updated its initial value.
I dont agree with drwolf on the
point 1), you should just make the constructor as private and that will prevent the derivation of the class.
and 3) we need to use the keyword "const" to make sure that the contents of the reference do not change, because const doesnt allow the programmer/application to change the value.
final class in java : does not allow a class to be subclassed.
- sonu February 21, 2009c++ , we can make use of private keyword and make all data in a class private ie. constructor , methods and data needs to be declared in private section. There is no keyword that we can use on class to prevent it from subclassing.
final keyword for variable : in c++ make it const , i guess.
final methods : In c++ , we cannot avoid subclasses overloading or overriding it.
let me know if i m wrong.