当前位置: 首页 > news >正文

极简c++(7)类的继承

为什么要用继承

在这里插入图片描述
子类不必复制父类的任何属性,已经继承下来了;易于维护与编写;

类的继承与派生

在这里插入图片描述

访问控制规则

一般只使用Public!
在这里插入图片描述

构造函数的继承与析构函数的继承

构造函数不被继承!
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在创建子类对象的时候,会先调用父类的构造函数,再调用子类的构造函数
在消亡子类对象的时候,会先调用子类的析构函数,再调用父类的析构函数

派生类构造函数

在这里插入图片描述

派生类析构函数

在这里插入图片描述

作业

在这里插入图片描述
在这里插入图片描述
main.cpp

#include "shape.h"
#include "circle.h"
#include "rectangle.h"
#include "roundrectangle.h" 
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(void) {shape myshape1("red");myshape1.display();circle mycircle1;mycircle1.display();circle mycircle2("red",2);mycircle2.display();Rectangle myrectangle1("green",2,3);myrectangle1.display();RoundRectangle myroundrectangle1("yeelow",2,3,1);myroundrectangle1.display();return 0;
}

shape.cpp

#include "shape.h"
#include <iostream>
using namespace std;		shape::shape():color("white"){cout<<"无参创建shape"<<endl;}shape::shape(string color){this->color = color;cout<<"有参创建shape"<<endl;}shape::~shape(){cout<<"消亡shape"<<endl;}string shape::getColor(){return this->color;}void shape::setcolor(char color){this->color = color;}void shape::display(){cout<<"color:"<<getColor()<<endl;}

shape.h

#ifndef SHAPE_H
#define SHAPE_H
#include <string>
#include <iostream>
using namespace std;
class shape{private:string color;public:shape();shape(string color);~shape();string getColor();void setcolor(char color);void display();
};#endif

circle.cpp

#include "circle.h"
#include "shape.h"
#include <iostream>const double pi = 3.14;circle::circle(){radius = 1;cout<<"无参创建circle"<<endl;}circle::circle(string color,double radius):shape(color){this->radius = radius;cout<<"有参创建circle"<<endl;}circle::~circle(){cout<<"消亡circle"<<endl;}double circle::getRadius(){return this->radius;}void circle::setradius(double radius){this->radius = radius;	}double circle::getArea(){return pi*radius*radius;}void circle::display(){shape::display();cout<<"R="<<getRadius()<<","<<"Area="<<getArea()<<endl;}

circle.h

#ifndef CIRCLE_H
#define CIRCLE_H
#include <string>
#include "shape.h"class circle:public shape{private:double radius;public:circle();circle(string color,double radius);~circle();double getRadius();void setradius(double radius);double getArea();void display();
};#endif

rectangle.cpp

#include "rectangle.h"
#include "shape.h"
#include <iostream>Rectangle::Rectangle(){width = 1;height = 1;cout<<"无参创建Rectangle"<<endl;}Rectangle::Rectangle(string color,double width,double height):shape(color){this->width = width;this->height = height;cout<<"有参创建Rectangle"<<endl; }Rectangle::~Rectangle(){cout<<"消亡Rectangle"<<endl;}double Rectangle::getWidth(){return width; }double Rectangle::getHeight(){return height;}void Rectangle::setWidth(double width){this->width = width;}void Rectangle::setHeight(double height){this->height = height;}double Rectangle::getArea(){return width*height;}void Rectangle::display(){shape::display();cout<<"width="<<getWidth()<<","<<"Height="<<getHeight()<<","<<"Area="<<getArea()<<endl;}

rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <string>
#include "shape.h"class Rectangle:public shape{private:double width;double height;public:Rectangle();Rectangle(string color,double width,double height);~Rectangle();double getWidth();double getHeight();void setWidth(double width);void setHeight(double height);double getArea();void display();
};#endif

roundrectangle.cpp

