Reusing Code in C++: inheriting and template

本文探讨了C++中代码复用的主要方式——继承和模板。通过对象和类创建程序是面向对象编程的核心,而继承是创建不同对象的一个子问题。公共、保护和私有继承提供了对基类成员的不同访问级别,模板则为泛型编程提供了手段,允许根据不同的类型生成代码。理解继承的本质和正确使用继承与模板,可以实现更高效且灵活的代码设计。
Reusing Code in C++: inheriting and template
The worldview of Object-oriented means a lot, including taking an object-oriented perspective to observe things and solve problems. OOP means that creating programs by using objects and creating objects. For an OOP program, the first thing a programmer thinks is what the objects the program would use to solve the problem and build the system, but not solving problem itself. Using and creating different objects are daily common through reusing provided code. Objects are the elements of the program.

Creating new objects by instancing new classes which are the inheritances of old classes is a sub-problem of creating different objects.

All efforts of reusing code in C++ serves classes-creating or type-creating which serves objects-creating. The program uses objects to build systems and solve the problem.




What is inheritance? Why is this facility designed like this? How to understand the Is-a relationship and has-a relationship?

In real and artificial world, the features of more complicated and concrete things or objects are sets of simple and abstract features! So, using integrated sets of abstract concepts and features to describe a special object is a natural developed thought. This thought or process of thinking can be utilized to model concepts from more general to more special!

Formal language with its self-contained is symbolized and information-condensed, formally, also describes the abstract concepts and relationship among them, for example, as mathematics does. Using formal language to model system and represent problem is amazing and powerful. Computer language is one of formal language. Using the thought(the process of thinking ) of utilizing abstract concepts to compose special objects is great.

As a computer language, C++ provides more facilities than C to reuse the code, and that makes you could concentrate on your project without concerning on any changes in a provided and reused code. You have many choices to use the same class code by public, protected and private inheritance. Every inheritance strategy is a concise and different consideration about the relationship between the provided code and the new class. The art of OOP is the consideration about the whole picture, which is described by your program and the provided nice designed libraries.

The implementation of inheritance is simple and easy to understand. When you need other debugged written code, you can modify and add new features to it, but you may break the old pure code with introducing new bug in it. So, you have to invent new machinery to deal with this problem. The answer is working with the compiler through inheritance mechanism.
Understanding the Whole picture and essence of inheritance
The mechanism of inheritance, including single or multiple public, protected and private inheritance, makes the features of base class, namely the protected and public members, to be public, protected or private. In terms of the accessing right of members in derived class, program can get more features from the public inheritance mechanism than protected and private. That means protected and private hide more features of base classes. However, in the derived class inherited in public, protected and private, the public methods can still access the methods of base class. Only in public inheritance, can program get all the features of the classes of derived and base. In OOP, all classes’ methods are useful, so hiding more features is not good at one hand. Program hopes to visit all the methods from the outside of the world, at the other. So, it is a balance that making inheritance properly, at one hand, fulfills the hiding command, at other hand, meets the design of entire program.

For the coupled data and methods called subobject, other methods can only access these data by their coupled methods in any style of inheritance!

Subobjects are real components of the program.

Understanding and Using public inheritance
For the program, no matter which the class is, what program can access or see is the public members of the derived class and all of the public members of inherited classes by this class. The program can get all the public by “->” and “.”. The newest class of every public inheritance level can access the inherited classes’ data members only with the inherited classes’ public methods.

When a new class inherits many features from classes publicly, this class has interfaces of the base classes. When a class which inherited many features from other classes publicly and when an object of derived class is created in memory, lots of objects of base classes would be created also by calling a chains of constructors! The fact that an object of class is created equals to the creation of many base objects! In short, an object of newest class are a set of nameless objects. Program can access these public methods in these derived classes’ objects. The newest class is a kind of base class, this relationship respects the restriction added by base class to newest class. Conflict between the general and individual leads the polymorphism.


Understanding and Using private inheritance


Understanding and Using Multiple (public, private or protected) Inheritance
Base class virtual or abstract is a kind of special class, many classes could inherit its features, also it can be a subject of any class, more classes may have a shared base class! Mechanism of Multiple inheritance makes a class may inherit more than one class at a time! This fact can bring the new class more features which come from different classes or the same classes. In short, new classes hope to inherit pure different features, which maybe have same name, from different base classes and MI brings many problems to be deal with.

