Computing.Net > Forums > Programming > C++ private constructor Why?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

C++ private constructor Why?

Reply to Message Icon

Name: ~BeBe~
Date: October 27, 2004 at 09:46:30 Pacific
OS: WinXP
CPU/Ram: 3.00Ghz/512DDR
Comment:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

enum CarSize{
small = 0, medium = 1, large = 2
};

class RentalAccount{
string accountNumber;
string clientName;
int age;
float balance;
// -- Default Constructor
RentalAccount(){}

public:
// -- Constructor
RentalAccount( const string newAccountNumber,
const string newClientName,
const int newAge,
const float newBalance = 0);

// -- Methods

// -- Set, and get Balance
void SetBalance(const float newBalance);
float GetBalance() const;

// -- Calculate Balance
void CalculateBalance(CarSize carSize);

// -- Print Client information
void Print() const;
};


My quuestion is Why did we declare the default constructor in the private section?In the blod and red spot above




Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: October 27, 2004 at 14:39:11 Pacific
Reply:

Well, I would think that would ensure that no code outside of the function can call the constructor without parameters.

That could make sense. Maybe you don't want someone instantiating a RentalAccount object without an account number, client name, etc.

I'm not a C++ expert, but I believe that if that code compiles OK, then my explanation is valid. I always assumed that a constructor couldn't be private, so that's where my question comes in.

I also assumed that if you didn't defined a no parameter constructor, then one couldn't be used, but that apparently is wrong.


0

Response Number 2
Name: Don Arnett
Date: October 27, 2004 at 14:40:50 Pacific
Reply:

My first sentence/paragraph should have read:

Well, I would think that would ensure that no code outside of the CLASS can call the constructor without parameters


0

Response Number 3
Name: Guy
Date: October 28, 2004 at 06:46:48 Pacific
Reply:

I think Don is right.

Also note that if you do not code a private no-args CTOR, then no one inside or outside the class can call it - because it will not exist!

Guy


0

Response Number 4
Name: ~BeBe~
Date: October 28, 2004 at 08:14:12 Pacific
Reply:

Thanks guys


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


String to Integer?? Batch File Creation with ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ private constructor Why?

c++ class constructor problem www.computing.net/answers/programming/c-class-constructor-problem/1216.html

why this works? char pointer in C www.computing.net/answers/programming/why-this-works-char-pointer-in-c/4444.html

Help! Test if a number is Prime in C++ www.computing.net/answers/programming/help-test-if-a-number-is-prime-in-c/2794.html