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
Which is not an object oriented programming language?
What is class and object with example?
What is object and example?
write a program to find 2^n+1 ?
What is a class oop?
What is the renewal class?
What is a function in oop?
What is overriding in oop?
#include
What is encapsulation with example?
What is new keyword in oops?
Can private class be inherited?
Plese get me a perfect C++ program for railway/airway reservation with all details.
What is oops in programming?
What is overriding in oops?