#include "roundrectangle.h"
#include "rectangle.h"
#include"shape.h"
#include <iostream>RoundRectangle::RoundRectangle(){this->roundRadius = 1;cout<<"无参创建RoundRectangle"<<endl; }RoundRectangle::RoundRectangle(string color,double width,double height,double roundRadius):Rectangle(color,width,height){this->roundRadius = roundRadius;cout<<"有参创建RoundRectangle"<<endl;}RoundRectangle::~RoundRectangle(){cout<<"消亡RoundRectangle"<<endl; }double RoundRectangle::getRoundradius(){return roundRadius;}void RoundRectangle::setRoundradius(double roundRadius){this->roundRadius = roundRadius;}double RoundRectangle::getArea(){return Rectangle::getWidth()*Rectangle::getHeight()+(3.14*roundRadius*roundRadius)/2;}void RoundRectangle::display(){shape::display();cout<<"width="<<Rectangle::getWidth()<<","<<"Height="<<Rectangle::getHeight()<<","<<"Area="<<getArea()<<endl;}

roundrectangle.h

#ifndef ROUNDRECTANGLE_H
#define ROUNDRECTANGLE_H
#include <string>
#include "rectangle.h"class RoundRectangle:public Rectangle{private:double roundRadius;public:RoundRectangle();RoundRectangle(string color,double width,double height,double roundRadius);~RoundRectangle();double getRoundradius();void setRoundradius(double roundRadius);double getArea();void display();
};#endif

相关文章:

极简c++(7)类的继承

为什么要用继承 子类不必复制父类的任何属性&#xff0c;已经继承下来了&#xff1b;易于维护与编写&#xff1b; 类的继承与派生 访问控制规则 一般只使用Public&#xff01; 构造函数的继承与析构函数的继承 构造函数不被继承&#xff01; 在创建子类对象的时候&…...

DOSBox和MASM汇编开发环境搭建

DOSBox和MASM汇编开发环境搭建 1 安装DOSBox2 安装MASM3 编译测试代码4 运行测试代码5 调试测试代码 本文属于《 X86指令基础系列教程》之一&#xff0c;欢迎查看其它文章。 1 安装DOSBox 下载DOSBox和MASM&#xff1a;https://download.csdn.net/download/u011832525/884180…...

047:mapboxGL本地上传shp文件,在map上解析显示图形

第047个 点击查看专栏目录 本示例的目的是介绍演示如何在vue+mapbox中本地上传shp文件,利用shapefile读取shp数据,并在地图上显示图形。 直接复制下面的 vue+mapbox源代码,操作2分钟即可运行实现效果 文章目录 示例效果配置方式示例源代码(共117行)加载shapefile.js方式…...

Windows下DataGrip连接Hive

DataGrip连接Hive 1. 启动Hadoop2. 启动hiveserver2服务3. 启动元数据服务4. 启动DG 1. 启动Hadoop 在控制台中输入start-all.cmd后&#xff0c;弹出下图4个终端&#xff08;注意终端的名字&#xff09;2. 启动hiveserver2服务 单独开一个窗口启动hiveserver2服务&#xff0c;…...

Xshell7和Xftp7超详细下载教程(包括安装及连接服务器附安装包)

1.下载 1.官网地址&#xff1a; XSHELL - NetSarang Website 选择学校免费版下载 2.将XSHELL和XFTP全都下载下来 2.安装 安装过程就是选择默认选项&#xff0c;然后无脑下一步 3.连接服务器 1.打开Xshell7&#xff0c;然后新建会话 2.填写相关信息 出现Connection establi…...

ASP.net数据从Controller传递到视图

最常见的方式是使用模型或 ViewBag。 使用模型传递数据&#xff1a; 在控制器中&#xff0c;创建一个模型对象&#xff0c;并将数据赋值给模型的属性。然后将模型传递给 View 方法。 public class HomeController : Controller {public IActionResult Index(){// 创建模型对…...

c++ 友元函数 友元类

1. 友元函数 1.1 简介 友元函数是在类的声明中声明的非成员函数&#xff0c;它被授予访问类的私有成员的权限。这意味着友元函数可以访问类的私有成员变量和私有成员函数&#xff0c;即使它们不是类的成员。 一个类中&#xff0c;可以将其他类或者函数声明为该类的友元&#…...

Spring推断构造器源码分析

