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
can inline function declare in private part of class?
What language is oop?
can we make game by using c
What makes a language oop?
Where You Can Use Interface in your Project
Question: Write a program that prints a paycheck. Ask the program user for the name of the employee, the hourly rate, and the number of hours worked. If the number of hours exceeds 40, the employee is paid “time and a half”, that is, 150 percent of the hourly rate on the hours exceeding 40. Be sure to use stepwi se refine ment and break your solution into several functions. Use the int_name function to print the dollar amount of the check.
What is inheritance and how many types of inheritance?
What is abstraction in oops?
What is the types of inheritance?
program for insertion ,deletion,sorting in double link list
If a=5, b=6, c=7, b+=a%c*2. What is the final value of b?
What is an advantage of polymorphism?
What is oops in simple words?
what is the 3 types of system development life cycle
Is enum a class?