Lappeenranta University of Technology

Course: 010775000 Advanced Programming

Project 1: C++ Personal department information

 

Student name: Thoralf Czichy

Student number: 10956

Student email: czichy@rcs.urz.tu-dresden.de

 

 

 

Table of Contents

1 Task description *

2 Class structure *

3 Solution description *

3.1 Inheritance structure *

3.2 Properties *

3.3 Constructors *

3.4 Other methods *

3.5 The main() program *

3.6 Helper functions *

3.6.1 Function addNewPerson() *

3.6.2 Function readLine() *

4. Program output *

 

 

1 Task description

 

There has to be engineered a program which deals with the information about the persons within a company. There are five types of persons (= employee types): temporary, employee, secretary, manager and director. Each of these person types has certain different properties, but it is possible to draw several dependencies between them (see ® 2 Class structure).

Basic idea is, that the user should have the possibility to add (= insert) a person. If he does so, he is first asked for the persons type and then depending on that the user has to enter the specific properties of the chosen person type.

Furthermore the user should have the possibility to print a list of the persons he has entered. The list should contain first the type of that person and then depending on the type a list of the properties of that person (for detailed list of the properties see ® 2 Class structure)

2 Class structure

3 Solution description

 

3.1 Inheritance structure

The final source code consists of six classes. The inheritance structure is done according to the class diagram as stated in ® 2 Class structure. Which means that there is a base class Person from which all other classes are directly or indirectly derived. Temporary as well as Employee are directly derived from the class Person using single inheritance. The class Manager is derived form the class Employee and the class Director is derived from the class Manager. Both times single inheritance is used. In the case of the class Secretary it is different. There multiple inheritance is used. The class Secretary is derived from the classes Temporary and Employee.

3.2 Properties

The properties of the classes are given to the uppermost class possible. Which means e.g., that the properties name and salary, which are used in every class, are given to the uppermost class: Person. A more detailed view of the distribution of the properties is given in ® 2 Class structure. All attributes have the access level protected, because the derived classes have to have access to them for printing the properties in a list, but no other class should have access to them (information hiding).

3.3 Constructors

The constructor of each class is used to get the specific data for that class (e.g. level for class Manager) from the user and to initialize the attributes for that particular class. Because with the initialization of a class all constructors of the base classes are also called, the required information for a particular class is asked from the user in some kind of chain.

E.g. with the initialization of the class Manager first the constructor of the class Person is called. It asks for the initial values of the properties name and salary. Second the constructor of the class Employee is called. It asks only for the property department. And finally the constructor of the class Manager is called and it asks for the property level.

Because the class Secretary is derived from the classes Temporary and Employee (multiple inheritance) and both of these are based on the class Person, the class Person would be initialized twice e.g. with two names. To avoid that the two classes Temporary and Employee are derived virtually from Secretary (e.g.: "class Temporary : virtual public Person"). So that with the instancing of a Secretary object there is only one Person object created.

3.4 Other methods

Each class contains a method printData(). This method is responsible for printing first the type of that object (e.g. temporary, employee, ) and second the information linked to that type. So e.g. the printData() method of the class Employee prints first "Employee" and then the current content of its properties: department and derived from the class Person: name and salary.

The printData() method of the class Person is declared pure virtual (keyword virtual and "= 0"). So each derived class has to overwrite this method. This is required because there is no sense in instancing a Person object (it is not possible). Furthermore the polymorphism of the class Person and its derivations can be used for an easy managing of all persons in list.

Furthermore there are two functions isEmployee() and isTemporary(). These functions return true or false, depending on whether they represent an Employee or a Temporary (e.g. isEmployee() of class Employee returns true and isTemporary() of the same class returns false). This function is declared pure virtual, so that each derived class has to overwrite this method.

3.5 The main() program

The main() program (or function) is asking the user, whether he wants to insert a new person or wants to print a list of the current persons in the list. If the user chooses to insert a new person the function addNewPerson() is called (see 3.6. Helper functions). Otherwise, if the user wants to see a list of the current persons a loop is passed. The loop goes trough the list and calls for each object (more exact: pointer to an object) the printData() method, depending on the users choice for the type of the list. Which means, that e.g., if the users chooses to see a list of all temporaries only objects, which are of this type (isTemporary() == true) are printed. Because the printData() method of the class Person is declared virtual the right function for the right object is called.

3.6 Helper functions

3.6.1 Function addNewPerson()

The function addNewPerson adds, as the name states, a new Person object. The persons entered by the user are stored in a list (from <list.h>). Therefore a new type is defined: PersonsList. Which is a list of pointers to Persons objects. A reference to that list is handled in the main() method. The addNewPerson() method gets a reference to that PersonsList as a parameter from its caller.

In this method first the user is asked what kind of person he wants to create (e.g. temporary, employee, ) and depending on the users choice a new object of that type is created. During the creation process of that object (in the constructor method) the user will be asked for the required information. After that the new object is added to the PersonsList.

3.6.2 Function readLine()

The function readLine() is a simple function for getting an input string from the user. Therefore first all characters from the input buffer are stored in an array (charBuffer), second according to the number of chars entered by the user there is memory allocated for a char pointer and third the input in the buffer is copied to the newly allocated memory. And last the function returns a reference to that pointer to its caller.

4. Program output

Following is a printout of an example dialog, where first 5 persons are entered and then a list of these persons is printed. The input of the user is printed in bold, underlined letters.

D:\temp\thoralf>personal

Personal Department Information

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 1

(1) Temporary (2) Employee (3) Secretary (4) Manager (5) Director (6) Cancel

Your Choice: 1

Name: Britney

Salary: 1000

contract time: 1.1.1999 to 31.12.1999

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 1

(1) Temporary (2) Employee (3) Secretary (4) Manager (5) Director (6) Cancel

Your Choice: 2

Name: Whitney

Salary: 2000

Department: 2

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 1

(1) Temporary (2) Employee (3) Secretary (4) Manager (5) Director (6) Cancel

Your Choice: 3

Name: Alanis

Salary: 3000

contract time: 1.1.1993 to 31.12.2001

Department: 3

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 1

(1) Temporary (2) Employee (3) Secretary (4) Manager (5) Director (6) Cancel

Your Choice: 4

Name: Shania

Salary: 4000

Department: 4

Level: 4

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 1

(1) Temporary (2) Employee (3) Secretary (4) Manager (5) Director (6) Cancel

Your Choice: 5

Name: Madonna

Salary: 5000

Department: 5

Level: 5

Company: Madonna Oy

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 2

(1) list of temporaries (2) list of employees (3) list all

your choice: 1

TEMPORARY:

Name: Britney

Contract time: 1.1.1999 to 31.12.1999

Salary: 1000

SECRETARY:

Name: Alanis

Department: 3

Salary: 3000

Contract time: 1.1.1993 to 31.12.2001

end of list

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 2

(1) list of temporaries (2) list of employees (3) list all

your choice: 2

EMPLOYEE:

Name: Whitney

Department: 2

Salary: 2000

SECRETARY:

Name: Alanis

Department: 3

Salary: 3000

Contract time: 1.1.1993 to 31.12.2001

MANAGER:

Name: Shania

Department: 4

Salary: 4000

Level: 4

DIRECTOR:

Name: Madonna

Department: 5

Salary: 5000

Level: 5

Company: Madonna Oy

end of list

(1) Insert (2) List (3) List-one by one (4) Cancel

Your choice: 4

D:\temp\thoralf>