From c79e9e4af7b380a1e357fb5e149ab4aa62328077 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Wed, 1 Jul 2020 16:29:22 +0200 Subject: [PATCH] Ex4.2 feedback: setup Calorimeter class --- ex4.2/CaloGrid.hh | 7 +++++++ ex4.2/Calorimeter.hh | 31 ++++++++++++++++++++++++++++++- ex4.2/Point.hh | 21 +++++++++++---------- ex4.2/main.cpp | 10 ++++++++++ 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/ex4.2/CaloGrid.hh b/ex4.2/CaloGrid.hh index f9dd737..28e1caa 100644 --- a/ex4.2/CaloGrid.hh +++ b/ex4.2/CaloGrid.hh @@ -40,6 +40,13 @@ class CaloGrid // contigious cells cells = new CaloCell[ nx*ny ]; + + // set Cell Ids + for ( int i = 0; i < nx ; i++ ) { + for ( int j = 0; j < ny ; j++ ) { + cells[i*ny +j].setId( i*ny+j ); + } + } } } ; #endif diff --git a/ex4.2/Calorimeter.hh b/ex4.2/Calorimeter.hh index e2d79bd..15bba96 100644 --- a/ex4.2/Calorimeter.hh +++ b/ex4.2/Calorimeter.hh @@ -5,5 +5,34 @@ class Calorimeter { -} + public: + Calorimeter( int nx, int ny, double x = 0, double y = 0, double z = 0) : + calogrid(nx, ny), + point(x, y, z) + {} + + Calorimeter( const Calorimeter& other ) : + calogrid( other.calogrid ), + point( other.point ) + {} + + CaloGrid& grid() { + return calogrid; + } + const CaloGrid& grid() const { + return calogrid; + } + + Point& position() { + return point; + } + const Point& position() const { + return point; + } + + private: + Point point; + CaloGrid calogrid; + +}; #endif diff --git a/ex4.2/Point.hh b/ex4.2/Point.hh index 6463376..421de86 100644 --- a/ex4.2/Point.hh +++ b/ex4.2/Point.hh @@ -6,28 +6,29 @@ class Point { public: - Point() { init(0, 0, 0) ; } - Point( double x, double y, double z ) { init( x, y, z ); } + Point( double x = 0, double y = 0, double z = 0) { init( x, y, z ) ; } + + // No need for Constructor or Destructor int getX() const { return x ; } - bool setX( int new_x ) { return ( x = new_x ) ; } + void setX( double new_x ) { x = new_x ; } int getY() const { return y ; } - bool setY( int new_y ) { return ( y = new_y ) ; } + void setY( double new_y ) { y = new_y ; } int getZ() const { return z ; } - bool setZ( int new_z ) { return ( z = new_z ) ; } + void setZ( double new_z ) { z = new_z ; } - void setPoint( double in_x, double in_y, double in_z ) { - x = in_x ; - y = in_y ; - z = in_z ; + void setPoint( double x, double y, double z ) { + setX(x) ; + setY(y) ; + setZ(z) ; } private: void init( double x, double y, double z ) { - setPoint( x, y, z ); + setPoint( x, y, z ) ; } double x, y, z = 0 ; diff --git a/ex4.2/main.cpp b/ex4.2/main.cpp index e617311..a4c0faf 100644 --- a/ex4.2/main.cpp +++ b/ex4.2/main.cpp @@ -1,6 +1,8 @@ +#include "iostream" #include "CaloCell.hh" #include "CaloGrid.hh" #include "Point.hh" +#include "Calorimeter.hh" int main() { CaloCell cell(0, 1) ; @@ -10,4 +12,12 @@ int main() { grid.cell(1, 1); + + int nx = 2; + int ny = 2; + + Calorimeter calo(nx, ny) ; + + std::cout << calo.grid().cell(0,0)->getId() << std::endl; + std::cout << calo.grid().cell(nx-1,ny-1)->getId() << std::endl; }