C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 1)
1.
Which of the following type of class allows only one object of it to be created?
Discussion:
104 comments Page 1 of 11.
Yogesh Dahake said:
5 years ago
Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. For example, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. You can create a singleton class using the following code.
Example
#include <iostream>
using namespace std;
class Singleton
{
static Singleton *instance;
int data;
// Private constructor so that no objects can be created.
Singleton()
{
data = 0;
}
public:
static Singleton *getInstance()
{
if (!instance)
instance = new Singleton;
return instance;
}
int getData()
{
return this -> data;
}
void setData(int data)
{
this -> data = data;
}
};
Initialize pointer to zero so that it can be initialized in the first call to getInstance.
Singleton *Singleton::instance = 0;
int main(){
Singleton *s = s->getInstance();
cout << s->getData() << endl;
s->setData(100);
cout << s->getData() << endl;
return 0;
}
output
This will give the output:
0
100
Example
#include <iostream>
using namespace std;
class Singleton
{
static Singleton *instance;
int data;
// Private constructor so that no objects can be created.
Singleton()
{
data = 0;
}
public:
static Singleton *getInstance()
{
if (!instance)
instance = new Singleton;
return instance;
}
int getData()
{
return this -> data;
}
void setData(int data)
{
this -> data = data;
}
};
Initialize pointer to zero so that it can be initialized in the first call to getInstance.
Singleton *Singleton::instance = 0;
int main(){
Singleton *s = s->getInstance();
cout << s->getData() << endl;
s->setData(100);
cout << s->getData() << endl;
return 0;
}
output
This will give the output:
0
100
(17)
Mani kanta said:
2 years ago
Singleton class allow only one object to be created.
(12)
Prasad PANJAM said:
2 years ago
Please explain this.
(10)
Jagdish said:
5 years ago
If a class has only one object created and that is the only object of the class. Then the object is known as the singleton object.
(7)
Shivam Mishra said:
7 years ago
The Singleton class exactly means is that, if we create many objects in a class then it will point to the first object which has been created.
It will not point to other objects.
It will not point to other objects.
(6)
Diku said:
3 years ago
Why is virtual class not accessed through any class? Please tell me.
(6)
Pumbhdiya Bhaudik said:
2 years ago
Why only singleton class is allowed? Please explain me.
(6)
Neha said:
6 years ago
I cannot understand this. Please, anyone explain to me.
(5)
M.SIva Mounika said:
9 years ago
Singleton class is a designed pattern that restricts the instantiation of a class to one object.
Ex: By giving double click on the Application of any video player (Windows,VLC...etc) it will open the Application. if we try to open new application by minimizing previous one, it will display minimized one only, it wont create another new application. Because, It's restricted to the instantiation of a class to only one object.
Ex: By giving double click on the Application of any video player (Windows,VLC...etc) it will open the Application. if we try to open new application by minimizing previous one, it will display minimized one only, it wont create another new application. Because, It's restricted to the instantiation of a class to only one object.
(2)
Vikram said:
6 years ago
I can't understand this. Please, anyone explain to me.
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers