From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp | 75 +++++++++++++ .../Codebeispiele/2_ch10/fig10_01_05/circle.cpp | 74 +++++++++++++ .../Codebeispiele/2_ch10/fig10_01_05/circle.h | 44 ++++++++ .../Codebeispiele/2_ch10/fig10_01_05/point.cpp | 65 +++++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h | 40 +++++++ .../Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp | 31 ++++++ .../Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp | 74 +++++++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_06/circle.h | 44 ++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp | 65 +++++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_06/point.h | 40 +++++++ .../Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp | 74 +++++++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_07/circle.h | 44 ++++++++ .../Codebeispiele/2_ch10/fig10_07/fig10_07.cpp | 48 +++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp | 65 +++++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_07/point.h | 40 +++++++ .../Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp | 80 ++++++++++++++ .../Codebeispiele/2_ch10/fig10_08_12/circle.cpp | 74 +++++++++++++ .../Codebeispiele/2_ch10/fig10_08_12/circle.h | 44 ++++++++ .../Codebeispiele/2_ch10/fig10_08_12/point.cpp | 65 +++++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h | 40 +++++++ .../Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp | 120 +++++++++++++++++++++ .../Codebeispiele/2_ch10/fig10_13_20/circle.cpp | 81 ++++++++++++++ .../Codebeispiele/2_ch10/fig10_13_20/circle.h | 47 ++++++++ .../Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp | 75 +++++++++++++ .../Codebeispiele/2_ch10/fig10_13_20/cylinder.h | 46 ++++++++ .../Codebeispiele/2_ch10/fig10_13_20/point.cpp | 72 +++++++++++++ .../Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h | 45 ++++++++ .../Codebeispiele/2_ch10/fig10_13_20/shape.cpp | 36 +++++++ .../Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h | 41 +++++++ .../Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp | 65 +++++++++++ .../Codebeispiele/2_ch10/fig10_23_33/baseplus.h | 40 +++++++ .../2_ch10/fig10_23_33/commission.cpp | 76 +++++++++++++ .../Codebeispiele/2_ch10/fig10_23_33/commission.h | 44 ++++++++ .../Codebeispiele/2_ch10/fig10_23_33/employee.cpp | 86 +++++++++++++++ .../Codebeispiele/2_ch10/fig10_23_33/employee.h | 50 +++++++++ .../Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp | 100 +++++++++++++++++ .../Codebeispiele/2_ch10/fig10_23_33/hourly.cpp | 80 ++++++++++++++ .../Codebeispiele/2_ch10/fig10_23_33/hourly.h | 44 ++++++++ .../Codebeispiele/2_ch10/fig10_23_33/salaried.cpp | 61 +++++++++++ .../Codebeispiele/2_ch10/fig10_23_33/salaried.h | 40 +++++++ 40 files changed, 2375 insertions(+) create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h (limited to 'Bachelor/Prog2/Codebeispiele/2_ch10') diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp new file mode 100644 index 0000000..c04f750 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp @@ -0,0 +1,75 @@ +// Fig. 10.5: fig10_05.cpp +// Aiming base-class and derived-class pointers at base-class +// and derived-class objects, respectively. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point point( 30, 50 ); + Point *pointPtr = 0; // base-class pointer + + Circle circle( 120, 89, 2.7 ); + Circle *circlePtr = 0; // derived-class pointer + + // set floating-point numeric formatting + cout << fixed << setprecision( 2 ); + + // output objects point and circle + cout << "Print point and circle objects:" + << "\nPoint: "; + point.print(); // invokes Point's print + cout << "\nCircle: "; + circle.print(); // invokes Circle's print + + // aim base-class pointer at base-class object and print + pointPtr = &point; + cout << "\n\nCalling print with base-class pointer to " + << "\nbase-class object invokes base-class print " + << "function:\n"; + pointPtr->print(); // invokes Point's print + + // aim derived-class pointer at derived-class object + // and print + circlePtr = &circle; + cout << "\n\nCalling print with derived-class pointer to " + << "\nderived-class object invokes derived-class " + << "print function:\n"; + circlePtr->print(); // invokes Circle's print + + // aim base-class pointer at derived-class object and print + pointPtr = &circle; + cout << "\n\nCalling print with base-class pointer to " + << "derived-class object\ninvokes base-class print " + << "function on that derived-class object\n"; + pointPtr->print(); // invokes Point's print + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp new file mode 100644 index 0000000..91b708d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.4: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return 2 * getRadius(); + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "center = "; + Point::print(); // invoke Point's print function + cout << "; radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h new file mode 100644 index 0000000..42ec256 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.3: circle.h +// Circle class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h new file mode 100644 index 0000000..6a2874e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h @@ -0,0 +1,40 @@ +// Fig. 10.1: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp new file mode 100644 index 0000000..8c3c122 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp @@ -0,0 +1,31 @@ +// Fig. 10.6: fig10_06.cpp +// Aiming a derived-class pointer at a base-class object. +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point point( 30, 50 ); + Circle *circlePtr = 0; + + // aim derived-class pointer at base-class object + circlePtr = &point; // Error: a Point is not a Circle + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp new file mode 100644 index 0000000..2f3325e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.4: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return getRadius() * 2; + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "Center = "; + Point::print(); // invoke Point's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h new file mode 100644 index 0000000..fc22e5b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.3: circle.h +// Circle4 class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h new file mode 100644 index 0000000..6a2874e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h @@ -0,0 +1,40 @@ +// Fig. 10.1: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp new file mode 100644 index 0000000..582d5d9 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.4: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return getRadius() * 2; + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "Center = "; + Point::print(); // invoke Point's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h new file mode 100644 index 0000000..fc22e5b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.3: circle.h +// Circle4 class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp new file mode 100644 index 0000000..184ed32 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp @@ -0,0 +1,48 @@ +// Fig. 10.7: fig10_07.cpp +// Attempting to invoke derived-class-only member functions +// through a base-class pointer. +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point *pointPtr = 0; + Circle circle( 120, 89, 2.7 ); + + // aim base-class pointer at derived-class object + pointPtr = &circle; + + // invoke base-class member functions on derived-class + // object through base-class pointer + int x = pointPtr->getX(); + int y = pointPtr->getY(); + pointPtr->setX( 10 ); + pointPtr->setY( 10 ); + pointPtr->print(); + + // attempt to invoke derived-class-only member functions + // on derived-class object through base-class pointer + double radius = pointPtr->getRadius(); + pointPtr->setRadius( 33.33 ); + double diameter = pointPtr->getDiameter(); + double circumference = pointPtr->getCircumference(); + double area = pointPtr->getArea(); + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h new file mode 100644 index 0000000..6a2874e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h @@ -0,0 +1,40 @@ +// Fig. 10.1: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp new file mode 100644 index 0000000..be43be3 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp @@ -0,0 +1,80 @@ +// Fig. 10.10: fig10_10.cpp +// Introducing polymorphism, virtual functions and dynamic +// binding. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point point( 30, 50 ); + Point *pointPtr = 0; + + Circle circle( 120, 89, 2.7 ); + Circle *circlePtr = 0; + + // set floating-point numeric formatting + cout << fixed << setprecision( 2 ); + + // output objects point and circle using static binding + cout << "Invoking print function on point and circle " + << "\nobjects with static binding " + << "\n\nPoint: "; + point.print(); // static binding + cout << "\nCircle: "; + circle.print(); // static binding + + // output objects point and circle using dynamic binding + cout << "\n\nInvoking print function on point and circle " + << "\nobjects with dynamic binding"; + + // aim base-class pointer at base-class object and print + pointPtr = &point; + cout << "\n\nCalling virtual function print with base-class" + << "\npointer to base-class object" + << "\ninvokes base-class print function:\n"; + pointPtr->print(); + + // aim derived-class pointer at derived-class + // object and print + circlePtr = &circle; + cout << "\n\nCalling virtual function print with " + << "\nderived-class pointer to derived-class object " + << "\ninvokes derived-class print function:\n"; + circlePtr->print(); + + // aim base-class pointer at derived-class object and print + pointPtr = &circle; + cout << "\n\nCalling virtual function print with base-class" + << "\npointer to derived-class object " + << "\ninvokes derived-class print function:\n"; + pointPtr->print(); // polymorphism: invokes circle's print + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp new file mode 100644 index 0000000..550b3e2 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.11: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return getRadius() * 2; + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "Center = "; + Point::print(); // invoke Point's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h new file mode 100644 index 0000000..138a4b8 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.10: circle.h +// Circle class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + virtual void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h new file mode 100644 index 0000000..5cd7bd6 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h @@ -0,0 +1,40 @@ +// Fig. 10.8: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + virtual void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp new file mode 100644 index 0000000..2ce0553 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp @@ -0,0 +1,120 @@ +// Fig. 10.20: fig10_20.cpp +// Driver for shape, point, circle, cylinder hierarchy. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include + +using std::vector; + +#include "shape.h" // Shape class definition +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition +#include "cylinder.h" // Cylinder class definition + +void virtualViaPointer( const Shape * ); +void virtualViaReference( const Shape & ); + +int main() +{ + // set floating-point number format + cout << fixed << setprecision( 2 ); + + Point point( 7, 11 ); // create a Point + Circle circle( 22, 8, 3.5 ); // create a Circle + Cylinder cylinder( 10, 10, 3.3, 10 ); // create a Cylinder + + cout << point.getName() << ": "; // static binding + point.print(); // static binding + cout << '\n'; + + cout << circle.getName() << ": "; // static binding + circle.print(); // static binding + cout << '\n'; + + cout << cylinder.getName() << ": "; // static binding + cylinder.print(); // static binding + cout << "\n\n"; + + // create vector of three base-class pointers + vector< Shape * > shapeVector( 3 ); + + // aim shapeVector[0] at derived-class Point object + shapeVector[ 0 ] = &point; + + // aim shapeVector[1] at derived-class Circle object + shapeVector[ 1 ] = &circle; + + // aim shapeVector[2] at derived-class Cylinder object + shapeVector[ 2 ] = &cylinder; + + // loop through shapeVector and call virtualViaPointer + // to print the shape name, attributes, area and volume + // of each object using dynamic binding + cout << "\nVirtual function calls made off " + << "base-class pointers:\n\n"; + + for ( int i = 0; i < shapeVector.size(); i++ ) + virtualViaPointer( shapeVector[ i ] ); + + // loop through shapeVector and call virtualViaReference + // to print the shape name, attributes, area and volume + // of each object using dynamic binding + cout << "\nVirtual function calls made off " + << "base-class references:\n\n"; + + for ( int j = 0; j < shapeVector.size(); j++ ) + virtualViaReference( *shapeVector[ j ] ); + + return 0; + +} // end main + +// make virtual function calls off a base-class pointer +// using dynamic binding +void virtualViaPointer( const Shape *baseClassPtr ) +{ + cout << baseClassPtr->getName() << ": "; + + baseClassPtr->print(); + + cout << "\narea is " << baseClassPtr->getArea() + << "\nvolume is " << baseClassPtr->getVolume() + << "\n\n"; + +} // end function virtualViaPointer + +// make virtual function calls off a base-class reference +// using dynamic binding +void virtualViaReference( const Shape &baseClassRef ) +{ + cout << baseClassRef.getName() << ": "; + + baseClassRef.print(); + + cout << "\narea is " << baseClassRef.getArea() + << "\nvolume is " << baseClassRef.getVolume() << "\n\n"; + +} // end function virtualViaReference + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp new file mode 100644 index 0000000..b8f6f59 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp @@ -0,0 +1,81 @@ +// Fig. 10.17: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return 2 * getRadius(); + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// override virtual function getArea: return area of Circle +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// override virutual function getName: return name of Circle +string Circle::getName() const +{ + return "Circle"; + +} // end function getName + +// override virtual function print: output Circle object +void Circle::print() const +{ + cout << "center is "; + Point::print(); // invoke Point's print function + cout << "; radius is " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h new file mode 100644 index 0000000..6d6877c --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h @@ -0,0 +1,47 @@ +// Fig. 10.16: circle.h +// Circle class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + virtual double getArea() const; // return area + + // return name of shape (i.e., "Circle") + virtual string getName() const; + + virtual void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp new file mode 100644 index 0000000..7c51113 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp @@ -0,0 +1,75 @@ +// Fig. 10.19: cylinder.cpp +// Cylinder class inherits from class Circle. +#include + +using std::cout; + +#include "cylinder.h" // Cylinder class definition + +// default constructor +Cylinder::Cylinder( int xValue, int yValue, double radiusValue, + double heightValue ) + : Circle( xValue, yValue, radiusValue ) +{ + setHeight( heightValue ); + +} // end Cylinder constructor + +// set Cylinder's height +void Cylinder::setHeight( double heightValue ) +{ + height = ( heightValue < 0.0 ? 0.0 : heightValue ); + +} // end function setHeight + +// get Cylinder's height +double Cylinder::getHeight() const +{ + return height; + +} // end function getHeight + +// override virtual function getArea: return Cylinder area +double Cylinder::getArea() const +{ + return 2 * Circle::getArea() + // code reuse + getCircumference() * getHeight(); + +} // end function getArea + +// override virtual function getVolume: return Cylinder volume +double Cylinder::getVolume() const +{ + return Circle::getArea() * getHeight(); // code reuse + +} // end function getVolume + +// override virtual function getName: return name of Cylinder +string Cylinder::getName() const +{ + return "Cylinder"; + +} // end function getName + +// override virtual function print: output Cylinder object +void Cylinder::print() const +{ + Circle::print(); // code reuse + cout << "; height is " << getHeight(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h new file mode 100644 index 0000000..f20d624 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h @@ -0,0 +1,46 @@ +// Fig. 10.18: cylinder.h +// Cylinder class inherits from class Circle. +#ifndef CYLINDER_H +#define CYLINDER_H + +#include "circle.h" // Circle class definition + +class Cylinder : public Circle { + +public: + + // default constructor + Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); + + void setHeight( double ); // set Cylinder's height + double getHeight() const; // return Cylinder's height + + virtual double getArea() const; // return Cylinder's area + virtual double getVolume() const; // return Cylinder's volume + + // return name of shape (i.e., "Cylinder" ) + virtual string getName() const; + + virtual void print() const; // output Cylinder + +private: + double height; // Cylinder's height + +}; // end class Cylinder + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp new file mode 100644 index 0000000..3f63c81 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp @@ -0,0 +1,72 @@ +// Fig. 10.15: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// override pure virtual function getName: return name of Point +string Point::getName() const +{ + return "Point"; + +} // end function getName + +// override pure virtual function print: output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h new file mode 100644 index 0000000..264b37b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h @@ -0,0 +1,45 @@ +// Fig. 10.14: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +#include "shape.h" // Shape class definition + +class Point : public Shape { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + // return name of shape (i.e., "Point" ) + virtual string getName() const; + + virtual void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp new file mode 100644 index 0000000..125ea79 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp @@ -0,0 +1,36 @@ +// Fig. 10.13: shape.cpp +// Shape class member-function definitions. +#include + +using std::cout; + +#include "shape.h" // Shape class definition + +// return area of shape; 0.0 by default +double Shape::getArea() const +{ + return 0.0; + +} // end function getArea + +// return volume of shape; 0.0 by default +double Shape::getVolume() const +{ + return 0.0; + +} // end function getVolume + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h new file mode 100644 index 0000000..0c929a5 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h @@ -0,0 +1,41 @@ +// Fig. 10.12: shape.h +// Shape abstract-base-class definition. +#ifndef SHAPE_H +#define SHAPE_H + +#include // C++ standard string class + +using std::string; + +class Shape { + +public: + + // virtual function that returns shape area + virtual double getArea() const; + + // virtual function that returns shape volume + virtual double getVolume() const; + + // pure virtual functions; overridden in derived classes + virtual string getName() const = 0; // return shape name + virtual void print() const = 0; // output shape + +}; // end class Shape + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp new file mode 100644 index 0000000..2052d48 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp @@ -0,0 +1,65 @@ +// Fig. 10.32: baseplus.cpp +// BasePlusCommissionEmployee member-function definitions. +#include + +using std::cout; + +#include "baseplus.h" + +// constructor for class BasePlusCommissionEmployee +BasePlusCommissionEmployee::BasePlusCommissionEmployee( + const string &first, const string &last, + const string &socialSecurityNumber, + double baseSalaryAmount, + double grossSalesAmount, + double rate ) + : CommissionEmployee( first, last, socialSecurityNumber, + grossSalesAmount, rate ) +{ + setBaseSalary( baseSalaryAmount ); + +} // end BasePlusCommissionEmployee constructor + +// set base-salaried commission worker's wage +void BasePlusCommissionEmployee::setBaseSalary( double salary ) +{ + baseSalary = salary < 0.0 ? 0.0 : salary; + +} // end function setBaseSalary + +// return base-salaried commission worker's base salary +double BasePlusCommissionEmployee::getBaseSalary() const +{ + return baseSalary; + +} // end function getBaseSalary + +// return base-salaried commission worker's earnings +double BasePlusCommissionEmployee::earnings() const +{ + return getBaseSalary() + CommissionEmployee::earnings(); + +} // end function earnings + +// print base-salaried commission worker's name +void BasePlusCommissionEmployee::print() const +{ + cout << "\nbase-salaried commission worker: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h new file mode 100644 index 0000000..d049914 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h @@ -0,0 +1,40 @@ +// Fig. 10.31: baseplus.h +// BasePlusCommissionEmployee class derived from Employee. +#ifndef BASEPLUS_H +#define BASEPLUS_H + +#include "commission.h" // Employee class definition + +class BasePlusCommissionEmployee : public CommissionEmployee { + +public: + BasePlusCommissionEmployee( const string &, const string &, + const string &, double = 0.0, double = 0.0, double = 0.0 ); + + void setBaseSalary( double ); + double getBaseSalary() const; + + virtual double earnings() const; + virtual void print() const; + +private: + double baseSalary; // base salary per week + +}; // end class BasePlusCommissionEmployee + +#endif // BASEPLUS_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp new file mode 100644 index 0000000..2d05932 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp @@ -0,0 +1,76 @@ +// Fig. 10.30: commission.cpp +// CommissionEmployee class member-function definitions. +#include + +using std::cout; + +#include "commission.h" // Commission class + +// CommissionEmployee constructor +CommissionEmployee::CommissionEmployee( const string &first, + const string &last, const string &socialSecurityNumber, + double grossWeeklySales, double percent ) + : Employee( first, last, socialSecurityNumber ) +{ + setGrossSales( grossWeeklySales ); + setCommissionRate( percent ); + +} // end CommissionEmployee constructor + +// return commission worker's rate +double CommissionEmployee::getCommissionRate() const +{ + return commissionRate; + +} // end function getCommissionRate + +// return commission worker's gross sales amount +double CommissionEmployee::getGrossSales() const +{ + return grossSales; + +} // end function getGrossSales + +// set commission worker's weekly base salary +void CommissionEmployee::setGrossSales( double sales ) +{ + grossSales = sales < 0.0 ? 0.0 : sales; + +} // end function setGrossSales + +// set commission worker's commission +void CommissionEmployee::setCommissionRate( double rate ) +{ + commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; + +} // end function setCommissionRate + +// calculate commission worker's earnings +double CommissionEmployee::earnings() const +{ + return getCommissionRate() * getGrossSales(); + +} // end function earnings + +// print commission worker's name +void CommissionEmployee::print() const +{ + cout << "\ncommission employee: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h new file mode 100644 index 0000000..423bd03 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h @@ -0,0 +1,44 @@ +// Fig. 10.29: commission.h +// CommissionEmployee class derived from Employee. +#ifndef COMMISSION_H +#define COMMISSION_H + +#include "employee.h" // Employee class definition + +class CommissionEmployee : public Employee { + +public: + CommissionEmployee( const string &, const string &, + const string &, double = 0.0, double = 0.0 ); + + void setCommissionRate( double ); + double getCommissionRate() const; + + void setGrossSales( double ); + double getGrossSales() const; + + virtual double earnings() const; + virtual void print() const; + +private: + double grossSales; // gross weekly sales + double commissionRate; // commission percentage + +}; // end class CommissionEmployee + +#endif // COMMISSION_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp new file mode 100644 index 0000000..151aa58 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp @@ -0,0 +1,86 @@ +// Fig. 10.24: employee.cpp +// Abstract-base-class Employee member-function definitions. +// Note: No definitions are given for pure virtual functions. +#include + +using std::cout; +using std::endl; + +#include "employee.h" // Employee class definition + +// constructor +Employee::Employee( const string &first, const string &last, + const string &SSN ) + : firstName( first ), + lastName( last ), + socialSecurityNumber( SSN ) +{ + // empty body + +} // end Employee constructor + +// return first name +string Employee::getFirstName() const +{ + return firstName; + +} // end function getFirstName + +// return last name +string Employee::getLastName() const +{ + return lastName; + +} // end function getLastName + +// return social security number +string Employee::getSocialSecurityNumber() const +{ + return socialSecurityNumber; + +} // end function getSocialSecurityNumber + +// set first name +void Employee::setFirstName( const string &first ) +{ + firstName = first; + +} // end function setFirstName + +// set last name +void Employee::setLastName( const string &last ) +{ + lastName = last; + +} // end function setLastName + +// set social security number +void Employee::setSocialSecurityNumber( const string &number ) +{ + socialSecurityNumber = number; // should validate + +} // end function setSocialSecurityNumber + +// print Employee's information +void Employee::print() const +{ + cout << getFirstName() << ' ' << getLastName() + << "\nsocial security number: " + << getSocialSecurityNumber() << endl; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h new file mode 100644 index 0000000..50ab131 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h @@ -0,0 +1,50 @@ +// Fig. 10.23: employee.h +// Employee abstract base class. +#ifndef EMPLOYEE_H +#define EMPLOYEE_H + +#include // C++ standard string class + +using std::string; + +class Employee { + +public: + Employee( const string &, const string &, const string & ); + + void setFirstName( const string & ); + string getFirstName() const; + + void setLastName( const string & ); + string getLastName() const; + + void setSocialSecurityNumber( const string & ); + string getSocialSecurityNumber() const; + + // pure virtual function makes Employee abstract base class + virtual double earnings() const = 0; // pure virtual + virtual void print() const; // virtual + +private: + string firstName; + string lastName; + string socialSecurityNumber; + +}; // end class Employee + +#endif // EMPLOYEE_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp new file mode 100644 index 0000000..c8e9761 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp @@ -0,0 +1,100 @@ +// Fig. 10.33: fig10_33.cpp +// Driver for Employee hierarchy. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include + +using std::vector; + +#include + +#include "employee.h" // Employee base class +#include "salaried.h" // SalariedEmployee class +#include "commission.h" // CommissionEmployee class +#include "baseplus.h" // BasePlusCommissionEmployee class +#include "hourly.h" // HourlyEmployee class + +int main() +{ + // set floating-point output formatting + cout << fixed << setprecision( 2 ); + + // create vector employees + vector < Employee * > employees( 4 ); + + // initialize vector with Employees + employees[ 0 ] = new SalariedEmployee( "John", "Smith", + "111-11-1111", 800.00 ); + employees[ 1 ] = new CommissionEmployee( "Sue", "Jones", + "222-22-2222", 10000, .06 ); + employees[ 2 ] = new BasePlusCommissionEmployee( "Bob", + "Lewis", "333-33-3333", 300, 5000, .04 ); + employees[ 3 ] = new HourlyEmployee( "Karen", "Price", + "444-44-4444", 16.75, 40 ); + + // generically process each element in vector employees + for ( int i = 0; i < employees.size(); i++ ) { + + // output employee information + employees[ i ]->print(); + + // downcast pointer + BasePlusCommissionEmployee *commissionPtr = + dynamic_cast < BasePlusCommissionEmployee * > + ( employees[ i ] ); + + // determine whether element points to base-salaried + // commission worker + if ( commissionPtr != 0 ) { + cout << "old base salary: $" + << commissionPtr->getBaseSalary() << endl; + commissionPtr->setBaseSalary( + 1.10 * commissionPtr->getBaseSalary() ); + cout << "new base salary with 10% increase is: $" + << commissionPtr->getBaseSalary() << endl; + + } // end if + + cout << "earned $" << employees[ i ]->earnings() << endl; + + } // end for + + // release memory held by vector employees + for ( int j = 0; j < employees.size(); j++ ) { + + // output class name + cout << "\ndeleting object of " + << typeid( *employees[ j ] ).name(); + + delete employees[ j ]; + + } // end for + + cout << endl; + + return 0; + +} // end main + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp new file mode 100644 index 0000000..1a517e1 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp @@ -0,0 +1,80 @@ +// Fig. 10.28: hourly.cpp +// HourlyEmployee class member-function definitions. +#include + +using std::cout; + +#include "hourly.h" + +// constructor for class HourlyEmployee +HourlyEmployee::HourlyEmployee( const string &first, + const string &last, const string &socialSecurityNumber, + double hourlyWage, double hoursWorked ) + : Employee( first, last, socialSecurityNumber ) +{ + setWage( hourlyWage ); + setHours( hoursWorked ); + +} // end HourlyEmployee constructor + +// set hourly worker's wage +void HourlyEmployee::setWage( double wageAmount ) +{ + wage = wageAmount < 0.0 ? 0.0 : wageAmount; + +} // end function setWage + +// set hourly worker's hours worked +void HourlyEmployee::setHours( double hoursWorked ) +{ + hours = ( hoursWorked >= 0.0 && hoursWorked <= 168.0 ) ? + hoursWorked : 0.0; + +} // end function setHours + +// return hours worked +double HourlyEmployee::getHours() const +{ + return hours; + +} // end function getHours + +// return wage +double HourlyEmployee::getWage() const +{ + return wage; + +} // end function getWage + +// get hourly worker's pay +double HourlyEmployee::earnings() const +{ + if ( hours <= 40 ) // no overtime + return wage * hours; + else // overtime is paid at wage * 1.5 + return 40 * wage + ( hours - 40 ) * wage * 1.5; + +} // end function earnings + +// print hourly worker's information +void HourlyEmployee::print() const +{ + cout << "\nhourly employee: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h new file mode 100644 index 0000000..a74d4ce --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h @@ -0,0 +1,44 @@ +// Fig. 10.27: hourly.h +// HourlyEmployee class definition. +#ifndef HOURLY_H +#define HOURLY_H + +#include "employee.h" // Employee class definition + +class HourlyEmployee : public Employee { + +public: + HourlyEmployee( const string &, const string &, + const string &, double = 0.0, double = 0.0); + + void setWage( double ); + double getWage() const; + + void setHours( double ); + double getHours() const; + + virtual double earnings() const; + virtual void print() const; + +private: + double wage; // wage per hour + double hours; // hours worked for week + +}; // end class HourlyEmployee + +#endif // HOURLY_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp new file mode 100644 index 0000000..3b9ed98 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp @@ -0,0 +1,61 @@ +// Fig. 10.26: salaried.cpp +// SalariedEmployee class member-function definitions. +#include + +using std::cout; + +#include "salaried.h" // SalariedEmployee class definition + +// SalariedEmployee constructor +SalariedEmployee::SalariedEmployee( const string &first, + const string &last, const string &socialSecurityNumber, + double salary ) + : Employee( first, last, socialSecurityNumber ) +{ + setWeeklySalary( salary ); + +} // end SalariedEmployee constructor + +// set salaried worker's salary +void SalariedEmployee::setWeeklySalary( double salary ) +{ + weeklySalary = salary < 0.0 ? 0.0 : salary; + +} // end function setWeeklySalary + +// calculate salaried worker's pay +double SalariedEmployee::earnings() const +{ + return getWeeklySalary(); + +} // end function earnings + +// return salaried worker's salary +double SalariedEmployee::getWeeklySalary() const +{ + return weeklySalary; + +} // end function getWeeklySalary + +// print salaried worker's name +void SalariedEmployee::print() const +{ + cout << "\nsalaried employee: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h new file mode 100644 index 0000000..80b2595 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h @@ -0,0 +1,40 @@ +// Fig. 10.25: salaried.h +// SalariedEmployee class derived from Employee. +#ifndef SALARIED_H +#define SALARIED_H + +#include "employee.h" // Employee class definition + +class SalariedEmployee : public Employee { + +public: + SalariedEmployee( const string &, const string &, + const string &, double = 0.0 ); + + void setWeeklySalary( double ); + double getWeeklySalary() const; + + virtual double earnings() const; + virtual void print() const; // "salaried employee: " + +private: + double weeklySalary; + +}; // end class SalariedEmployee + +#endif // SALARIED_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file -- cgit v1.2.3