当前位置: 首页 > 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…...

竞赛选题 深度学习+python+opencv实现动物识别 - 图像识别

文章目录 0 前言1 课题背景2 实现效果3 卷积神经网络3.1卷积层3.2 池化层3.3 激活函数&#xff1a;3.4 全连接层3.5 使用tensorflow中keras模块实现卷积神经网络 4 inception_v3网络5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; *…...

Codeforces Round 903 (Div. 3)ABCDE

Codeforces Round 903 (Div. 3)ABCDE 目录 A. Dont Try to Count题目大意思路核心代码 B. Three Threadlets题目大意思路核心代码 C. Perfect Square题目大意思路核心代码 D. Divide and Equalize题目大意思路核心代码 E. Block Sequence题目大意思路核心代码 A. Don’t Try t…...

C# 与 C/C++ 的交互

什么是平台调用 (P/Invoke) P/Invoke 是可用于从托管代码访问非托管库中的结构、回调和函数的一种技术。 托管代码与非托管的区别 托管代码和非托管代码的主要区别是内存管理方式和对计算机资源的访问方式。托管代码通常运行在托管环境中&#xff0c;如 mono 或 java 虚拟机等…...

新版Android Studio搜索不到Lombok以及无法安装Lombok插件的问题

前言 在最近新版本的Android Studio中&#xff0c;使用插件时&#xff0c;在插件市场无法找到Lombox Plugin&#xff0c;具体表现如下图所示&#xff1a; 1、操作步骤&#xff1a; &#xff08;1&#xff09;打开Android Studio->Settings->Plugins&#xff0c;搜索Lom…...

BST二叉搜索树

文章目录 概述实现创建节点查找节点增加节点查找后驱值根据关键词删除找到树中所有小于key的节点的value 概述 二叉搜索树&#xff0c;它具有以下的特性&#xff0c;树节点具有一个key属性&#xff0c;不同节点之间key是不能重复的&#xff0c;对于任意一个节点&#xff0c;它…...

【Leetcode】211. 添加与搜索单词 - 数据结构设计

一、题目 1、题目描述 请你设计一个数据结构&#xff0c;支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。 实现词典类 WordDictionary &#xff1a; WordDictionary() 初始化词典对象void addWord(word) 将 word 添加到数据结构中&#xff0c;之后可以对它…...

Discuz户外旅游|旅行游记模板/Discuz!旅行社、旅游行业门户网站模板

价值328的discuz户外旅游|旅行游记模板&#xff0c;本模板需要配套【仁天际-PC模板管理】插件使用。 模板说明 1、模板页面宽度1200px&#xff0c;简洁大气&#xff0c;较适合户外旅行、骑行、游记、摩旅、旅游、活动等类型的论坛、频道网站&#xff1b; 2、所优化的页面有&…...

【重拾C语言】十一、外部数据组织——文件

目录 前言 十一、外部数据组织——文件 11.1 重新考虑户籍管理问题——文件 11.2 文件概述 11.2.1 文件分类 11.2.2 文件指针、标记及文件操作 11.3 打开、关闭文件 11.4 I/O操作 11.4.1 字符读写 11.4.2 字符串读写 11.4.3 格式化读写 11.4.4 数据块读写 11.4.5 …...

dpdk/spdk/网络协议栈/存储/网关开发/网络安全/虚拟化/ 0vS/TRex/dpvs技术专家成长体系教程

课程围绕安全&#xff0c;网络&#xff0c;存储&#xff0c;云原生4个维度去讲解核心技术点。 6个专栏组成&#xff1a;dpdk网络专栏、存储技术专栏、安全与网关开发专栏、虚拟化与云原生专栏、测试工具专栏、性能测试专栏 一、dpdk网络 dpdk基础知识 多队列网卡&#xff0…...

树莓派玩转openwrt软路由:5.OpenWrt防火墙配置及SSH连接

1、SSH配置 打开System -> Administration&#xff0c;打开SSH Access将Interface配置成unspecified。 如果选中其他的接口表示仅在给定接口上侦听&#xff0c;如果未指定&#xff0c;则在所有接口上侦听。在未指定下&#xff0c;所有的接口均可通过SSH访问认证。 2、防火…...