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

news2024/7/6 19:04:06

为什么要用继承

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

类的继承与派生

在这里插入图片描述

访问控制规则

一般只使用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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1090252.html

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

相关文章

Kafka 开启SASL/SCRAM认证 及 ACL授权(三)验证

Kafka 开启SASL/SCRAM认证 及 ACL授权(三)验证。 官网地址:https://kafka.apache.org/ 本文说明如何做client验证ACL是否生效,我们之前开启了无acl信息不允许访问的配置。涉及的client有以下几个场景:shell脚本、python脚本、java应用、flink流。 kafka shell script验证…

【vr】【unity】白马VR课堂系列-VR开发核心基础05-主体设置-手柄对象的引入和设置

【视频教学】 【白马VR课堂系列-VR开发核心基础05-主体设置-手柄对象的引入和设置】 https://www.bilibili.com/video/BV19D4y1N73i/?share_source=copy_web&vd_source=7f5c96f5a58b7542fc6b467a9824b04e 【内容】 上一节引入了XR Origin并进行了初步设置,运行测试时V…

LiveMedia视频中间件视频隐私打码直播解决方案

一、方案背景 随着科技的发展&#xff0c;视频监控系统已经成为了我们生活中不可或缺的一部分。无论是在公共区域&#xff0c;还是在私人场所&#xff0c;我们都可以看到各种各样的监控设备。这些设备的出现&#xff0c;无疑提高了我们的生活安全&#xff0c;使得我们可以更好地…

微软宣布延长Azure支持Apache Cassandra 3.11时间到2024年

近日微软表示为缓解管理员不适应升级节奏&#xff0c;将Azure托管实例对Apache Cassandra 3.11 的支持延长1年&#xff0c;从而时间将持续到2024年年底。 Multiable万达宝汽车ERP(www.multiable.com.cn/solutions_qc)支持自定义栏位,实时生产排产&#xff0c;提高生产效率 此…

06 | @Entity 里面的 JPA 注解有哪些?在 Java 多态场景下如何使用?

前几课时我为你介绍了 Repository 的用法&#xff0c;其中我经常会提到“实体类”&#xff08;即我们前面的 User 类&#xff09;&#xff0c;它是对我们数据库中表的 Metadata 映射&#xff0c;那么具体如何映射呢&#xff1f;这一课时我们来讲解。 我们先看一下 Java Persis…

机器学习基础之《回归与聚类算法(2)—欠拟合与过拟合》

一、背景 1、上一篇说正规方程的时候&#xff0c;实际情况中使用很少&#xff0c;主要原因它不能解决过拟合。 2、训练集上表现的好&#xff0c;测试集上表现不好—过拟合 二、欠拟合和过拟合 1、欠拟合 训练集&#xff1a;有3个训练集&#xff0c;告诉机器都是天鹅 机器学…

使用.NET实现WOL唤醒远程开机

文章目录 1. 背景2. 关于 WOL2.1 WOL 工作原理2.2 开启网卡唤醒功能 3. 快速验证3.1 局域网 Wake on Lan 应用3.2 Ubuntu 的 etherwake 命令4. 代码实现4.1 创建.NET控制台应用程序4.2 编写代码4.3 运行应用程序 5. 最后 1. 背景 家居自动化是现代智能家居的重要组成部分&…

【JAVA】集合与背后的逻辑框架,包装类,List,Map,Set,静态内部类

❤️ Author&#xff1a; 老九 ☕️ 个人博客&#xff1a;老九的CSDN博客 &#x1f64f; 个人名言&#xff1a;不可控之事 乐观面对 &#x1f60d; 系列专栏&#xff1a; 文章目录 collectionCollection创建collection使用泛型collection方法 Map 接口Map的存储结构HashMap和Tr…

crontab报错/var/spool/cron : Permission denied和 -bash: chattr: command not found

crontab报错/var/spool/cron : Permission denied和 -bash: chattr: command not found 1、第一种情况2、第二种情况3、第三种情况 1、第一种情况 centos7下修改定时任务crontab -e的时候&#xff0c;控制台输出“crontab: installing new crontab”&#xff0c;表示任务添加成…

保障通航桥梁安全,创新边缘计算技术助力桥梁主动防撞预警系统

