面向对象程序设计(C++)
WHUT-CS 2022 Spring
传送门:https://pan.baidu.com/s/11KwE6tQzC_H-31AFgEWtOg?pwd=1111
本次实验主要在于学习使用C++类继承机制实现程序功能。C++中的举继承机制能够用于表示类之间的“s-a"关联合理使用能够有效减少重复代码,并进而实现多态行为。
本次作业将基于类继承体系设计并实现具有数据容器功能的一系列类,通过对这些类的开发过程学习掌握C++中的类继承机制。
注意:本次练习所涉及的部分类的功能在作业练习2中已经进行了实现,但并未基于类继承机制进行设计。本次练习应充分使用继承机制对相关类的功能进行重新改进和完善。
在如上图所示的类继承体系中包含多个常见的数据容器类。这些类以Storage为其根类,通过父类与子类之间的维承关系形成了相互之间的关联。
storage: 该类表示所有数据容器类的基本功能特性,即数据元能够放入容器并从中取出。其他所有类过继承机制作为该类的直接或间接子类,这意味着所有这些类都具备数据容器的基本功能。storage类可以被声明为一个抽象类(abstrat class),在其中通过必要的纯虎函数定义了所有子类应具备的公共函数签名。
上图中所有位于叶节点处的类都应被声明为具体类 (concreate class),它们可以被用于创建对象实例。
Array:该类用于表示数组容器。在本例中Arrav类应被实现为具体类,并目作为CircwlarArr的直接父类用于展示两个具体类之间的继承关系。
上图中的其他类全部声明为抽象类,它们分别包含若干纯虚函数供子类实现。
本次作业包含三个代码文件,说明如下
编写代码时应通过注释对代码内容进行必要且详细的描述,如果你觉得样例代码框架中存在错误,也可以对代码进行改正并在注释中描述你认为的错误原因。
样例代码中仅提供了最基础的代码框架,你可以对各个类的实现提供更多你认为必要的功能。
本次作业包含如下任务:
[1] C++ Primer Plus (edition 6), Stephen Prata, ISBN: 978-0321-77640-2 Pearsor
[2] C++语言程序设计(第5版),郑莉,董渊SBN:978-7302-56691-5,清华大学出版社
Homework 3 : C++ class inheritance
Instructor: Zhiyao Liang
CIS111&EIE111 2021 Spring
传送门:https://pan.baidu.com/s/11KwE6tQzC_H-31AFgEWtOg?pwd=1111
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.
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.
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.
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:
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 .
[1] “C++ Primer Plus”, Stephen Prata, edition 6, ISBN:978-0321-77640-2, Pearson.
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.
Ideally, we can record different types of data using the same code of classes. There are at least two possible choices to do so:
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.
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.
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.
https://all-learning.com/understanding-the-concept-of-inheritance-in-c/ ↩︎