From ab994fdada3eebc794d6b1686f55a35420e4d758 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 25 Apr 2012 10:45:32 +0200 Subject: New example, inheritance/polymorphism Also move the inheritance example to inheritance/reuse. --- inheritance/reuse/employee.hxx | 143 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 inheritance/reuse/employee.hxx (limited to 'inheritance/reuse/employee.hxx') diff --git a/inheritance/reuse/employee.hxx b/inheritance/reuse/employee.hxx new file mode 100644 index 0000000..7de3989 --- /dev/null +++ b/inheritance/reuse/employee.hxx @@ -0,0 +1,143 @@ +// file : inheritance/reuse/employee.hxx +// copyright : not copyrighted - public domain + +#ifndef EMPLOYEE_HXX +#define EMPLOYEE_HXX + +#include + +#include + +// Abstract person class. Note that it does not declare the object id. +// +#pragma db object abstract +class person +{ +public: + person (const std::string& first, const std::string& last) + : first_ (first), last_ (last) + { + } + + const std::string& + first () const + { + return first_; + } + + const std::string& + last () const + { + return last_; + } + +protected: + friend class odb::access; + person () {} + +private: + std::string first_; + std::string last_; +}; + +// Abstract employee class. It derives from the person class and declares +// the object id for all the concrete employee types. +// +#pragma db object abstract +class employee: public person +{ +public: + employee (const std::string& first, const std::string& last) + : person (first, last) + { + } + + unsigned long + number () const + { + return id_; + } + +protected: + friend class odb::access; + employee () {} + +private: + #pragma db id auto + unsigned long id_; +}; + +// Concrete permanent_employee class. Note that it doesn't define any +// data members of its own. +// +#pragma db object +class permanent_employee: public employee +{ +public: + permanent_employee (const std::string& first, const std::string& last) + : employee (first, last) + { + } + +private: + friend class odb::access; + permanent_employee () {} +}; + +// Concrete temporary_employee class. It adds the employment duration in +// months. +// +#pragma db object +class temporary_employee: public employee +{ +public: + temporary_employee (const std::string& first, + const std::string& last, + unsigned long duration) + : employee (first, last), duration_ (duration) + { + } + + unsigned long + duration () const + { + return duration_; + } + +private: + friend class odb::access; + temporary_employee () {} + + unsigned long duration_; +}; + +// Concrete contractor class. It derives from the person class (and not +// employee; an independent contractor is not considered an employee). +// We use the contractor's external email address as the object id. +// +#pragma db object +class contractor: public person +{ +public: + contractor (const std::string& first, + const std::string& last, + const std::string& email) + : person (first, last), email_ (email) + { + } + + const std::string& + email () const + { + return email_; + } + +private: + friend class odb::access; + contractor () {} + + #pragma db id + std::string email_; +}; + +#endif // EMPLOYEE_HXX -- cgit v1.1