Spring中bean虽然可以通过多种方式&#xff08;Supplier接口、FactoryMethod、构造器&#xff09;创建bean的实例对象&#xff0c;但是使用最多的还是通过构造器创建对象实例&#xff0c;也是我们最熟悉的创建对象的方式。如果有多个构造器时&#xff0c;那Spring是如何推断使用…...

十五、【历史记录画笔工具组】

文章目录 历史记录画笔工具历史记录艺术画笔工具 历史记录画笔工具 历史记录画笔工具很简单&#xff0c;就是将画笔工具嗯&#xff0c;涂抹过的修改过的地方&#xff0c;然后用历史记录画笔工具重新修改回来&#xff0c;比如我们将三叠美元中的一叠用画笔工具先涂抹掉&#xf…...

Spark上使用pandas API快速入门

文章最前&#xff1a; 我是Octopus&#xff0c;这个名字来源于我的中文名--章鱼&#xff1b;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github &#xff1b;这博客是记录我学习的点点滴滴&#xff0c;如果您对 Python、Java、AI、算法有兴趣&#xff0c;可以关注我的…...

【WebRTC---源码篇】(十:零)WEBRTC/StreamStatisticianImpl持续更新中)

StreamStatisticianImpl是WebRTC的一个内部实现类&#xff0c;用于统计和管理媒体流的各种统计信息。 StreamStatisticianImpl负责记录和计算以下统计数据&#xff1a; 1. 带宽统计&#xff1a;记录媒体流的发送和接收带宽信息&#xff0c;包括发送比特率、接收比特率、发送丢…...

