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
State what is encapsulation and friend function?
What polymorphism means?
Can we create object of abstract class?
What is super in oop?
What is class and object with example?
Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)
What are the components of marker interface?
What is the main feature of oop?
Which language is pure oop?
Why is polymorphism used?
What is polymorphism programming?
• What are the desirable attributes for memory managment?
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.
What is the importance of oop?
What is stream in oop?