Please Help!! This is an C++ problem. Needs to be done with words and explain wh
ID: 640017 • Letter: P
Question
Please Help!! This is an C++ problem. Needs to be done with words and explain why.
Class Details
Sample Driver
Mailbox
void setSize( int amount );
int getSize( );
void setAddress( string address );
string getAddress();
int getContents();
void pickupMail();
int deliverMail( int pieces );
int main() {
// Let's use the Mailbox
cout << "Welcome to Howie's Post Service!" << endl;
Mailbox m;
m.setAddress( "1900 Pico Boulevard" );
m.setSize( 12 );
int postage = m.deliverMail( 10 );
m.pickupMail( );
return( 0 );
}
Sally The Programmer is working to improve the Mailbox class we have been working with in this course. She decides to make a parameterless constructor so that driver code can conveniently say:
Mailbox m;
and have everything still compile and work properly. In her implementation of the parameterless constructor, she writes:
Mailbox::Mailbox() {
// empty...
}
What is wrong with her approach and what would you recommend she do to avoid potential problems?
Class Details
Sample Driver
Mailbox
Mailbox();Mailbox( string address );
void setSize( int amount );
int getSize( );
void setAddress( string address );
string getAddress();
int getContents();
void pickupMail();
int deliverMail( int pieces );
int my_Size;
int my_Contents; #include <iostream>
#include "Mailbox.h"
using namespace std;
int main() {
// Let's use the Mailbox
cout << "Welcome to Howie's Post Service!" << endl;
Mailbox m;
m.setAddress( "1900 Pico Boulevard" );
m.setSize( 12 );
int postage = m.deliverMail( 10 );
m.pickupMail( );
return( 0 );
}
Explanation / Answer
Mailbox::Mailbox() {
my_Address = "";
my_Size = 0;
my_contents = 0;
}
is the correct way to go
since, in your Mailbox class there are three member variables.
Constructors are defined to initialize member variables.
so, It would work well If you initialize member variables. Otherwise your member variables will just hold some garbage values.