作业练习3:类的继承

news2025/7/11 18:25:56

作业练习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.coloradmin.cn/o/38496.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Crack:wodXMPP ActiveX 即时通讯组件

wodXMPP ActiveX 组件 XMPP组件,Jabber(ICQ MSN AIM Yahoo GTalk)即时通讯组件 wodXMPP 是 XMPP/Jabber(可扩展消息传递和状态协议)协议的客户端组件。它用于创建轻量级的消息传递客户端,并且除了 wodXMPP 之外不需要其他第 3 方要…

第八章《Java高级语法》第10节:注解

注解可以被理解为一种特殊的注释。普通注释是添加到代码中的人类语言,它可以提高程序的可读性。当源程序被编译为字节码之后,普通注释都会被去除掉,因为这些注释对代码的执行没有任何影响。因此,普通注释只能对代码的阅读者起到帮助。而注释则不同,注释可以对编译器和虚拟…

数字验证学习笔记——UVM学习1

一、类库地图 在SV模块中,验证环境整体的构建,是从底层模块的验证组件搭建到通信和激励生成这些元素无论是软件对象的创建、访问、修改、配置,还是组件之间的通信等都是通过用户自定义的方式来实现的。UVM验证方法学作为之前所有方法学的融合…

ubuntu22.04安装教程

1、选择语言 (默认) 2、取消安装更新 (默认) 3、选择键盘语言 (默认) 4、配置ip,可以直接选择dhcp,也可选择配置静态ip (默认) 5、配置代理 跳过不填写 6、设置镜像源 (默认) https://mirrors.aliyun.com/ubuntu/ 7、磁盘配置,默认即可 (默…

Java_接口使用实例

目录 给对象数组排序 按年龄来比较: 按名字来比较: 尝试自己实现一个 sort 方法 给对象数组排序 class Student {public String name;public int age;public int score;public Student(String name, int age, int score) {this.name name;this…

nginx基础篇

nginx基础篇nginx最小配置解析域名解析常用解析多租户解析Nginx虚拟主机域名配置ServerName匹配规则完整匹配通配符匹配通配符结束匹配正则匹配隧道式模型、网关、代理正向代理&反向代理网关隧道式模式反向代理反向代理一台服务器反向代理多台服务器负载均衡策略动静分离UR…

【微服务】SpringCloud断路器Hystrix

目录 一、断路器Hystrix 1、引入断路器 1.1、依赖 1.2、示例 2、传播安全上下文或使用Spring范围 3、健康指标 4、 Hystrix超时和RibbonClient 一、断路器Hystrix 较低级别的服务中的服务故障可能会导致级联故障,直至服务雪崩。在metrics.rollingStats.timeI…

五、Nacos

文章目录一、安装nacos1.压缩包下载地址2.nacos 中修改端口(8848 端口被占用需要修改)3.启动 nacos:二、nacos项目环境配置三、nacos服务分级存储模型四、NacosRule 实现负载均衡五、服务实例的权重设置六、nacos注册中心一、安装nacos 1.压缩包下载地址 https://…

LeetCode HOT 100 —— 33.搜索旋转排序数组

题目 整数数组 nums 按升序排列&#xff0c;数组中的值 互不相同 在传递给函数之前&#xff0c;nums 在预先未知的某个下标 k&#xff08;0 < k < nums.length&#xff09;上进行了 旋转&#xff0c;使数组变为 [nums[k], nums[k1], …, nums[n-1], nums[0], nums[1], ……

Hive之存储和压缩

Hive系列 第十章 存储和压缩 10.1 首先看一下Hadoop中的压缩 10.1.1 基本概念 1、概念 压缩是一种通过特定的算法来减小计算机文件大小的机制。这种机制是一种很方便的发明&#xff0c;尤其是对网络用户&#xff0c;因为它可以减小文件的字节总数&#xff0c;使文件能够通过…

Linux-yum

Linux下的开发工具即配置基本都要自己手动&#xff0c;和Windows一键式安装相比&#xff0c;Linux软件的安装要复杂很多。 centos 7下&#xff0c;基本的安装方式有三种&#xff1a; 1.源码安装——挺常用的&#xff0c;但是复杂&#xff0c;对初学者来说可以忽略。 2.rpm包安…

FFmpeg的makefile逻辑分析

在开始分析之前&#xff0c;讲一个 makefile 的调试技巧&#xff0c;推荐阅读《如何调试MAKEFILE变量》 make -f Makefile -f vars.mk HOSTPROGS这里我对 vars.mk 做了点修改&#xff0c;因为源 vars.mk 没处理特殊字符&#xff0c;直接 echo 会报错。ffmpeg 的 makefile 的变…

改进粒子滤波的无人机三维航迹预测方法附Matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;修心和技术同步精进&#xff0c;matlab项目合作可私信。 &#x1f34e;个人主页&#xff1a;Matlab科研工作室 &#x1f34a;个人信条&#xff1a;格物致知。 更多Matlab仿真内容点击&#x1f447; 智能优化算法 …

某公司常见题刷题笔记

LeetCode搞起来&#xff0c;虽说和实际业务没啥联系&#xff0c;但现在就卷起来了&#xff0c;没办法被迫卷起来。 1&#xff0c;滑动平均值&#xff0c;官网 给定窗口大小size&#xff0c;然后每次增加一个值value放入窗口&#xff0c;求此窗口内的平均值 class MovingAver…

李宏毅:Life Long Learning

Life Long Learing 也是continual Learning&#xff0c;也是incremental learning 目录 Life-Long Learning vs Transfer Learning Evaluation Research Directions Selective Synaptic Plasticity——Regulization Based Additional Neural Resourcr Allocation Memo…

Python简介-Python3及环境配置

Python简介 Python是一种跨平台的计算机程序设计语言。 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell)&#xff0c;随着版本的不断更新和语言新功能的添加&#xff0c;越多被用于独立的、大型项目的开发。 Python语…

MFC的YUV播放器实现

MFC的YUV播放器实现 文章目录MFC的YUV播放器实现一、主要参考链接二、开发踩坑记录1、Gdiplus 绘图前置条件2、播放时点击滑竿能精准跳转3、鼠标悬停在滑竿上时显示预览小窗口本文记录使用MFC编写一个YUV播放器的过程&#xff0c;尽量实现播放器都有的常用功能。功能参考与网上…

四旋翼无人机学习第9节--OpenMV以及WIFI电路、供电电路再分析

文章目录0、参考与学习1、OpenMV插座电路2、OpenMV供电电路3、ESP8266电路分析3.0 绘制分享3.1 模块正面图3.2 模块背面图3.3 模块引脚介绍3.4 模块模式选择3.5 芯片手册参考电路4、供电电路分析4.1 电池接口与电源开关部分4.2 usb供电电路与电池电压降压电路4.3 5V降3.3V电路4…

[论文精度|博士论文]非结构环境下病虫害识别方法研究

概述 提出一种基于级联卷积神经网络的植物病害识别方法提出一种融合农田多源环境信息的害虫监测方法提出一种新的目标检测损失函数解决特征冲突问题 本文研究的主要方面在于特征提取。&#xff08;其他部分沿用目前最全面的公开数据和病虫害数据以及最先进的开源算法&#xf…

30岁之后身体还能像年轻的时候一样撸代码吗?

在IT圈流传着一句话&#xff0c;程序员吃的是青春饭。很多人认为&#xff0c;30岁是个阶段&#xff0c;在这个阶段后就需要往管理方向转型。因为在30岁之后身体再也不能像年轻的时候一样熬夜撸代码&#xff0c;而且继续从事一线开发的待遇也不如管理层优厚。至于转管理层失败的…