answersLogoWhite

0


Best Answer

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the use of private constructor in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


In C plus plus What function is called to initialize a class?

Class initialisation is normally handled by the class constructor(s). Every constructor has an optional initialisation section between the declaration and the body of the constructor. This is generally used to call specific base class constructors, but can be used to initialise any member variables via their own constructors. Member variables may alternatively be initialised in the body of the constructor, but this is really only necessary when member pointers need to be allocated new memory. For those classes that have many members and many constructors, the initialisation may be handled by a private member method called by each constructor in order to simplify maintenance during development. However, when the class is finalised, the private member method will generally be replaced with formal initialisation sections in each constructor.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.

Related questions

Is Constructor Can be Private and Protective in c?

question i understand not


Can you write own constructor in c plus plus?

Yes.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


Can you declare constructor as a private in c plus plus?

Yes. However, like any other private member, a private constructor can only be accessed by the class itself (typically via a static member function) or by one of its friends. There are very few cases where private constructors are appropriate, one of the most common being the need to suppress the compiler-generated copy construction of a base class. However, since C++11, suppressed constructors can simply be deleted, thus making error messages much more meaningful to users of your class. For example, instead of the following: class A { public: A (); // default constructor private: A (const A&); // suppress copy constructor (can still be invoked by the class and its friends) // ... }; You'd now use the following: class A { public: A (); // default constructor A (const A&) =delete; // suppress copy constructor (cannot be invoked at all) // ... };


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


What are different type of constructor in java?

Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


Write a program in c plus plus with constructor?

// constructor program to add two number's // program written by SuNiL kUmAr #include<iostream.h> #include<conio.h> class constructor { private: int a,b; public: constructor(int m,int n); int sum(); }; constructor::constructor(int m,int n) { a=m; b=n; } int constructor::sum() { int s; s=a+b; return (s); } int main() { int x,y; clrscr(); cout<<"enter two number's to add \n"; cin>>x>>y; class constructor k (x,y); cout<<"sum of two number's is = "<<k.sum(); getch(); return (0); }


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


In C plus plus What function is called to initialize a class?

Class initialisation is normally handled by the class constructor(s). Every constructor has an optional initialisation section between the declaration and the body of the constructor. This is generally used to call specific base class constructors, but can be used to initialise any member variables via their own constructors. Member variables may alternatively be initialised in the body of the constructor, but this is really only necessary when member pointers need to be allocated new memory. For those classes that have many members and many constructors, the initialisation may be handled by a private member method called by each constructor in order to simplify maintenance during development. However, when the class is finalised, the private member method will generally be replaced with formal initialisation sections in each constructor.


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.