作业练习3:类的继承

面向对象程序设计(C++)
WHUT-CS 2022 Spring

源码传送门

传送门:https://pan.baidu.com/s/11KwE6tQzC_H-31AFgEWtOg?pwd=1111

I.作业目的

本次实验主要在于学习使用C++类继承机制实现程序功能。C++中的举继承机制能够用于表示类之间的“s-a”关联合理使用能够有效减少重复代码,并进而实现多态行为。
本次作业将基于类继承体系设计并实现具有数据容器功能的一系列类,通过对这些类的开发过程学习掌握C++中的类继承机制。

注意:本次练习所涉及的部分类的功能在作业练习2中已经进行了实现,但并未基于类继承机制进行设计。本次练习应充分使用继承机制对相关类的功能进行重新改进和完善。

II.作业要求

1:类继承体系

继承体系.png

在如上图所示的类继承体系中包含多个常见的数据容器类。这些类以Storage为其根类,通过父类与子类之间的维承关系形成了相互之间的关联。

  • storage: 该类表示所有数据容器类的基本功能特性,即数据元能够放入容器并从中取出。其他所有类过继承机制作为该类的直接或间接子类,这意味着所有这些类都具备数据容器的基本功能。storage类可以被声明为一个抽象类(abstrat class),在其中通过必要的纯虎函数定义了所有子类应具备的公共函数签名。

  • 上图中所有位于叶节点处的类都应被声明为具体类 (concreate class),它们可以被用于创建对象实例。

  • Array:该类用于表示数组容器。在本例中Arrav类应被实现为具体类,并目作为CircwlarArr的直接父类用于展示两个具体类之间的继承关系。

  • 上图中的其他类全部声明为抽象类,它们分别包含若干纯虚函数供子类实现。

2:代码框架

本次作业包含三个代码文件,说明如下

  • storage.h: 所有类的声明代码
  • storage.cpp:所有类的功能实现代码
  • hmk3.cpp:主函数文件,用于编写功能测试代码

编写代码时应通过注释对代码内容进行必要且详细的描述,如果你觉得样例代码框架中存在错误,也可以对代码进行改正并在注释中描述你认为的错误原因。
样例代码中仅提供了最基础的代码框架,你可以对各个类的实现提供更多你认为必要的功能。

IlI.作业内容

本次作业包含如下任务:

  • 在storage.cpp文件中补齐所有缺失的代码,提供必要的函数实现;
  • 在strorage类中增加一个函数 bool find(item n),如果容器中包含指定的数据元素时,该函数返回true
    否则返回false。在storage类的所有子类中提供该函数的实现;
  • 在hmk3.cpp中的main函数中编写代码,用于调用并测试所有容器类中的公共函数。
  • 针对queue及其子类,编写代码测试其enqueue和dequeue功能的多态特性;
  • 针对stack及其子类,编写代码测试其push和pop功能的多态特性;
  • 针对array及其子类,编写代码测试其口操作符的多态特性;
  • 编译并运行你编写的程序;

IV.作业提交

  • 所有同学请独立完成本次作业,不允许共同完成和抄袭
  • 通过武汉理工大学“理工智课”平台中的本课程页面提交作业结果
    • 课程编号:69274(面向对象程序设计B)。
  • 截止日期: 2022 Dec31 23:00

Vl.参考文献

[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 Answer

Homework 3 : C++ class inheritance
Instructor: Zhiyao Liang
CIS111&EIE111 2021 Spring

源码传送门

传送门:https://pan.baidu.com/s/11KwE6tQzC_H-31AFgEWtOg?pwd=1111

Inheritance-Example-1024x512.png

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

hmk3_2021_c++.png_bw.png

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/ ↩︎

原文地址:http://www.cnblogs.com/codewriter/p/16928244.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性