How to make sure a class inherit pure features after multiple inheritance and a chain of inheritance is the issue. What can be done is that making the base class virtual.

Is-a and has-a is very different!
If the derived class, publicly, can still maintain the methods of base class, that means the outside world can access the methods of base class through an object of derived class directly! That is is-a relationship.

Private, protected inheritance and containment make the derived class has an implement of base class. That means the outside world can not access the methods of base class directly via an object of derived class.

But, the derived class would, with inheritance implicitly, have nameless subobjects-whose features are exposed to outside world-of base classes, and named subobjects with containment explicitly.

1,Public Inheritance:

Real Meaning of Public Inheritance
The style, process and results of public inheriting means a lot. One of the results is that derived class is a kind of base class, with the influence to type cast!

First, the objects of derived class can be assigned to the objects of base class without explicit expression.
Second, the reference and pointer of base class can refer and point to the objects of derived class without explicit expression.
Third, the derived classes can rewrite the methods of base class to implement different behaviors.That is polymorphic inheritance.

Private, Protected, Public members with private, protected, public Inheritance
Protected members in class:
Why is protected needed?

Template: another smart invention to reuse code for building a class or function, giving C++ a new level of abstraction

Why do we need template even STL?
To answer this question, a thinking about general and special is needed. Class template and function template give generic description of a kind of class and function. Reusing the template can make many special classes and functions in terms of different types. Template is another technology or facility to reuse already packaged code in libraries, comparing with inheritance and containment.

On the another hand, ADTs, such as stack, queue,list and tree, need templates to generically generate special and concrete versions of ADT in terms of different types, and STL just makes it done.

A template is more general and abstract than class specialized by some data type. In a set of classes and templates which consist of the conceptual world paralleling with the real world, templates are more abstract than classes, classes are concrete and real “things” in conceptual world, while the templates are relatively more abstract concepts. In that conceptual paralleling world, the abstract concept and concrete concept are relative.

Additionally, templates are convenient when, generally, discussing about some general algorithms-- sorting and searching, without the special types.

