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 ambiguity in inheritance?
What polymorphism means?
hi all..i want to know oops concepts clearly can any1 explain??
What does no cap mean?
What is variable example?
Whats oop mean?
write a C++ program for booking using constructor and destructor.
What is inheritance in oop?
What is coupling in oops?
What is the difference between a constructor and a destructor?
What is encapsulation and abstraction? How are they implemented in C++?
Why do pointers exist?
Write a java applet that computes and displays the squares of values between 25 and 1 inclusive and displays them in a TextArea box
What is pure oop?
write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.