From ff96644889735263689be207fd60ca9a384a2cbe Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Wed, 1 Jul 2020 14:34:41 +0200 Subject: [PATCH] Ex4.1 feedback: copy member objects in the correct way in copy constructor --- ex4.1/Dialer.hh | 16 ++++++++++++++-- ex4.1/Handset.hh | 8 +++++++- ex4.1/Housing.hh | 7 ++++++- ex4.1/Telephone.hh | 12 +++++------- ex4.1/main.cpp | 5 +++++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/ex4.1/Dialer.hh b/ex4.1/Dialer.hh index 2aeaf04..528fffe 100644 --- a/ex4.1/Dialer.hh +++ b/ex4.1/Dialer.hh @@ -4,6 +4,8 @@ #include "Button.hh" +#define DEFAULT_NUMBER_BUTTONS 12 + class Dialer { public: @@ -14,7 +16,11 @@ public: Dialer(const Dialer& other) { 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() { std::cout << "Dialer Destructor " << this << std::endl ; @@ -25,10 +31,16 @@ private: 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 ; + int buttons_size; } ; diff --git a/ex4.1/Handset.hh b/ex4.1/Handset.hh index c0cd9ea..9e38063 100644 --- a/ex4.1/Handset.hh +++ b/ex4.1/Handset.hh @@ -10,7 +10,13 @@ class Handset { public: 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 ; } private: diff --git a/ex4.1/Housing.hh b/ex4.1/Housing.hh index 4d1334b..0423357 100644 --- a/ex4.1/Housing.hh +++ b/ex4.1/Housing.hh @@ -9,7 +9,12 @@ class Housing { public: 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 ; } private: diff --git a/ex4.1/Telephone.hh b/ex4.1/Telephone.hh index e2d3da0..4d2e51a 100644 --- a/ex4.1/Telephone.hh +++ b/ex4.1/Telephone.hh @@ -12,14 +12,12 @@ public: Telephone() { 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; - - // Copy the data members - Cable cable = t.cable; - Housing housing = t.housing; - Dialer dialer = t.dialer; - Handset handset = t.handset; } ~Telephone() { std::cout << "Telephone Destructor " << this << std::endl; diff --git a/ex4.1/main.cpp b/ex4.1/main.cpp index 263bc75..74de493 100644 --- a/ex4.1/main.cpp +++ b/ex4.1/main.cpp @@ -8,4 +8,9 @@ int main(){ std::cout << std::endl; Telephone t2 = t; + + std::cout << std::endl; + std::cout << "Destructing all objects" << std::endl; + std::cout << std::endl; + }