How to avoid a class from instantiation?
Answers were Sorted based on User's Feedback
Answer / vinay bondade
Have a Pure Virtual function in the class. This will not
allow to create an object of the class and hence cannot be
instatiated. But its a different thing to inherit the class
and give definition to Pure Virtual function, But the class
alone cannot be instantiated.The class hence,is also called
Abstract Base class.
Is This Answer Correct ? | 11 Yes | 1 No |
Answer / chandrakant
make the class Abstract it will not be instantiated
and the pure virtual function is used to avoid the base call
function to be get called if we make a function as virtual
only a derived class function will get called
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / sanish joseph
Make an abstract class.abstract class cant b intialized.
I think so....
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / achal
Making a constructor private is perfectly legal.
But now u can't create instance of that class.
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / maniac_2004
if you want to construct a class whos constructor is private
then use a static method which constructs the object and
returns the pointer
class A
{
private: A();
public:
static A * createInstance();
};
A* A::getInstance()
{
return new A();
}
main(){ A::createInstance();}
This is similar to the "singleton" pattern...
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / vidushi
Make the class abstract
abstract class abc
{
.....
.....
};
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / ravi
We can aviod a class from instantiation by making all
variants of constructors as private. We can provide user
defined function like create member function under public
scope and call this private construcotrs from this create
function to create an object for that class.
Is This Answer Correct ? | 3 Yes | 5 No |
Answer / rsn
Yes, I agree with Ravi. Classes can be prevented from
instantiating and also from getting inherited using provate
constructors. The initialisation can be done using member
functions in which case it is called "clone" functions(I
hope so, Pls correct me if i'm wrong!).
Is This Answer Correct ? | 2 Yes | 4 No |
How the keyword struct is different from the keyword class in c++?
What is the importance of mutable keyword?
What is the difference between Class and Structure?
40 Answers HP, IBM, Samsung, TCS,
what are function pointers?
Badboy is defined who has ALL the following properties: 1. Does not have a girlfriend and is not married. 2. He is not more than 23 years old. 3. The middle name should be "Singh" 4. The last name should have more than 4 characters. 5. The character 'a' should appear in the last name at least two times. 6. The name of one of his brothers should be "Ram" Write a method: boolean isBadBoy(boolean hasGirlFriend , boolean isMarried, int age , String middleName , String lastName , String[] brotherName); isHaveGirlFriend is true if the person has a girlfriend isMarried is true if the person is married age is the age of the person middleName is the middle name of the person lastName is the last name of the person brotherName is the array of the names of his brothers
What will strcmp("Astring", "Astring"); return a) A positive value b) A negative value c) Zero
Why c++ is not a pure oop language?
Is swift a good first language?
What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator?
What is near, far and huge pointers? How many bytes are occupied by them?
What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?
Why do we need templates?