C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.1)

1. 

Which of the following type of class allows only one object of it to be created?

[A]. Virtual class
[B]. Abstract class
[C]. Singleton class
[D]. Friend class

Answer: Option C

Explanation:

No answer description available for this question.

Deep said: (Apr 30, 2012)  
A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.

The singleton design pattern is used whenever the design requires only one instance of a class. Some examples:.

Application classes. There should only be one application class. (Note: Please bear in mind, MFC class 'CWinApp' is not implemented as a singleton class).

Logger classes. For logging purposes of an application there is usually one logger instance required.

Prem Survase said: (May 5, 2012)  
I can't understand properly concept of Singleton class. ?

Padmanaban said: (May 7, 2012)  
I can't get your definition. Can you make me understand easier?

Pankaj said: (May 9, 2012)  
Tell me also what is singleton class?

Rakesh Sharma said: (May 10, 2012)  
Write the use of singleton class in a program in easy to understand language.

Sneha said: (May 15, 2012)  
How to make singleton class?

Rajkiran said: (May 19, 2012)  
A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.

Prasanth said: (May 21, 2012)  
Write a simple exemples in Singleton class?

Sanjaya said: (May 30, 2012)  
class A
{
private:
static A* m_objA;
A(){}; //note constructor is private
public:
~A();
A* GetInstance()
{
if(m_objA)
{
return m_objA;
}
else
{
m_objA = new A();
return m_objA;
}
}
};

Deepi said: (Jun 18, 2012)  
What is the use of singleton class?

Sudhir said: (Jul 8, 2012)  
To answer in simple words, singleton class is the one having only one instance of it created. This is the condition to be satisfied by a class to be singleton.

Maha said: (Jul 19, 2012)  
Difference between virtual and friend class ?

Manivannan said: (Jul 25, 2012)  
What is a singleton class ?

Animesh Priyadarshi said: (Aug 2, 2012)  
In object-oriented programming , a singleton class is a class that can have only one object (an instance of the class) at a time. For example, using Visual C++ , the "Application" class is an example of a singleton class. You can only create only one object of an Application class.

Nithya said: (Aug 4, 2012)  
What is the purpose of singleton ?

Susi said: (Aug 9, 2012)  
Tell me also what is singleton class ?

Difference between virtual and friend class ?

How to make singleton class ?

I can't get your definition. Can you make me understand easier ?

Ramya said: (Aug 17, 2012)  
How to use singleton class in our program?

Greeshma said: (Aug 19, 2012)  
Can you explane brifly like how to use a singleton class in a program?

Animesh Priyadarshi said: (Aug 20, 2012)  
@ nithya if in a class we don't want more than one object then we can choose singleton class..its purpose is to create only one object at a time

Nilesh said: (Aug 25, 2012)  
Using any class we can create any number of objects 1, 2, 3. So on then why to use singleton class?

V.Sriram said: (Aug 27, 2012)  
I can't understand concept of singleton class? let you to explain?

Priyanka said: (Aug 29, 2012)  
I can't understand concept of singleton class? please explain with example.

Vai said: (Sep 13, 2012)  
Whenever we a class gets loaded only one single class class object gets created for that class which is singleton for that particular class.

Kanika said: (Sep 15, 2012)  
I don't know about singleton class..
and shouldn't it be protected class????

Dattatraya Dhokane said: (Oct 1, 2012)  
Can I get more info about singltone class.

Uma said: (Nov 19, 2012)  
A class which is having only single object is called singleton.

Chandrababu said: (Dec 19, 2012)  
What is the use of singleton class? please give me simple explanation.

Bibhishan said: (Dec 21, 2012)  
Singleton class has only one instance this concept given in the design pattern.

Lalit Kumar Soni said: (Apr 25, 2013)  
I can't understand following statement:

static A* m_objA;

And Also term Singleton & Longer class.

Lovetoprogram said: (May 15, 2013)  
A Singleton class is a design pattern, which creates only a single instance at any time.

When is it required:

Based on the requirement.

Example : Singletons are often used to control access to resources such as database connections or sockets.

Suppose u have licence for only one connection for database, A singleton connection obj makes sure that only one connection can be made at any time.

How this can be implemented:

1. Have a static method, creates the instance if the instance does not exists. If the instance already there then returns the reference to the existing obj.

