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


    为什么要用继承

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

    类的继承与派生

    在这里插入图片描述

    访问控制规则

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

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

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

    派生类构造函数

    在这里插入图片描述

    派生类析构函数

    在这里插入图片描述

    作业

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

    #include "shape.h"
    #include "circle.h"
    #include "rectangle.h"
    #include "roundrectangle.h" 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    shape.cpp

    #include "shape.h"
    #include 
    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;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    shape.h

    #ifndef SHAPE_H
    #define SHAPE_H
    #include 
    #include 
    using namespace std;
    class shape{
    	private:
    		string color;
    	public:
    		shape();
    		shape(string color);
    		~shape();
    		string getColor();
    		void setcolor(char color);
    		void display();
    };
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    circle.cpp

    #include "circle.h"
    #include "shape.h"
    #include 
    	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;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    circle.h

    #ifndef CIRCLE_H
    #define CIRCLE_H
    #include 
    #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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    rectangle.cpp

    #include "rectangle.h"
    #include "shape.h"
    #include 
    
    	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;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    rectangle.h

    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    #include 
    #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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    roundrectangle.cpp

    #include "roundrectangle.h"
    #include "rectangle.h"
    #include"shape.h"
    #include 
    	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;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    roundrectangle.h

    #ifndef ROUNDRECTANGLE_H
    #define ROUNDRECTANGLE_H
    #include 
    #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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    [MAUI 项目实战] 笔记App(二):数据库设计
    六.schema设计
    log4j2漏洞复现
    AlphaLinux配置宽带拨号上网
    区块链技术的应用场景和优势
    I/O控制方式(程序直接控制方式,中断驱动方式,DMA方式,通道控制方式)
    电商收付通,多服务商模式
    如何重置iPhone的网络设置?这里提供详细步骤
    QODBC查询Oracle中文乱码问题
    Vue3 从零开始 搭建 简单 干净 的 后台管理系统
  • 原文地址:https://blog.csdn.net/weixin_45636780/article/details/133763559