001: #include <string>
002: #include <iostream>
003: 
004: using namespace std;
005: 
006: #include "ccc_empl.h"
007: 
008: /**
009:    A department in an organization.
010: */
011: class Department
012: {
013: public:
014:    Department(string n);
015:    Department(string n, Employee e);
016:    ~Department();
017:    Department& operator=(const Department& b);
018:    Department(const Department& b);
019:    void print() const;
020: private:
021:    string name;
022:    Employee* receptionist;
023: };
024: 
025: /**
026:    Constructs a department with a given name and no receptionist.
027:    @param n the department name
028: */
029: Department::Department(string n)
030: {
031:    name = n;
032:    receptionist = NULL;
033: 
034:    cout << "Constructor: ";
035:    print();
036: }
037: 
038: /**
039:    Constructs a department with a given name and receptionist.
040:    @param n the department name
041:    @param e the receptionist
042: */
043: Department::Department(string n, Employee e)
044: {
045:    name = n;
046:    receptionist = new Employee(e.get_name(), e.get_salary());
047:    
048:    cout << "Constructor: ";
049:    print();
050: }
051: 
052: /**
053:    Deletes the Employee object that this Department
054:    object manages.
055: */
056: Department::~Department()
057: {
058:    cout << "Destructor: ";
059:    print();
060: 
061:    delete receptionist; 
062: }
063: 
064: /**
065:    Constructs a Department object as a copy of another
066:    Department object.
067:    @param b the object to copy
068: */
069: Department::Department(const Department& b)
070: {
071:    cout << "Copy constructor: ";
072:    b.print();
073: 
074:    name = b.name;
075:    if (b.receptionist == NULL)
076:       receptionist = NULL;
077:    else
078:       receptionist = new Employee(b.receptionist->get_name(),
079:          b.receptionist->get_salary());
080: }
081: 
082: /**
083:    Sets this Department object to a copy of another
084:    Department object.
085:    @param b the object to copy
086: */
087: Department& Department::operator=(const Department& b)
088: {
089:    cout << "Assignment: ";
090:    print();
091:    cout << "= ";
092:    b.print();
093:    
094:    if (this != & b)
095:    {
096:       name = b.name;
097:       delete receptionist;
098:       if (b.receptionist == NULL)
099:          receptionist = NULL;
100:       else
101:          receptionist = new Employee(b.receptionist->get_name(),
102:             b.receptionist->get_salary());
103:    }
104:    return *this;
105: }
106: 
107: /**
108:    Prints a description of this department.
109: */
110: void Department::print() const
111: {
112:    cout << "[name=" << name << ",receptionist=";
113:    if (receptionist == NULL)
114:       cout << "NULL";
115:    else
116:       cout << receptionist->get_name();
117:    cout << "]\n";
118: }
119: 
120: int main()
121: {
122:    Department shipping("Shipping");
123:    Department qc("Quality Control", 
124:       Employee("Tester, Tina", 50000));
125:    Department dept(qc); 
126:    dept = shipping;
127:    return 0;
128: }
129: 
130: 
131: