aboutsummaryrefslogtreecommitdiff
path: root/inheritance/reuse/employee.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-04-25 10:45:32 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-04-25 10:45:32 +0200
commitab994fdada3eebc794d6b1686f55a35420e4d758 (patch)
treeccfbdd212fc3a06fea8f68b07c01f68989d2e450 /inheritance/reuse/employee.hxx
parent27245519b1a659eb849a31714df0090dc58bb87a (diff)
New example, inheritance/polymorphism
Also move the inheritance example to inheritance/reuse.
Diffstat (limited to 'inheritance/reuse/employee.hxx')
-rw-r--r--inheritance/reuse/employee.hxx143
1 files changed, 143 insertions, 0 deletions
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 <string>
+
+#include <odb/core.hxx>
+
+// 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