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 static in oop?
What is oops and its features?
String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?
Can we override main method?
What is abstract class in oop?
Is this job good for future? can do this job post grduate student?
What is the benefit of oop?
Write a program to implement OOPS concepts such as inheritance, polymorphism, friend function, operator overloading?
just right the logic of it 1--> If few people are electing then every time ur candidate should win 2--> arrange books in box, if box carry weight == books weight then take another box..... find the no of box required.
What are the 3 pillars of oop?
What does it mean when someone says I oop?
What is the difference between procedural programming and oops?
How do you use inheritance in unity?
Why is destructor used?
Where You Can Use Interface in your Project