473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

private constructor

what is the use of private constructor?
Aug 30 '06 #1
5 4149
rgb
37
private constructors might be similar to private functions where you can make a call to the defined private constructor on one (or more) of your public constructors passing different parameters based on the initialization you needed.
Aug 30 '06 #2
Banfa
9,065 Expert Mod 8TB
I have seen private constructors used to prevent external code calling the default constructor when a class really needs to be initialised as it is constructed. The default constructor is defined to as private to prevent it being called externally.

If a class had a static function to allocate instances of itself for some reason then you might want to declare the constructor(s) private to prevent construction by external code.
Aug 30 '06 #3
We can use private constructors in two cases

1) when you create Base class say Sports and some derived class of this lets say
Cricket, Football ... Now If you want that no one can create object of Sports Base class .. you only store member varibales into it not member functions.. then declare Sports class constructor in private space.. so derived class objects
Cricket ck ;
Football bl;
Can access data available in base class Sports (public data) .. but no one can create object of Sports class directly.

2) When you want to implement SingleTone class Design patterns in that case you are required to declare constructor in private space.
Aug 31 '06 #4
I think private constructor in C++ can't be created.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5. enum CarSize{
  6. small = 0, medium = 1, large = 2
  7. };
  8.  
  9. class RentalAccount{
  10. string accountNumber;
  11. string clientName;
  12. int age;
  13. float balance;
  14. // -- Default Constructor
  15. RentalAccount(){}
  16. public:
  17. // -- Constructor
  18. RentalAccount( const string newAccountNumber,
  19. const string newClientName,
  20. const int newAge,
  21. const float newBalance = 0);
  22.  
  23. // -- Methods
  24.  
  25. // -- Set, and get Balance
  26. void SetBalance(const float newBalance);
  27. float GetBalance() const;
  28.  
  29. // -- Calculate Balance
  30. void CalculateBalance(CarSize carSize);
  31.  
  32. // -- Print Client information
  33. void Print() const;
  34. };
  35.  
  36. int main()
  37. {
  38.     RentalAccount ra;
  39.     ra.SetBalance(10.99);
  40.     ra.GetBalance();
  41.     return 0;
  42. }
I think contradicting my statement, there's a way to stop our class being inherited similar to this code, which would be allowed by C++ . Can anyone tell me how?
Aug 16 '07 #5
Well another use of decalring constructor private is to use the feature of composition. Say there is a class A for which an instantion need not be done make the constructor private and then make the class a member of another class B (composition feature being used here) then at that time private constructors are useful.
Mar 10 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate =...
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
1
by: Andrew Poulos | last post by:
I having some trouble understanding how to make functions private I have created an instance of an object using a constructor function and there are 4 prototypes: Comm = function() { //blah...
7
by: Jan | last post by:
Hi There, I want to insert an object with private copy constructor at the and of a STL list. Is there an easy way to do it like that: list <x> myList; // x has private copy constructor .... ...
4
by: plmanikandan | last post by:
Is static constructor available in C++? Can anybody explain me about private and static constructors in c++ Rgds, Mani
3
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.