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
What is multilevel inheritance?
What is solid in oops?
What are the 3 pillars of oop?
What are oops functions?
What is abstraction with example?
What is use of overloading?
What are different types of JVM's? for example we use dalvik jvm for android then what about the remaining operating systems?
Why do we need polymorphism in c#?
Is data hiding and abstraction same?
What is new keyword in oops?
Can main method override?
What is object in oop with example?
What is a superclass in oop?
What does sksksk mean in text slang?
What is the use of oops?