2. Have a private constructor.

Why Static method:

No obj is required to call static method and memory is used only when required.

private constructor is to make sure that object is not created in any other way.

I explained as per my knowledge. Hope this would be useful.

Manish Singh said: (Jun 1, 2013)  
When class having only one class is called singleton class;

Means that single=means one it means that required of one object.

Kiran said: (Jun 8, 2013)  
So the class derived as private is called singleton class?

Kamal Dhaker said: (Jun 18, 2013)  
How we create singleton class?

Sonu said: (Aug 1, 2013)  
What is meant by singleton class?

Ananadan B said: (Aug 8, 2013)  
How to make singleton class?

Shaju said: (Aug 22, 2013)  
Singleton means only one instance or object at a time can be created.

Why using (for eg: only one person can open a mailbox from a system using a web browser from a system).

Shaju said: (Aug 22, 2013)  
Making constructor as private it will not allow any other function even main to create an object. Only inside class, object can be created.

Rohit said: (Sep 11, 2013)  
What is use of this mainly in c++?

Ramya said: (Oct 7, 2013)  
I expect that friend, virtual class doesn't access more than one object.

Lakshmi.T.R said: (Oct 25, 2013)  
Is there any other special features of singleton classes?

T.Srinivas said: (Nov 16, 2013)  
Above example in that one getInstance word used what is mean by GetInstance and A*GetInstance how it come nd wts mean by A*GetInstance please anybody Explain clearly?

Madhavi Y.S. said: (Nov 18, 2013)  
What I mean by singleton class?

Nitin H said: (Dec 5, 2013)  
How we know the class is singleton class?

Keshworjit Meitei said: (Dec 12, 2013)  
Could you explain me about the singleton?

Siva said: (Dec 13, 2013)  
I can't understand your definition. Can you easy to say?

Ganesh Vahinde said: (Jan 5, 2014)  
The singleton class is such class which has only single instance, so that it's know as singleton class.

Awadh said: (Jan 5, 2014)  
Please elaborate the functioning and use of this class, not getting properly.

Thanks.

Saedlovely said: (Mar 7, 2014)  
Yeah singleton class is the one only to be created an object.

Kirya said: (Apr 10, 2014)  
When is to call for the singleton class in the coding of C++ program?

Binaryworld-Powerofeverything said: (Apr 16, 2014)  
Everyone just wants to understand that if the Singleton class is a concept like 'virtual class' - for e.g. then how you will implement that singleton class?

- Any keyword is used?

- Any methods declaration inside that class will tell us that it is a singleton one? (e.g. Static methods).

- When the object of that class is created, will the compiler warn us not to do so?

- And some other questions.

Abc said: (Apr 22, 2014)  
The names of variables, function, labels and various other user-defined objects are called what?

Sush said: (May 1, 2014)  
Please explain in detail, what is singleton class?

Ranjeet said: (Jun 17, 2014)  
If the constructor is private how we can create instance of class?

Diksha said: (Jun 23, 2014)  
What us the proper meaning of instances and instantiated?

Kirshana Sagar Nim said: (Jul 30, 2014)  
Tell me also what is singleton class?

Mkn said: (Aug 8, 2014)  
Where we are using singleton class?

Ankit said: (Aug 9, 2014)  
What is singleton class?

Shonaya said: (Aug 14, 2014)  
Same question is here what is singleton class is actually mean ?

Implementation ?

Sabiha said: (Aug 19, 2014)  
Please briefly explain about the singleton class in simple words.

Jesuspriya said: (Aug 28, 2014)  
What is singleton class?

Ans: A class which is having single object known as singleton class.

Deepu George Jacob said: (Aug 28, 2014)  
Singleton class mainly used to limit object creations. Singleton class return only single object for all the declarations.

Jayachandra Reddy said: (Sep 7, 2014)  
Do we need to follow any other syntax for singleton classes?

Pankaj Kumar said: (Oct 13, 2014)  
Please explain real world example of singleton class ?

Pinki said: (Nov 6, 2014)  
What is virtual, abstract, and friend class? Can you explain me?

Tanaji said: (Jan 27, 2015)  
Hello can we give me details about singleton class?

Riddhi said: (Feb 21, 2015)  
Can we say inline function & singleton class are same?

