From be584af6c396dd3734181da6e1b139b2c5635884 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Tue, 17 Dec 2019 15:05:27 +0100 Subject: [PATCH] Ex4.*, did not finish Ex4.2 --- ex4.1/Cable.hh | 3 +++ ex4.1/Dialer.hh | 23 +++++++++++++++++++---- ex4.1/Telephone.hh | 2 +- ex4.1/main.cpp | 7 +++++++ ex4.1/tree.txt | 41 +++++++++++++++++++++++++++++++++++++++++ ex4.2/CaloCell.cc | 18 ++++++++++++++++++ ex4.2/CaloCell.hh | 31 +++++++++++++++++++++++++++++++ ex4.2/CaloGrid.cc | 18 ++++++++++++++++++ ex4.2/CaloGrid.hh | 42 ++++++++++++++++++++++++++++++++++++++++++ ex4.2/Calorimeter.hh | 9 +++++++++ ex4.2/Point.hh | 35 +++++++++++++++++++++++++++++++++++ ex4.2/main.cpp | 13 +++++++++++++ ex4.3/String.cc | 30 ++++++++++++++++++++++++++++++ ex4.3/String.hh | 9 ++++++++- ex4.3/main.cpp | 35 +++++++++++++++++++++++++++++++++++ 15 files changed, 310 insertions(+), 6 deletions(-) create mode 100644 ex4.1/main.cpp create mode 100644 ex4.1/tree.txt create mode 100644 ex4.2/CaloCell.cc create mode 100644 ex4.2/CaloCell.hh create mode 100644 ex4.2/CaloGrid.cc create mode 100644 ex4.2/CaloGrid.hh create mode 100644 ex4.2/Calorimeter.hh create mode 100644 ex4.2/Point.hh create mode 100644 ex4.2/main.cpp create mode 100644 ex4.3/String.cc create mode 100644 ex4.3/main.cpp diff --git a/ex4.1/Cable.hh b/ex4.1/Cable.hh index 78a7132..adb89ef 100644 --- a/ex4.1/Cable.hh +++ b/ex4.1/Cable.hh @@ -1,4 +1,6 @@ +#ifndef CABLE_HH +#define CABLE_HH #include class Cable { @@ -11,3 +13,4 @@ public: private: } ; +#endif diff --git a/ex4.1/Dialer.hh b/ex4.1/Dialer.hh index 996545b..2aeaf04 100644 --- a/ex4.1/Dialer.hh +++ b/ex4.1/Dialer.hh @@ -7,13 +7,28 @@ class Dialer { public: - Dialer() { std::cout << "Dialer Constructor " << this << std::endl ; } - Dialer(const Dialer&) { std::cout << "Dialer Copy Constructor " << this << std::endl ; } - ~Dialer() { std::cout << "Dialer Destructor " << this << std::endl ; } + Dialer() { + std::cout << "Dialer Constructor " << this << std::endl ; + init(); + } + Dialer(const Dialer& other) { + std::cout << "Dialer Copy Constructor " << this << std::endl ; + + init(); + } + ~Dialer() { + std::cout << "Dialer Destructor " << this << std::endl ; + delete[] buttons ; + } private: - Button buttons[12] ; + void init() + { + buttons = new Button[12]; + } + + Button* buttons ; } ; diff --git a/ex4.1/Telephone.hh b/ex4.1/Telephone.hh index 33a6db0..371d826 100644 --- a/ex4.1/Telephone.hh +++ b/ex4.1/Telephone.hh @@ -10,7 +10,7 @@ class Telephone { public: Telephone() { std::cout << "Telephone Constructor " << this << std::endl ; } - Telephone(const Telephone&) { std::cout << "Telephone Copy Constructor " << this << std::endl ; } + Telephone(const Telephone& t ) { std::cout << "Telephone Copy Constructor " << this << std::endl ; } ~Telephone() { std::cout << "Telephone Destructor " << this << std::endl ; } private: diff --git a/ex4.1/main.cpp b/ex4.1/main.cpp new file mode 100644 index 0000000..1d527dc --- /dev/null +++ b/ex4.1/main.cpp @@ -0,0 +1,7 @@ +#include "Telephone.hh" + +int main(){ + Telephone t; + + Telephone t2 = t; +} diff --git a/ex4.1/tree.txt b/ex4.1/tree.txt new file mode 100644 index 0000000..f874fdc --- /dev/null +++ b/ex4.1/tree.txt @@ -0,0 +1,41 @@ +========================================================================== +a) +========================================================================== + + +-----------+ + | Telephone | + +-----------+ + | + | + +------------>-------->-+-----<---------<-------+ + | | | | ++-------+ +---------+ +--------+ +---------+ +| Cable |->-| Handset | | Dialer | | Housing | ++-------+ +---------+ +--------+ +---------+ + | | | + +-->-----+--<-+ | +---------------+ + | | | | | ++-----------+ +----------+ +--------+ +---------+ +-------+ +| Moutpiece | | Earpiece | | Button | | Chassis | | Shell | ++-----------+ +----------+ +--------+ +---------+ +-------+ + +========================================================================== +b) +========================================================================== + +In file included from Handset.hh:7, + from Telephone.hh:7: +Cable.hh:4:7: fout: redefinition of ‘class Cable’ + 4 | class Cable { + | ^~~~~ +In file included from Telephone.hh:4: +Cable.hh:4:7: note: previous definition of ‘class Cable’ + 4 | class Cable { + | ^~~~~ + +Header protectors missing in Cable.hh + +========================================================================== +e) +========================================================================== +Yes, it does. diff --git a/ex4.2/CaloCell.cc b/ex4.2/CaloCell.cc new file mode 100644 index 0000000..b4e4f57 --- /dev/null +++ b/ex4.2/CaloCell.cc @@ -0,0 +1,18 @@ +//CaloGrid.cc +#include +#include "CaloGrid.hh" + +CaloCell* CaloGrid::cell(int x, int y ) { + if ( x > nx or x < 0 ) { + std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ; + return nullptr; + } + + if ( y > ny or y < 0 ) { + std::cout << "CaloGrid::cell() Error: out of grid (y)" << std::endl ; + return nullptr; + } + + return cells[ x*nx + y ] ; +} + diff --git a/ex4.2/CaloCell.hh b/ex4.2/CaloCell.hh new file mode 100644 index 0000000..30a396d --- /dev/null +++ b/ex4.2/CaloCell.hh @@ -0,0 +1,31 @@ +//CaloCell.hh +#ifndef CALOCELL_HH +#define CALOCELL_HH + + +class CaloCell +{ + public: + CaloCell() { init( 0, 0 ); } + CaloCell( double energy, int ID ) { + init( energy, ID ); + } + // No need for Constructor or Destructor + + double getEnergy() const { return energy; } + bool setEnergy( double new_energy ) { return (energy = new_energy) ; } + + int getId() const { return ID ; } + bool setId( int new_id ) { return ( ID = new_id ) ; } + + + private: + void init( double init_energy, int ID ) + { + setEnergy( init_energy ); + setId( ID ); + } + int ID ; + double energy ; +}; +#endif diff --git a/ex4.2/CaloGrid.cc b/ex4.2/CaloGrid.cc new file mode 100644 index 0000000..3c52230 --- /dev/null +++ b/ex4.2/CaloGrid.cc @@ -0,0 +1,18 @@ +//CaloGrid.cc +#include +#include "CaloGrid.hh" + +CaloCell* CaloGrid::cell(int x, int y ) { + if ( x > nx or x < 0 ) { + std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ; + return nullptr; + } + + if ( y > ny or y < 0 ) { + std::cout << "CaloGrid::cell() Error: out of grid (y)" << std::endl ; + return nullptr; + } + + return cells[ x * nx + y ] ; +} + diff --git a/ex4.2/CaloGrid.hh b/ex4.2/CaloGrid.hh new file mode 100644 index 0000000..ed1da6b --- /dev/null +++ b/ex4.2/CaloGrid.hh @@ -0,0 +1,42 @@ +//CaloGrid.hh + +#include "CaloCell.hh" + +#ifndef CALOGRID_HH +#define CALOGRID_HH +class CaloGrid +{ + public: + CaloGrid( int nx, int ny ) { + init(nx, ny ); + } + CaloGrid( const CaloGrid& other ) { + // Set everything using init to allocate memory + init(other.nx, other.ny ) ; + + // Copy elements + // Because we make it a 1Dim array, we can simply run over all elements in one go + for (int i=0 ; i + #ifndef STRING_HH #define STRING_HH - class String { public: @@ -8,9 +9,15 @@ public: String(const String& a) : _s(0) { insert(a._s) ; } ~String() { delete[] _s ; } + String& operator=( const String& a ); + String& operator+=( const String& a ); + + operator const char* const() { return data(); } + int length() const { return _len ; } const char* data() const { return _s ; } + private: char* _s ; diff --git a/ex4.3/main.cpp b/ex4.3/main.cpp new file mode 100644 index 0000000..d4a4e2a --- /dev/null +++ b/ex4.3/main.cpp @@ -0,0 +1,35 @@ +#include +#include +#include "String.hh" + +int main() { + String str("Blubaloo"); + std::cout << "String '" << str.data() << "' is " << str.length() << " chars long." << std::endl; + + + // Chain Assignment + std::cout << "String assignment" << std::endl; + String a, b; + a = b = String("Empty"); + std::cout << "String '" << a.data() << "' is " << a.length() << " chars long." << std::endl; + std::cout << "String '" << b.data() << "' is " << b.length() << " chars long." << std::endl; + + + // Operator+ + std::cout << "Operator +" << std::endl; + a = String("Loose") + String(" Statement"); + std::cout << "String '" << a.data() << "' is " << a.length() << " chars long." << std::endl; + + + // Operator+= + std::cout << "Operator +=" << std::endl; + b += " " ; // String takes a const char* as input to constructor, so autoconversion available + b += a ; + std::cout << "String '" << b.data() << "' is " << b.length() << " chars long." << std::endl; + + // Type Conversion + std::cout << "Type Conversion" << std::endl; + String c("Almost Empty"); + + std::cout << "String '" << c.data() << "' is " << strlen(c) << " chars long." << std::endl; +}