1
0
Fork 0

Ex4.1 feedback: copy member objects in the correct way in copy constructor

This commit is contained in:
Eric Teunis de Boone 2020-07-01 14:34:41 +02:00
parent bf04d5a4b2
commit ff96644889
5 changed files with 37 additions and 11 deletions

View file

@ -4,6 +4,8 @@
#include "Button.hh" #include "Button.hh"
#define DEFAULT_NUMBER_BUTTONS 12
class Dialer { class Dialer {
public: public:
@ -14,7 +16,11 @@ public:
Dialer(const Dialer& other) { Dialer(const Dialer& other) {
std::cout << "Dialer Copy Constructor " << this << std::endl ; std::cout << "Dialer Copy Constructor " << this << std::endl ;
init(); init(other.buttons_size);
// Copy the buttons
for ( int i = 0; i < other.buttons_size; i++ ) {
buttons[i] = other.buttons[i];
}
} }
~Dialer() { ~Dialer() {
std::cout << "Dialer Destructor " << this << std::endl ; std::cout << "Dialer Destructor " << this << std::endl ;
@ -25,10 +31,16 @@ private:
void init() void init()
{ {
buttons = new Button[12]; this->init( DEFAULT_NUMBER_BUTTONS );
}
void init( int num_buttons )
{
buttons_size = num_buttons;
buttons = new Button[buttons_size];
} }
Button* buttons ; Button* buttons ;
int buttons_size;
} ; } ;

View file

@ -10,7 +10,13 @@ class Handset {
public: public:
Handset() { std::cout << "Handset Constructor " << this << std::endl ; } Handset() { std::cout << "Handset Constructor " << this << std::endl ; }
Handset(const Handset&) { std::cout << "Handset Copy Constructor " << this << std::endl ; } Handset(const Handset& h) :
mouthpiece( h.mouthpiece ),
earpiece( h.earpiece ),
cable( h.cable )
{
std::cout << "Handset Copy Constructor " << this << std::endl ;
}
~Handset() { std::cout << "Handset Destructor " << this << std::endl ; } ~Handset() { std::cout << "Handset Destructor " << this << std::endl ; }
private: private:

View file

@ -9,7 +9,12 @@ class Housing {
public: public:
Housing() { std::cout << "Housing Constructor " << this << std::endl ; } Housing() { std::cout << "Housing Constructor " << this << std::endl ; }
Housing(const Housing&) { std::cout << "Housing Copy Constructor " << this << std::endl ; } Housing(const Housing& h) :
chassis( h.chassis ),
shell( h.shell )
{
std::cout << "Housing Copy Constructor " << this << std::endl ;
}
~Housing() { std::cout << "Housing Destructor " << this << std::endl ; } ~Housing() { std::cout << "Housing Destructor " << this << std::endl ; }
private: private:

View file

@ -12,14 +12,12 @@ public:
Telephone() { Telephone() {
std::cout << "Telephone Constructor " << this << std::endl; std::cout << "Telephone Constructor " << this << std::endl;
} }
Telephone(const Telephone& t ) { Telephone(const Telephone& t ) :
cable( t.cable ),
housing( t.housing ),
dialer( t.dialer ),
handset( t.handset ) {
std::cout << "Telephone Copy Constructor " << this << std::endl; std::cout << "Telephone Copy Constructor " << this << std::endl;
// Copy the data members
Cable cable = t.cable;
Housing housing = t.housing;
Dialer dialer = t.dialer;
Handset handset = t.handset;
} }
~Telephone() { ~Telephone() {
std::cout << "Telephone Destructor " << this << std::endl; std::cout << "Telephone Destructor " << this << std::endl;

View file

@ -8,4 +8,9 @@ int main(){
std::cout << std::endl; std::cout << std::endl;
Telephone t2 = t; Telephone t2 = t;
std::cout << std::endl;
std::cout << "Destructing all objects" << std::endl;
std::cout << std::endl;
} }