diff --git a/ex8.2/Employee.hh b/ex8.2/Employee.hh new file mode 100644 index 0000000..4e51444 --- /dev/null +++ b/ex8.2/Employee.hh @@ -0,0 +1,33 @@ +#ifndef EMPLOYEE_HH +#define EMPLOYEE_HH + +#include +#include +using namespace std ; + +class Employee { + +public: + // Constructor + Employee(const char* name, double salary) : _name(name), _salary(salary) {} + + // Accessors + const char* name() const { return _name.c_str() ; } + double salary() const { return _salary ; } + + // Print functions + virtual void businessCard(ostream& os = cout) const { + os << " +------------------+ " << endl + << " | ACME Corporation | " << endl + << " +------------------+ " << endl + << " " << name() << endl ; + } + +private: + + string _name ; + double _salary ; + +} ; + +#endif diff --git a/ex8.2/Manager.hh b/ex8.2/Manager.hh new file mode 100644 index 0000000..6a099dc --- /dev/null +++ b/ex8.2/Manager.hh @@ -0,0 +1,43 @@ +#ifndef MANAGER_HH +#define MANAGER_HH + +#include +#include +#include +#include "Employee.hh" + +using namespace std ; + +class Manager : public Employee { + +public: + Manager(const char* name, double salary) : Employee(name, salary) {} + + void businessCard( ostream& os = cout) const { + Employee::businessCard( os ); + os << " +----------+ " << endl + << " Subordinates " << endl + << " +----------+ " << endl; + + for ( auto iter = _subordinates.begin() ; iter != _subordinates.end(); iter++ ) { + os << " - " << (*iter)->name() << endl; + } + + os << " +----------+ " << endl; + } + + void addSubordinate ( Employee& empl ) { + _subordinates.insert( &empl ); + } + const set& listOfSubordinates() const { + return _subordinates; + } + +private: + string _name ; + double _salary ; + set _subordinates ;// subordinates is an unordered collection so set is usefull enough + +} ; + +#endif diff --git a/ex8.2/main.cpp b/ex8.2/main.cpp new file mode 100644 index 0000000..5f32235 --- /dev/null +++ b/ex8.2/main.cpp @@ -0,0 +1,56 @@ +#include +#include "Employee.hh" +#include "Manager.hh" + +void populate_directory( set& directory ) { + // Ownership of caller + Employee* wouter = new Employee( "Wouter" , 2); + Employee* ivo = new Employee( "Ivo", 3); + Manager* stan = new Manager("Stan", 4); + Manager* jo = new Manager("Jo", 5); + Manager* frank = new Manager("Frank", 6); + + (*stan).addSubordinate( *wouter ); + (*stan).addSubordinate( *ivo ); + + directory.insert( stan ); + directory.insert( wouter ); + + // This does not give a problem because stan is also of type Employee + (*frank).addSubordinate( *stan ); + (*frank).addSubordinate( *jo ); + + directory.insert( wouter ); + directory.insert( ivo ); + directory.insert( stan ); + directory.insert( jo ); + directory.insert( frank ); + +} + +/* + * Without the virtual keyword in front of Employee:businessCard(), + * this function only gives the Employee type of businessCard(), instead of the Manager's variant if applicable + */ +void printAllCards( const set::iterator begin, const set::iterator end) { + for ( auto it = begin ; it != end ; ++it ) { + (*it)->businessCard(); + + std::cout << std::endl; + } +} + + +int main() { + set directory; + populate_directory( directory ); + + printAllCards( directory.begin(), directory.end() ); + + // Delete the contents of the directory + for ( auto it = directory.begin() ; it != directory.end() ; ++it ) { + delete (*it); + } + + return 0; +} diff --git a/ex8.3/Circle.hh b/ex8.3/Circle.hh new file mode 100644 index 0000000..00a50b9 --- /dev/null +++ b/ex8.3/Circle.hh @@ -0,0 +1,23 @@ +#ifndef CIRCLE_HH +#define CIRCLE_HH + +#include "Shape.hh" + +#define M_PI 3.14159265358979323846 + +class Circle: public Shape { +public: + + // Constructor, destructor + Circle(int radius) : _radius(radius) {} ; + virtual ~Circle() {} ; + + // Implementation of abstract interface + virtual double surface() const { return M_PI * _radius * _radius ; } + virtual double circumference() const { return 2 * M_PI * _radius ; } + virtual const char* shapeName() const { return "Circle"; } + +private: + int _radius ; +} ; +#endif diff --git a/ex8.3/Shape.hh b/ex8.3/Shape.hh index fc8f92d..b20fddc 100644 --- a/ex8.3/Shape.hh +++ b/ex8.3/Shape.hh @@ -11,6 +11,7 @@ public: // Pure virtual interface functions virtual double surface() const = 0 ; virtual double circumference() const = 0 ; + virtual const char* shapeName() const = 0; } ; #endif diff --git a/ex8.3/Square.hh b/ex8.3/Square.hh index fda5e54..392b47a 100644 --- a/ex8.3/Square.hh +++ b/ex8.3/Square.hh @@ -13,6 +13,7 @@ public: // Implementation of abstract interface virtual double surface() const { return _size * _size ; } virtual double circumference() const { return 4 * _size ; } + virtual const char* shapeName() const { return "Square"; } private: diff --git a/ex8.3/main.cpp b/ex8.3/main.cpp new file mode 100644 index 0000000..5105ffd --- /dev/null +++ b/ex8.3/main.cpp @@ -0,0 +1,57 @@ +#include +#include + +#include "Shape.hh" +#include "Square.hh" +#include "Circle.hh" + + +using namespace std; + +void listShapes( list l ); + +int main() { + + Square square(5); + + cout << "Square" << endl; + cout << "Surface : " << square.surface() << endl; + cout << "Circumference : " << square.circumference() << endl; + + cout << endl; + Circle circle(3); + + cout << "Circle" << endl; + cout << "Surface : " << circle.surface() << endl; + cout << "Circumference : " << circle.circumference() << endl; + + // Shape List + list l; + + // Create Circles + for ( int i = 0 ; i < 4 ; i++ ) { + l.push_back( new Circle(i*i) ); + } + + // Create Squares + for ( int i = 0 ; i < 5 ; i++ ) { + l.push_back( new Square(i*i) ); + } + + // List all Shapes + listShapes( l ); + + // Delete Shapes + for ( auto it = l.begin() ; it != l.end() ; ++it ) { + delete (*it); + } +} + +void listShapes( list l ) { + for ( auto it = l.begin() ; it != l.end() ; ++it ) { + cout << (*it)->shapeName() << endl; + cout << "Surface : " << (*it)->surface() << endl; + cout << "Circumference : " << (*it)->circumference() << endl; + cout << endl; + } +}