The function of template is generating types(templates or classes) by given specifying parameters through identical description!
下载代码方式:https://pan.quark.cn/s/a4b39357ea24 依据所提供的资料,我们深入剖析此问题以及所给出的两种算法方案。 ### 问题背景 该问题源自王晓东编撰的《算法设计与实验题解》一书,书中阐述了一个值得注意的数学议题:针对一本页码从1到n顺序编号的书籍,要求统计所有页码中数字0至9各自出现的频次。例如,若n=13,则页码序列为1、2、...、13,其中数字1出现5次(体现在1、10、11、12、13中),数字0出现1次(体现在10中)。 ### 问题描述 具体而言,我们需要开发一种算法,其输入参数为一个正整数n,输出结果需为0至9这十个数字各自出现的频次。所有页码均以十进制形式呈现,且不包含任何前导零,即不会出现如006之类的页码表示。 ### 解决方案一:时间复杂度为O(n*log10(n))的算法 首先,介绍一种时间复杂度为O(n*log10(n))的算法实现。其核心构思在于遍历从1到n的每一个数值,然后逐一分解每个数值的各个位,并统计各类数字出现的频次。具体步骤如下: 1. 初始化一个长度为10的数组`count`,用于记录0至9每个数字出现的频次,初始值均为0。 2. 从1开始遍历至n,对于每一个数值i,将其转换为整数并进行以下操作: - 利用循环结构,持续将当前数值除以10,获取余数(即当前最低位的数字),并累加到对应的计数器中。 3. 遍历完成后,输出`count`数组中的每一个元素,即为所求的结果。 ### 解决方案二:优化算法 为了提升效率,提出了一种更为优越的算法。该算法基于以下观察:在1到10^n-1之间的任意区间内,每一种数字0至9出现的频次是相等的。例如,在1到999之间,每一种数字0至9出现的频次均相...
内容概要:本文档详细介绍了基于直驱永磁同步发电机(PMSG)的1.5MW风力发电系统在Simulink环境下的建模与仿真方法,涵盖风力机、传动系统、PMSG本体及电力电子变换器等核心组件的数学建模与系统集成。通过构建完整的风电系统仿真平台,实现了对风速扰动、机械动力学响应、电磁能量转换及并网运行特性的动态模拟,重点解析了PMSG在不同工况下的运行行为与先进控制策略的设计与实现,如最大功率点跟踪(MPPT)和矢量控制技术。该模型不仅可用于风电系统的性能评估与优化,还可作为控制器设计与算法验证的有效工具,支持新能源领域的教学、科研与工程应用。; 适合人群:具备电力系统、电机控制或可再生能源发电等相关背景的科研人员、工程技术人员及高校研究生;熟悉MATLAB/Simulink仿真环境者尤佳。; 使用场景及目标:①开展风力发电系统的动态特性分析与先进控制策略研究;②完成课程设计、学位论文或科研项目中的系统建模任务;③复现高水平学术论文中的风电仿真案例,支撑科研成果的验证与发表。; 阅读建议:建议结合文档中提到的相关控制算法与优化策略进行拓展学习,重点关注模型结构搭建、参数配置与仿真调试过程,并通过改变风速输入、负载条件等变量开展多工况仿真实验,深入理解系统动态响应机制与控制效果。
内容概要:本文系统研究了基于粒子群PSO、灰狼GWO、鲸鱼WOA、哈里斯鹰HHO、蜣螂DBO、麻雀SSA等多种智能优化算法的无人机三维路径规划方法,利用Matlab代码实现了在复杂三维环境下的路径搜索与避障功能,并构建包含路径长度、飞行高度、障碍物规避、转弯代价等多维度的综合成本函数体系,对各算法的收敛速度、寻优能力、路径平滑性及全局搜索性能进行了定量对比分析。研究不仅展示了各类群智能算法在路径规划中的实现机制与参数敏感性,还提供了可复现的仿真平台,为无人机自主导航系统的开发与优化提供了理论依据和技术支撑。; 适合人群:具备Matlab编程基础和基本优化算法知识,从事无人机路径规划、智能控制、自动化、机器人技术等相关领域的科研人员、工程技术人员及高校研究生。; 使用场景及目标:① 对比分析主流群智能优化算法在复杂三维空间路径规划中的性能差异与适用条件;② 构建并优化多目标成本函数以提升路径规划的安全性与经济性;③ 为科研项目、学术论文撰写或实际工程应用提供可靠、可复现的Matlab代码参考与仿真框架; 阅读建议:建议读者结合所提供的Matlab代码逐模块调试运行,深入理解各算法的迭代机制与路径生成过程,重点关注参数设置对优化结果的影响,并可根据具体应用场景调整环境建模与成本权重,进一步拓展和优化算法性能。
内容概要:本文围绕“风光制氢合成氨系统优化研究”展开,详细介绍了利用Python代码对该综合能源系统进行建模与优化的全过程。通过复现高水平学术论文,构建了集成风能、光伏等可再生能源的制氢及合成氨系统模型,充分考虑了可再生能源出力的随机性与波动性、关键设备运行的技术约束以及系统整体的经济性目标,采用先进的数学优化算法对系统的容量配置与运行调度策略进行联合求解,旨在提升绿氢与绿氨生产的效率,促进可再生能源的高效消纳并推动工业领域深度脱碳。文中提供了完整的Python代码实现方案,涵盖数据处理、模型构建、求解器调用与结果可视化等环节,具有较强的可复现性和二次开发价值。; 适合人群:具备一定Python编程基础和优化建模能力,从事新能源系统规划、综合能源系统优化、绿色化工、电力系统调度及相关领域的科研人员、工程技术人员和高校研究生。; 使用场景及目标:①深入学习并复现风光耦合电解水制氢与合成氨的集成系统优化模型;②掌握基于Python的能源系统建模、多目标优化与不确定性处理方法;③应用于绿色氨生产系统设计、可再生能源大规模消纳、低碳工业流程优化等前沿科研与工程项目。; 阅读建议:建议读者结合文中提供的完整代码,使用实际气象与负荷数据进行调试与验证,深入理解目标函数的构建逻辑、各类物理与运行约束的数学表达以及优化求解器(如Pyomo+CBC或Gurobi)的具体应用,进而可拓展至考虑更多不确定性因素(如价格波动)或多能互补(如储能)的复杂场景研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值