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?
Virtual class
Abstract class
Singleton class
Friend class
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
104 comments Page 10 of 11.

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.
(2)

Rupali said:   8 years ago
Singleton class is a class that can have only one object at a time.

Akshata said:   7 years ago
Singleton class is a class created only one object (instance) at a time.

Harsh Nandal said:   7 years ago
Singleton class is a class that can have only one object/element/instance at a time and is designed to control object creation.

Ningaraja G said:   7 years ago
Singleton class can create only one object, all others class we can't create object.

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.
(6)

Vikram said:   6 years ago
I can't understand this. Please, anyone explain to me.
(2)

Neha said:   6 years ago
I cannot understand this. Please, anyone explain to me.
(5)

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
(17)

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)


Post your comments here:

Your comments will be displayed after verification.