Finished Ex8.*
This commit is contained in:
parent
579d3355cc
commit
78d6fb4e3c
7 changed files with 214 additions and 0 deletions
33
ex8.2/Employee.hh
Normal file
33
ex8.2/Employee.hh
Normal file
|
@ -0,0 +1,33 @@
|
|||
#ifndef EMPLOYEE_HH
|
||||
#define EMPLOYEE_HH
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
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
|
43
ex8.2/Manager.hh
Normal file
43
ex8.2/Manager.hh
Normal file
|
@ -0,0 +1,43 @@
|
|||
#ifndef MANAGER_HH
|
||||
#define MANAGER_HH
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#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<Employee*>& listOfSubordinates() const {
|
||||
return _subordinates;
|
||||
}
|
||||
|
||||
private:
|
||||
string _name ;
|
||||
double _salary ;
|
||||
set<Employee*> _subordinates ;// subordinates is an unordered collection so set is usefull enough
|
||||
|
||||
} ;
|
||||
|
||||
#endif
|
56
ex8.2/main.cpp
Normal file
56
ex8.2/main.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
#include <iostream>
|
||||
#include "Employee.hh"
|
||||
#include "Manager.hh"
|
||||
|
||||
void populate_directory( set<Employee*>& 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<Employee*>::iterator begin, const set<Employee*>::iterator end) {
|
||||
for ( auto it = begin ; it != end ; ++it ) {
|
||||
(*it)->businessCard();
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
set<Employee*> 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;
|
||||
}
|
23
ex8.3/Circle.hh
Normal file
23
ex8.3/Circle.hh
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
57
ex8.3/main.cpp
Normal file
57
ex8.3/main.cpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
#include "Shape.hh"
|
||||
#include "Square.hh"
|
||||
#include "Circle.hh"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
void listShapes( list<Shape*> 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<Shape*> 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<Shape*> 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;
|
||||
}
|
||||
}
|
Reference in a new issue