Udayabhaskar said: (Mar 5, 2015)  
How Singleton class is useful in real time applications?

Subhadev said: (Mar 11, 2015)  
Why use single tone class in a program? What is the benefit?

Rajeesh said: (Mar 14, 2015)  
Singleton class is useful in real time application. Take the example of a real time application of Hospital administration system. Whenever you register in the Hospital database, your object is created and you will be provided with a unique number. This ID is only refers to you created only once in lifetime. Just imagine that you are going to a Hospital first time and to see a Physician. Then you are registering your name in the database only once. Next time, if you plan to visit a Cardiologist in the same hospital. Do you think that your ID needs to be created again? Not all right. Because you have already registered before there is no need to create another object again.

Here comes the singleton class concept. We can create only instance that serves the purpose in every time.

Hope you understand the concept well.

Sangeetha said: (Apr 4, 2015)  
Object created to the only one class called singleton class.

Sima said: (Jul 4, 2015)  
I cannot understand the concept of singleton class I think it can be virtual class.

Soori said: (Jul 10, 2015)  
Nice example @Rajeesh.

Divya said: (Aug 10, 2015)  
In singleton class, what about the variable. Is there we use private constructor?

Pradeep Kumar said: (Aug 10, 2015)  
How can we say the given class is singleton not?

Hari Narayanan said: (Aug 18, 2015)  
If it is singleton, explain others?

Aparna said: (Sep 21, 2015)  
Singleton set is used for only one instance of the class.

Singleton means only one instance or object at a time can be created.

Singleton set example:

public class singleton();
{
private static singleton instance;
private singleton()
{

}

Public static singleton instance (this is one and only one way to obtain any instance of the singleton set).
{
get
{
if(instance==null)
{
instanece = new singleton(); (memory will be allocated only on first call to the 'instance' property)
}
return instance;
}
}

Davis said: (Sep 30, 2015)  
Only single class can be inherited, no multiple inheritance can take place here.

Varsha said: (Oct 3, 2015)  
Please explain in this question in details.

Raj said: (Feb 22, 2016)  
What is friend class? Explain?

Shri said: (Mar 10, 2016)  
I too didn't understand this singleton class.

Bhargav said: (Apr 2, 2016)  
Can we use any other class, other than singleton class?

Raj said: (Jun 7, 2016)  
Singleton used in the development of an application like public chat, cricket score.

Kalaivani said: (Jun 24, 2016)  
Give more explanation for singleton class with clear example.

Vedanti said: (Jun 26, 2016)  
@Kalaivani.

Singleton class is one whose only one instance (object) can be created. The object is declared globally so that we can use it anywhere in the program.

But this should be avoided as per the fundamental principles like encapsulation or data hiding. Singleton class can also be created by using all static methods which require no instance to call a method.

Eg:-

In the case of an atm machine when we insert our card, an instance is created for our account and all the transactions are created using that single instance of that card.

Prema said: (Jul 6, 2016)  
Define singleton class?

Rahul Thete said: (Jul 20, 2016)  
The Singleton's purpose is to control object creation, limiting the number of obejcts to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.

For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.

S.Jayachitra said: (Sep 17, 2016)  
What is the different between the singleton class and virtual class?

Rajeev said: (Dec 3, 2016)  
Why virtual class not access through any class?

Shantanu said: (Feb 3, 2017)  
Not getting the meaning of Singleton. Please describe it clearly.

Anni said: (Feb 6, 2017)  
What is an abstract class?

Describe it.

M.Siva Mounika said: (Feb 28, 2017)  
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.

Rupali said: (Aug 31, 2017)  
Singleton class is a class that can have only one object at a time.

Akshata said: (Apr 21, 2018)  
Singleton class is a class created only one object (instance) at a time.

Harsh Nandal said: (Jul 28, 2018)  
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: (Sep 16, 2018)  
Singleton class can create only one object, all others class we can't create object.

Shivam Mishra said: (Oct 16, 2018)  
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.

Vikram said: (Jul 26, 2019)  
I can't understand this. Please, anyone explain to me.

Neha said: (Aug 18, 2019)  
I cannot understand this. Please, anyone explain to me.

Yogesh Dahake said: (Apr 30, 2020)  
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

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.