Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
#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

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.

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

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

![]() |
String to Integer??
|
Batch File Creation with ...
|

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