联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2021-05-31 11:23

Homework 3 : C++ class inheritance

CIS111&EIE111 2021 Spring

1. Overview

The mechanism of C++ inheritance can represent the is-a relationships between concepts. The advantage of the inheritance mechanism include

avoiding repeated code, and useful behavior like polymorphic inheritance. The above picture shows an example of inheritance between concepts,

which is found from the web

[1]

In this homework, we will design the classes of the data storage family and show their inheritance relationships. Some of them are addressed in

homework 2 without mentioning their inheritance relationships.

The knowledge of chapters 13 and later in the textbook [1] will be helpful.

2. Designing a family of classes

2.1 The inheritance relationships

There are different data structures. Sometimes, a user does not care about the the sequential order of putting in an taking out data items, then we

can use some data structures like Bag. When a user care about sequential order of data, then Stacks and queues will be helpful. The above

picture shows a possible design of the classes, where each round-corner rectangle is a class, and an arrow points from a base class to a derived

class.

2.2 The provided code

Three C++ files are provided with this homework:

storage.h : the declarations of classes.

storage.cpp : the definitions of methods of the classes.

hmk3.cpp : the main function, which should test all the methods of the classes.

The comments and code in these files should provide detailed descriptions of the classes. A video explaining the code should also be provided

with this assignment. If you find some error of the provided code, or want to change some lines for some reason, clearly document your change as

comments in the code file, and mention your changes in the readme file.

2.3 Brief Descriptions of the classes

The classes shown on the above picture are described here.

Storage : It represent the fundamental features of a storage where data items can be put in or taken out. It is like the math concept of "Bag". All

the other classes are descendant of Storage , which means that all these classes can be used by a user to simply put int and take out data items.

Storage should a genuine abstract base class (GABC), which means it contains some pure virtual function (its prototype ends with = 0 ).

In the picture, all the classes at the leaf nodes are concrete (actual classes), which means all of their methods are defined and their objects can be

declared and created. Another concrete class is Array . Although it is possible to design an abstract class which is common ancestor of Array

and CircleArr, here wo let Array be the direct base class of CircleArr , to show that it is possible to have inheritance relationship between

concrete classes.

The other classes are GABC. They declare some methods without definitions. They work as some interface agreement.

The provided code only consider some most fundamental operations of classes, like:

- the array classes have the index operator [] .

- the stack classes have the push and pop operations.

- the queue classes have the enqueue and dequeue operations.

You can add more code to implement more operations.

Tasks of the homework

Provide the missing code (method definitions) in the file storage.cpp .

Add a method bool find(Item n) into the class Storage . It returns true if the Item n is found in the storage. Provide the missing

definitions of find in all the concrete descendant classes of Storage .

Design 3 public useful methods in some classes. Provide code for them.

Add more code in the main function in hmk3.cpp to test all the public methods of the classes.

Test the polymorphic inheritance behavior of the enqueue and dequeue operations of the queue classes.

Test the polymorphic inheritance behavior of the push and pop operations of the stack classes.

Test the polymorphic inheritance behavior of the [] operator of the array classes

Compile and run your program.

Fill the scores.xlxs file to report the results of your homework.

Submission

Deadline: June 5 11:00pm 2021

At most 3 students can form a group to do the homework together.

Clearly mention the names and classes of the group members at the top of the file scores.txt .

Only one member of the group need to submit the files

The other members can do nothing or to submit a readme.txt to confirm the names of group members

Submit the files at Moodle.

Only the source code (.cpp and .h), scores.xlxs , and a readme.txt should be submitted. If you want to add some screen shots and

images, you can replace readme.txt with readme.doc .

References

[1] "C++ Primer Plus", Stephen Prata, edition 6, ISBN:978-0321-77640-2, Pearson.

Appendix: A. Some design questions

We may face some questions that are difficult to answer when we try to express similarity between classes using the inheritance mechanism of

C++. Some of these questions are discussed below.

A.1 What is the type of a data item?

Ideally, we can record different types of data using the same code of classes. There are at least two possible choices to do so:

1. Design class templates, or use function templates, so that a data type can be represented as a variable.

2. Using C's solution void * to represent the undecided data type.

Choice of data type in this homework: In this homework, to simplify the work, let's focus on the inheritance mechanism, and do not require more

complex ways of handling general data types. Instead, we consider all data items are double . With the following statement in the program, we

can replace double with some other data type when we want to represent other types of data items.

typedef double Item;

This solution is simple, but the program requires recompilation.

A.2 Should we include a special field storage in the base class Storage ?

Conceptually, there is some storage representing all the data saved in the class, which is a common feature of all the derived classes in the family.

We could design a special member storage whose type is some template data type, or void * . There are some hard related questions: How to

declare such a member? What is its type?

To make the tasks simpler, this homework adopt the following policy:

In a base class, if the type of a data field is not decided, do not mention the data field. Only mention the methods that can access the data

field. Let a derived class add some data field with specific data type, and implement/overwrite the inherited methods to handle the data fields.

A.3 The different Node types in the single-linked list and double-linked list

We know that a node in a single linked node lacks a field prev , which is the address of the previous node in the list, comparing with a node in a

double-linked list. If we introduce How to properly deal with the similarity and difference between the two types of Nodes? One challenge is that if

two different node types are expressed, then the function prototypes will be different making the inheritance relationships between the two List

class difficult to describe.

There are possible solutions for this:

One way to deal with the different Node types is to use the template mechanism, designing some class templates where the Node type is a

type variable.

Another way is to introduce inheritance between two Node types. We can declare them as two classes with inheritance relationship. C++ also

allow inheritance between two structure ( struct ) types.

Choice of this homework:

Do not mention the Node type in the methods of the two List classes. Users do not need to know the existence of Nodes.

The two Lists are siblings derived from an abstract List class; they each have a private/protected Node type, and do not inherit the Node type

from each other.

Appendix B: Some rules of designing classes of inheritance

If some derived class (D) will define or override some inherited methods, then the method should be declared in the declaration of D.

Otherwise, compilation error.

For a class C, if one of its methods (inherited or self-declared) does not have a definition (inherited or self-defined), then C is abstract, and

cannot have on object. However. a pointer to C or reference to C is allowed.

C x; // not allowed

C* xp; // ok

C& xr; // tricky, can only appear as some member of a class of parameter type

If a method is declared in a class but does not have a definition provided by the class, the prototype of the method should have a suffix = 0 .

void function_no_def(void) = 0; // pure abstract function

Otherwise, compilation error, compiler will require a definition of a regular method

If a method declared in a Base class but will be implemented or overrode by by a derived class, it should be declared as virtual to support

polymorphic inheritance.

1. https://all-learning.com/understanding-the-concept-of-inheritance-in-c/ ?


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp