极简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)类的继承
为什么要用继承 子类不必复制父类的任何属性,已经继承下来了;易于维护与编写; 类的继承与派生 访问控制规则 一般只使用Public! 构造函数的继承与析构函数的继承 构造函数不被继承! 在创建子类对象的时候&…...

DOSBox和MASM汇编开发环境搭建
DOSBox和MASM汇编开发环境搭建 1 安装DOSBox2 安装MASM3 编译测试代码4 运行测试代码5 调试测试代码 本文属于《 X86指令基础系列教程》之一,欢迎查看其它文章。 1 安装DOSBox 下载DOSBox和MASM: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后,弹出下图4个终端(注意终端的名字)2. 启动hiveserver2服务 单独开一个窗口启动hiveserver2服务,…...

Xshell7和Xftp7超详细下载教程(包括安装及连接服务器附安装包)
1.下载 1.官网地址: XSHELL - NetSarang Website 选择学校免费版下载 2.将XSHELL和XFTP全都下载下来 2.安装 安装过程就是选择默认选项,然后无脑下一步 3.连接服务器 1.打开Xshell7,然后新建会话 2.填写相关信息 出现Connection establi…...
ASP.net数据从Controller传递到视图
最常见的方式是使用模型或 ViewBag。 使用模型传递数据: 在控制器中,创建一个模型对象,并将数据赋值给模型的属性。然后将模型传递给 View 方法。 public class HomeController : Controller {public IActionResult Index(){// 创建模型对…...
c++ 友元函数 友元类
1. 友元函数 1.1 简介 友元函数是在类的声明中声明的非成员函数,它被授予访问类的私有成员的权限。这意味着友元函数可以访问类的私有成员变量和私有成员函数,即使它们不是类的成员。 一个类中,可以将其他类或者函数声明为该类的友元&#…...
Spring推断构造器源码分析
Spring中bean虽然可以通过多种方式(Supplier接口、FactoryMethod、构造器)创建bean的实例对象,但是使用最多的还是通过构造器创建对象实例,也是我们最熟悉的创建对象的方式。如果有多个构造器时,那Spring是如何推断使用…...

十五、【历史记录画笔工具组】
文章目录 历史记录画笔工具历史记录艺术画笔工具 历史记录画笔工具 历史记录画笔工具很简单,就是将画笔工具嗯,涂抹过的修改过的地方,然后用历史记录画笔工具重新修改回来,比如我们将三叠美元中的一叠用画笔工具先涂抹掉…...
Spark上使用pandas API快速入门
文章最前: 我是Octopus,这个名字来源于我的中文名--章鱼;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github ;这博客是记录我学习的点点滴滴,如果您对 Python、Java、AI、算法有兴趣,可以关注我的…...
【WebRTC---源码篇】(十:零)WEBRTC/StreamStatisticianImpl持续更新中)
StreamStatisticianImpl是WebRTC的一个内部实现类,用于统计和管理媒体流的各种统计信息。 StreamStatisticianImpl负责记录和计算以下统计数据: 1. 带宽统计:记录媒体流的发送和接收带宽信息,包括发送比特率、接收比特率、发送丢…...
调用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 实际运行会报错: attempt to call a nil v…...

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

Kafka进阶
Kafka进阶 Kafka事务 kafka的事务机制是指kafka支持跨多个主题和分区的原子性写入,即在一个事务中发送的所有消息要么全部成功,要么全部失败。 kafka的事务机制涉及到以下几个方面: 事务生产者(transactional producer&#x…...
大数计算:e^1000/300!
1.问题:大数计算可能超出数据类型范围 当单独计算 ,因为 ,double的最大取值为1.79769e308,所以 肯定超过了double的表示范围。 同样,对于300!也是如此。 那我们应该怎么去计算和存储结果呢?…...

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

聚观早报 | “百度世界2023”即将举办;2024款岚图梦想家上市
【聚观365】10月13日消息 “百度世界2023”即将举办 2024款岚图梦想家上市 腾势D9用户超10万 华为发布新一代GigaGreen Radio OpenAI拟进行重大更新 “百度世界2023”即将举办 “百度世界2023”将于10月17日在北京首钢园举办。届时,百度创始人、董事长兼首席执…...
Windows 应用程序监控重启
执行思路 1.定时关闭可执行程序,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…...

springboot 百货中心供应链管理系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,百货中心供应链管理系统被用户普遍使用,为方…...
FFmpeg 低延迟同屏方案
引言 在实时互动需求激增的当下,无论是在线教育中的师生同屏演示、远程办公的屏幕共享协作,还是游戏直播的画面实时传输,低延迟同屏已成为保障用户体验的核心指标。FFmpeg 作为一款功能强大的多媒体框架,凭借其灵活的编解码、数据…...
uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖
在前面的练习中,每个页面需要使用ref,onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入,需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...
Leetcode 3577. Count the Number of Computer Unlocking Permutations
Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…...

家政维修平台实战20:权限设计
目录 1 获取工人信息2 搭建工人入口3 权限判断总结 目前我们已经搭建好了基础的用户体系,主要是分成几个表,用户表我们是记录用户的基础信息,包括手机、昵称、头像。而工人和员工各有各的表。那么就有一个问题,不同的角色…...

el-switch文字内置
el-switch文字内置 效果 vue <div style"color:#ffffff;font-size:14px;float:left;margin-bottom:5px;margin-right:5px;">自动加载</div> <el-switch v-model"value" active-color"#3E99FB" inactive-color"#DCDFE6"…...

(二)原型模式
原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...

SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现
摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序,以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务,提供稳定高效的数据处理与业务逻辑支持;利用 uniapp 实现跨平台前…...

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

优选算法第十二讲:队列 + 宽搜 优先级队列
优选算法第十二讲:队列 宽搜 && 优先级队列 1.N叉树的层序遍历2.二叉树的锯齿型层序遍历3.二叉树最大宽度4.在每个树行中找最大值5.优先级队列 -- 最后一块石头的重量6.数据流中的第K大元素7.前K个高频单词8.数据流的中位数 1.N叉树的层序遍历 2.二叉树的锯…...