​调用Lua脚本tostring(xxx)报attempt to call a nil value (global ‘tostring‘

在c程序里调用Lua脚本, 脚本中用到了转字符串 tostring(xxx) str "test" function output(a,b,c)d "a:"..tostring(a).."b:"..tostring(b).."c"..tostring(c)return d end 实际运行会报错&#xff1a; attempt to call a nil v…...

PBA.客户需求分析 需求管理

一、客户需求分析 1 需求的三个层次: Requirement/Wants/Pains 大部分人认为&#xff0c;产品满足不了客户需要&#xff0c;是因为客户告知的需求是错误的&#xff0c;这听起来有一些道理&#xff0c;却没有任何意义。不同角色对于需求的理解是不一样的。在客户的需求和厂家的…...

Kafka进阶

Kafka进阶 Kafka事务 kafka的事务机制是指kafka支持跨多个主题和分区的原子性写入&#xff0c;即在一个事务中发送的所有消息要么全部成功&#xff0c;要么全部失败。 kafka的事务机制涉及到以下几个方面&#xff1a; 事务生产者&#xff08;transactional producer&#x…...

大数计算:e^1000/300!

1.问题&#xff1a;大数计算可能超出数据类型范围 当单独计算 &#xff0c;因为 &#xff0c;double的最大取值为1.79769e308&#xff0c;所以 肯定超过了double的表示范围。 同样&#xff0c;对于300&#xff01;也是如此。 那我们应该怎么去计算和存储结果呢&#xff1f;…...

力扣164最大间距

1.前言 因为昨天写了一个基数排序&#xff0c;今天我来写一道用基数排序实现的题解&#xff0c;希望可以帮助你理解基数排序。 这个题本身不难&#xff0c;就是线性时间和线性额外空间(O(n))的算法&#xff0c;有点难实现 基数排序的时间复杂度是O(d*(nradix))&#xff0c;其中…...

聚观早报 | “百度世界2023”即将举办;2024款岚图梦想家上市

【聚观365】10月13日消息 “百度世界2023”即将举办 2024款岚图梦想家上市 腾势D9用户超10万 华为发布新一代GigaGreen Radio OpenAI拟进行重大更新 “百度世界2023”即将举办 “百度世界2023”将于10月17日在北京首钢园举办。届时&#xff0c;百度创始人、董事长兼首席执…...

Windows 应用程序监控重启

执行思路 1.定时关闭可执行程序&#xff0c;2.再通过定时监控启动可执行程序 定时启动关闭程序.bat echo off cd "D:\xxxx\" :: 可执行程序目录 Start "" /b xxxx.exe :: 可执行程序 timeout /T 600 /nobreak >nul :: 600秒 taskkill /IM xxxx.exe /…...

springboot 通过url下载文件并上传到OSS

DEMO流程 传入一个需要下载并上传的url地址下载文件上传文件并返回OSS的url地址 springboot pom文件依赖 <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w…...

docker创建elasticsearch、elasticsearch-head部署及简单操作

elasticsearch部署 1 拉取elasticsearch镜像 docker pull elasticsearch:7.7.0 2 创建文件映射路径 mkdir /mydata/elasticsearch/data mkdir /mydata/elasticsearch/plugins mkdir /mydata/elasticsearch/config 3 文件夹授权 chmod 777 /mydata/elastic…...

【Oracle APEX开发小技巧12】

有如下需求&#xff1a; 有一个问题反馈页面&#xff0c;要实现在apex页面展示能直观看到反馈时间超过7天未处理的数据&#xff0c;方便管理员及时处理反馈。 我的方法&#xff1a;直接将逻辑写在SQL中&#xff0c;这样可以直接在页面展示 完整代码&#xff1a; SELECTSF.FE…...

多场景 OkHttpClient 管理器 - Android 网络通信解决方案

下面是一个完整的 Android 实现&#xff0c;展示如何创建和管理多个 OkHttpClient 实例&#xff0c;分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

java调用dll出现unsatisfiedLinkError以及JNA和JNI的区别

UnsatisfiedLinkError 在对接硬件设备中&#xff0c;我们会遇到使用 java 调用 dll文件 的情况&#xff0c;此时大概率出现UnsatisfiedLinkError链接错误&#xff0c;原因可能有如下几种 类名错误包名错误方法名参数错误使用 JNI 协议调用&#xff0c;结果 dll 未实现 JNI 协…...

HBuilderX安装(uni-app和小程序开发)

下载HBuilderX 访问官方网站&#xff1a;https://www.dcloud.io/hbuilderx.html 根据您的操作系统选择合适版本&#xff1a; Windows版&#xff08;推荐下载标准版&#xff09; Windows系统安装步骤 运行安装程序&#xff1a; 双击下载的.exe安装文件 如果出现安全提示&…...

NLP学习路线图(二十三):长短期记忆网络(LSTM)

在自然语言处理(NLP)领域,我们时刻面临着处理序列数据的核心挑战。无论是理解句子的结构、分析文本的情感,还是实现语言的翻译,都需要模型能够捕捉词语之间依时序产生的复杂依赖关系。传统的神经网络结构在处理这种序列依赖时显得力不从心,而循环神经网络(RNN) 曾被视为…...

力扣-35.搜索插入位置

题目描述 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

听写流程自动化实践,轻量级教育辅助

随着智能教育工具的发展&#xff0c;越来越多的传统学习方式正在被数字化、自动化所优化。听写作为语文、英语等学科中重要的基础训练形式&#xff0c;也迎来了更高效的解决方案。 这是一款轻量但功能强大的听写辅助工具。它是基于本地词库与可选在线语音引擎构建&#xff0c;…...

深度学习之模型压缩三驾马车:模型剪枝、模型量化、知识蒸馏

一、引言 在深度学习中&#xff0c;我们训练出的神经网络往往非常庞大&#xff08;比如像 ResNet、YOLOv8、Vision Transformer&#xff09;&#xff0c;虽然精度很高&#xff0c;但“太重”了&#xff0c;运行起来很慢&#xff0c;占用内存大&#xff0c;不适合部署到手机、摄…...

认识CMake并使用CMake构建自己的第一个项目

1.CMake的作用和优势 跨平台支持&#xff1a;CMake支持多种操作系统和编译器&#xff0c;使用同一份构建配置可以在不同的环境中使用 简化配置&#xff1a;通过CMakeLists.txt文件&#xff0c;用户可以定义项目结构、依赖项、编译选项等&#xff0c;无需手动编写复杂的构建脚本…...

DiscuzX3.5发帖json api

参考文章&#xff1a;PHP实现独立Discuz站外发帖(直连操作数据库)_discuz 发帖api-CSDN博客 简单改造了一下&#xff0c;适配我自己的需求 有一个站点存在多个采集站&#xff0c;我想通过主站拿标题&#xff0c;采集站拿内容 使用到的sql如下 CREATE TABLE pre_forum_post_…...