Ex4.*, did not finish Ex4.2
This commit is contained in:
parent
7924ee2a62
commit
be584af6c3
15 changed files with 310 additions and 6 deletions
|
@ -1,4 +1,6 @@
|
|||
|
||||
#ifndef CABLE_HH
|
||||
#define CABLE_HH
|
||||
#include <iostream>
|
||||
|
||||
class Cable {
|
||||
|
@ -11,3 +13,4 @@ public:
|
|||
private:
|
||||
|
||||
} ;
|
||||
#endif
|
||||
|
|
|
@ -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 ;
|
||||
|
||||
} ;
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
7
ex4.1/main.cpp
Normal file
7
ex4.1/main.cpp
Normal file
|
@ -0,0 +1,7 @@
|
|||
#include "Telephone.hh"
|
||||
|
||||
int main(){
|
||||
Telephone t;
|
||||
|
||||
Telephone t2 = t;
|
||||
}
|
41
ex4.1/tree.txt
Normal file
41
ex4.1/tree.txt
Normal file
|
@ -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.
|
18
ex4.2/CaloCell.cc
Normal file
18
ex4.2/CaloCell.cc
Normal file
|
@ -0,0 +1,18 @@
|
|||
//CaloGrid.cc
|
||||
#include <iostream>
|
||||
#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 ] ;
|
||||
}
|
||||
|
31
ex4.2/CaloCell.hh
Normal file
31
ex4.2/CaloCell.hh
Normal file
|
@ -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
|
18
ex4.2/CaloGrid.cc
Normal file
18
ex4.2/CaloGrid.cc
Normal file
|
@ -0,0 +1,18 @@
|
|||
//CaloGrid.cc
|
||||
#include <iostream>
|
||||
#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 ] ;
|
||||
}
|
||||
|
42
ex4.2/CaloGrid.hh
Normal file
42
ex4.2/CaloGrid.hh
Normal file
|
@ -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<nx*ny ; i++ ) {
|
||||
cells[i] = other.cells[i] ;
|
||||
}
|
||||
}
|
||||
~CaloGrid() {
|
||||
delete[] cells;
|
||||
}
|
||||
|
||||
CaloCell* cell(int x, int y);
|
||||
|
||||
|
||||
private:
|
||||
int nx, ny ;
|
||||
CaloCell* cells ;
|
||||
|
||||
void init( int in_nx, int in_ny ) {
|
||||
nx = in_nx ;
|
||||
ny = in_ny ;
|
||||
|
||||
// contigious cells
|
||||
cells = new CaloCell[ nx*ny ];
|
||||
}
|
||||
} ;
|
||||
#endif
|
9
ex4.2/Calorimeter.hh
Normal file
9
ex4.2/Calorimeter.hh
Normal file
|
@ -0,0 +1,9 @@
|
|||
//Calorimeter.hh
|
||||
#ifndef CALORIMETER_HH
|
||||
#define CALORIMETER_HH
|
||||
|
||||
|
||||
class Calorimeter
|
||||
{
|
||||
}
|
||||
#endif
|
35
ex4.2/Point.hh
Normal file
35
ex4.2/Point.hh
Normal file
|
@ -0,0 +1,35 @@
|
|||
//Point.hh
|
||||
#ifndef POINT_HH
|
||||
#define POINT_HH
|
||||
|
||||
|
||||
class Point
|
||||
{
|
||||
public:
|
||||
Point() { init(0, 0, 0) ; }
|
||||
Point( double x, double y, double z ) { init( x, y, z ); }
|
||||
|
||||
int getX() const { return x ; }
|
||||
bool setX( int new_x ) { return ( x = new_x ) ; }
|
||||
|
||||
int getY() const { return y ; }
|
||||
bool setY( int new_y ) { return ( y = new_y ) ; }
|
||||
|
||||
int getZ() const { return z ; }
|
||||
bool setZ( int new_z ) { return ( z = new_z ) ; }
|
||||
|
||||
void setPoint( double in_x, double in_y, double in_z ) {
|
||||
x = in_x ;
|
||||
y = in_y ;
|
||||
z = in_z ;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
void init( double x, double y, double z ) {
|
||||
setPoint( x, y, z );
|
||||
}
|
||||
|
||||
double x, y, z = 0 ;
|
||||
};
|
||||
#endif
|
13
ex4.2/main.cpp
Normal file
13
ex4.2/main.cpp
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "CaloCell.hh"
|
||||
#include "CaloGrid.hh"
|
||||
#include "Point.hh"
|
||||
|
||||
int main() {
|
||||
CaloCell cell(0, 1) ;
|
||||
Point p ;
|
||||
|
||||
CaloGrid grid(2, 2) ;
|
||||
|
||||
|
||||
grid.cell(1, 1);
|
||||
}
|
30
ex4.3/String.cc
Normal file
30
ex4.3/String.cc
Normal file
|
@ -0,0 +1,30 @@
|
|||
//String.cc
|
||||
#include "String.hh"
|
||||
|
||||
String operator+( const String& lhs, const String& rhs ) {
|
||||
String result(lhs); // Copy lhs into res with constructor
|
||||
result += rhs ; // Use operator+= function which is a member of the class
|
||||
return result ;
|
||||
}
|
||||
|
||||
String& String::operator=( const String& a ) {
|
||||
if ( &a != this ) {
|
||||
insert(a._s) ;
|
||||
}
|
||||
|
||||
return *this ;
|
||||
}
|
||||
String& String::operator+=( const String& a ) {
|
||||
int newlen = _len + a._len ;
|
||||
char * newstr = new char[ newlen+1 ] ;
|
||||
|
||||
strcpy(newstr, _s) ;
|
||||
strcpy(newstr + _len, a._s) ;
|
||||
|
||||
if (_s) delete[] _s ;
|
||||
|
||||
_s = newstr ;
|
||||
_len = newlen ;
|
||||
|
||||
return *this ;
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#include <cstring>
|
||||
|
||||
#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 ;
|
||||
|
|
35
ex4.3/main.cpp
Normal file
35
ex4.3/main.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#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;
|
||||
}
|
Reference in a new issue