辅导CO3105、辅导C++设计、讲解C/C++、辅导Handin system

- 首页 >> C/C++编程

Assignment 2

Advanced C++

CO3105 / CO4105 / CO7105 / CO4203

Dr. Rayna Dimitrova and Dr. Yi Hong

Released: 19.11.2018, Due: 10.12.2018, 23:59

This assignment is 30% of the coursework for the module.

You need to submit this assignment using the Departmental Handin

system using the link provided on the module webpage.

Marking criteria: You are expected to submit code that constitutes

a correct implementation of the required classes and class

member functions. You are also expected to provide comments

sufficiently explaining the logic of your code.

10% of the mark will be given for submitting well-commented code

and for following good C++ coding practices.

90% of the mark is based on the correctness of your implementation,

to be assessed by testing and manual inspection.

1

Anonymous marking will be performed by automatically

downloading and saving the solutions in an anonymized form.

The assignment has two parts. Part 1 accounts for 50% of the

overall mark, and Part 2 accounts for 50% of the overall mark.

Resubmission: After the submission deadline and marking,

you will be given the full set of tests used for the evaluation, and

you will be allowed to submit a solution again. If your resubmitted

code passes all the test you will pass the assignment, but in such

case, your mark for this assignment will be capped by 50%.

2

Provided files:

For Part 1:

– Person.h contains the incomplete definition of the classes

Person, Customer, Employee, Manager and Consultant

and the detailed descriptions of their methods.

– Person.cpp is the file in which you have to provide the

implementations of the member functions of the classes

defined in Person.h.

Remove code referring to removeMe variables, such as:

unsigned int removeMe=0;

return removeMe;

– main.cpp contains demonstration of some example usage.

– A makefile.

For Part 2:

– Expression.h contains the incomplete definitions of the

classes Expression, BinaryExpression, UnaryExpression,

Number, Multiply, Add, Abs and Square and the detailed

descriptions of their methods.

– Expression.cpp is the file in which you have to provide

3

the implementations of the member functions of the classes

defined in Expression.h.

– main.cpp contains demonstration of example usage that

will help you test your classes.

– A makefile.

The rest of this document contains descriptions of the two parts

of the assignment.

4

1 Part: Person class hierarchy 50%

In this exercise you will implement a simple hierarchy of people using inheritance. The

hierarchy consists of the base class Person, the classes Customer and Employee, which

are derived from Person, and two other classes, Manager and Consultant, which are

derived from the class Employee. The hierarchy is shown in the figure below.

Person

Customer Employee

Manager Consultant

Each person supports two functions: toString and type. The function type should

return the name of that class. The toString function should return a string with the

information of the instance in the format illustrated in the examples below.

For a Customer with name John Smith, customer ID 42 and no orders:

[John Smith, 42]

For a Customer with name John Smith, customer ID 42 and orders with

numbers 123 and 456:

[John Smith, 42, 123, 456]

For a Manager with name Alice Jones and salary 50000.5:

[Alice Jones, earns: 50000.5]

For a Consultant Bob Lewis with salary 30000 and manager Alice Jones:

[Bob Lewis, earns: 30000, manager: Alice Jones]

For a Consultant Bob Lewis with salary 30000 and no manager:

[Bob Lewis, earns: 30000]

Customer has an additional feature to place an order. Every employee has the additional

feature to get their salary increased. Consultant has a manager, which is implemented

by a pointer to and object of type Manager. Notice that many consultants will

share the pointer to the same Manager, and that the Consultant objects do not own the

corresponding Manager object. Also, some of the classes have some extra functions.

Your task is to add the necessary member variables and function implementations.

You should add the keyword virtual where appropriate, and then implement the functions

where they are most appropriate. Person and Employee could be abstract classes,

but Customer, Manager and Consultant should be concrete classes. The skeletons of the

classes are less complete than in previous assignment, as you now have to choose where

to implement each function, where add the virtual keyword, etc.

A detailed description of the methods of the classes is given in Person.h.

5

2 Part : Expression class hierarchy 50%

In this exercise you will implement storing and evaluation of simple arithmetic expressions

using inheritance. Expressions are either multiplication, addition, absolute value, square,

or just an integer value. The hierarchy of expressions is shown in the figure below.

Expression

BinaryExpression Number UnaryExpression

Multiply Add Abs Square

Number represents expressions that are integer values, Multiply represents multiplication

of two Expressions, Add represents sum of two Expressions, Abs represents the absolute

value of an Expression, and Square represents the square of an Expression.

All expressions should support the following functions:

type : returns the name of the class,

print : returns a string representation of the entire expression, as shown below,

evaluate: returns the integer result of evaluating the expression.

The result of the function print should be as follows.

For an object of class Multiply with sub-expressions subexp1 and subexp2:

(subexp*subexp)

For an object of class Add with sub-expressions subexp1 and subexp2:

(subexp+subexp)

For an object of class Number with value n:

n

For an object of class Abs with sub-expression subexp:

Abs(subexp)

For an object of class Square with sub-expression subexp:

Square(subexp)

Above, the sub-strings (,),*,+,Abs,Square are part of the output format, and do not

depend on the contents of the expression. You should not add spaces or new lines.

As in Part 1, you should add the virtual keyword where appropriate and then implement

the functions where they are most appropriate. Expression, BinaryExpression,

6

and UnaryExpression could be abstract classes but all other classes should be concrete

classes. You should add the necessary member variables and function implementations.

The skeletons of the classes are less complete than in previous assignment, as you now

have to choose where to implement each function, where add the virtual keyword, etc.

A detailed description of each function is given in the file Expression.h.


站长地图