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


Please Help Members By Posting Answers For Below Questions

can inline function declare in private part of class?

3658


What language is oop?

591


can we make game by using c

3417


What makes a language oop?

593


Where You Can Use Interface in your Project

1423






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.

695


What is inheritance and how many types of inheritance?

617


What is abstraction in oops?

585


What is the types of inheritance?

602


program for insertion ,deletion,sorting in double link list

2278


If a=5, b=6, c=7, b+=a%c*2. What is the final value of b?

942


What is an advantage of polymorphism?

591


What is oops in simple words?

577


what is the 3 types of system development life cycle

2431


Is enum a class?

604