一、需求分析 随着公路、铁路等交通基础设施建设&#xff0c;公路桥梁数量及里程也在近20年内迅猛发展&#xff0c;2000-2020年&#xff0c;公路桥梁数量从24.06万座增加到91.28万座&#xff0c;年均增长6.89%。此外&#xff0c;长三角、珠三角等地区还有大量跨江、跨河…

机器人控制算法综述

随着机器人技术的不断进步&#xff0c;机器人控制算法也越来越复杂和精细。机器人控制算法的研究是机器人技术发展的关键之一。本文将综述机器人控制算法的研究现状&#xff0c;主要包括传统控制算法、现代控制算法、智能控制算法三个方面。 一、传统控制算法 传统控制算法是机…

Mac M1运行、连接 Docker MongoDB7。导出、恢复数据库

MongoDB&#xff0c;版本5、6.02、7.02适用 记录两种使用MongoDB的方法&#xff0c;本地安装包和docker 本地安装包方法&#xff1a;Mac M1安装MongoDB6、后台运行 Docker Mongo 一、docker运行MongoDB&#xff0c;并设置用户名密码 可以在创建容器的时候指定root用户名密码…

ChatGPT角色扮演教程,Prompt词分享

使用指南 1、可直复制使用 2、可以前往已经添加好Prompt预设的AI系统测试使用 https://ai.idcyli.comhttps://ai.idcyli.com 雅思写作考官 我希望你假定自己是雅思写作考官&#xff0c;根据雅思评判标准&#xff0c;按我给你的雅思考题和对应答案给我评分&#xff0c;并且按…

【软件测试】Requests库中处理cookie的几种方式

前言 发送请求时经常需要利用请求头中的cookie字段来做用户访问状态的保持&#xff0c;关于的cookie的处理常见有下面一些处理方式&#xff0c;这里做个小小的总结 1.直接在请求中传递cookies参数 import requestshost http://119.91.144.93:82 api r/ecshop/user.php url…

一文深入理解高并发服务器性能优化

我们现在已经搞定了 C10K并发连接问题 &#xff0c;升级一下&#xff0c;如何支持千万级的并发连接&#xff1f;你可能说&#xff0c;这不可能。你说错了&#xff0c;现在的系统可以支持千万级的并发连接&#xff0c;只不过所使用的那些激进的技术&#xff0c;并不为人所熟悉。…

Electron基础学习笔记

Electron基础学习笔记 官网&#xff1a; https://www.electronjs.org/ 文档&#xff1a;https://www.electronjs.org/zh/docs/latest/ Electon概述 Electron 是由 Github开发的开源框架它允许开发者使用Web技术构建跨平台的桌面应用 Electron Chromium Node.js Native AP…

基于IDEA 配置Maven环境和JDK版本(全局)

1.首先启动IDEA&#xff0c;进去初始界面 选择 Customize 之后&#xff0c;选择 All settings 2. 选择下图中的列表配置 3. 找到Maven下的Runner, 配置JRE的版本&#xff0c;选择自己下载使用的jdk的版本即可 4.最后配置Compiler 下的 Java Compiler 选择自己的jdk版本号&am…

将Excel表中数据导入MySQL数据库

1、准备好Excel表&#xff1a; 2、数据库建表case2 字段信息与表格对应建表&#xff1a; 3、实现代码 import pymysql import pandas as pd import openpyxl 从excel表里读取数据后&#xff0c;再存入到mysql数据库。 需要安装openpyxl pip install openpyxl# 读入数据&#x…

构建智能工厂设施的“智能电机保护和信息监控解决方案”

施耐德电气新推出了“智能电机保护与信息监控解决方案”&#xff0c;这是一个智能工厂设施建设和数字化的集成解决方案包。 进入数字化转型时代&#xff0c;行业最大的话题无疑是“智能工厂”。 智能工厂的字面意思是“真正的智能工厂”。 是指通过物联网&#xff08;IoT&…

【每日一句】只出现一次的数

文章目录 Tag题目来源题目解读解题思路方法一&#xff1a;位运算 其他语言Cpython3 写在最后 Tag 【位运算-异或和】【数组】【2023-10-14】 题目来源 136. 只出现一次的数字 题目解读 给你一个数组&#xff0c;找出数组中只出现一次的元素。题目保证仅有一个元素出现一次&a…