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.
Mani kanta said:
2 years ago
Singleton class allow only one object to be created.
(12)
Pumbhdiya Bhaudik said:
2 years ago
Why only singleton class is allowed? Please explain me.
(6)
Prasad PANJAM said:
2 years ago
Please explain this.
(10)
Diku said:
3 years ago
Why is virtual class not accessed through any class? Please tell me.
(6)
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)
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)
Neha said:
6 years ago
I cannot understand this. Please, anyone explain to me.
(5)
Vikram said:
6 years ago
I can't understand this. Please, anyone explain to me.
(2)
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)
Ningaraja G said:
7 years ago
Singleton class can create only one object, all others class we can't create object.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers