Can we have a private constructor ?

Answer Posted / baikunta

yes , we can creat private constructor trrough static
method we can access the class (constructor), for example
singleton, there are a lot of use in design pattern
here is example of single ton
class Singleton {
static Singleton s;
int i;
Singleton(int x) : i(x) { }
void operator=(Singleton&);
Singleton(const Singleton&);
public:
static Singleton& getHandle() {
return s;
}
int getValue() { return i; }
void setValue(int x) { i = x; }
};
Singleton Singleton::s(47);
int main() {
Singleton& s = Singleton::getHandle();
cout << s.getValue() << endl;
Singleton& s2 = Singleton::getHandle();
s2.setValue(9);
cout << s.getValue() << endl;
} ///:~

Is This Answer Correct ?    34 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is multilevel inheritance?

928


What is solid in oops?

791


What are the 3 pillars of oop?

829


What are oops functions?

771


What is abstraction with example?

809


What is use of overloading?

830


What are different types of JVM's? for example we use dalvik jvm for android then what about the remaining operating systems?

1875


Why do we need polymorphism in c#?

865


Is data hiding and abstraction same?

751


What is new keyword in oops?

770


Can main method override?

805


What is object in oop with example?

928


What is a superclass in oop?

900


What does sksksk mean in text slang?

1810


What is the use of oops?

777