question i understand not
Yes, you can. Making a constructor private ensures that no other class can instantiate that class you just created with a private constructor. It is usually used in Singleton Patterns.
Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors
no we cannot initialize a constructor in private in order to call a constructor from outside of a class it must be a public member.in order to create an object we should call the constructor .so only private members can implement outside of the class.
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) // ... };
no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }
jacks mums fit!
Not possible in C.
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.
You cannot invoke a constructor explicitly. It will get invoked implicitly when you call the new keyword on the class to create an object of the class. Ex: private ClassExample obj = new ClassExample(); here this new keyword usage on the ClassExample class will invoke the constructor of this class and create an object of that class.
C is not an object-oriented programming language so there is no friend keyword let alone friend constructors. In C++, however, constructors can be declared friends. Consider the following code where the class Y default constructor has private access to X::foo() because the Y::Y() constructor is explicitly declared a friend of class X. Note that Y must be defined before X can be defined, thus X must be forward declared. #include<iostream> class X; // fwd declaration class Y { public: Y() { X x; x.foo(); } // X::foo is private, but Y::Y() is a friend. }; class X { friend Y::Y(); // friend constructor private: void foo() {} };
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.
A constructor is a function in C which has the same name of the class. The constructor can be used to initialize some function.