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

C++--day5

实现一个图形类(Shape),包含受保护成员属性:周长、面积,

公共成员函数:特殊成员函数书写

定义一个圆形类(Circle),继承自图形类,包含私有属性:半径

公共成员函数:特殊成员函数、以及获取周长、获取面积函数

定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度

公共成员函数:特殊成员函数、以及获取周长、获取面积函数

在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。

#include <iostream>using namespace std;
class Shape
{
protected:double round;double area;
public:Shape(){cout<<"无参构造"<<endl;}Shape(double r,double a):round(r),area(a){cout<<"有参构造"<<endl;}~Shape(){cout<<"析构函数"<<endl;}Shape(const Shape &other):round(other.round),area(other.area){cout<<"拷贝构造"<<endl;}Shape & operator=(const Shape &other){this->area=other.area;this->round=other.round;cout<<"拷贝赋值"<<endl;return *this;}Shape & operator=(Shape &&other){this->area=other.area;this->round=other.round;cout<<"移动赋值"<<endl;return *this;}};
class Circle:public Shape
{
private:double bj;
public:Circle() {}Circle(double r):bj(r){cout<<"有参构造"<<endl;}~Circle(){cout<<"析构函数"<<endl;}Circle(const Circle &other):Shape(other.round,other.area),bj(other.bj){cout<<"拷贝构造"<<endl;}Circle & operator=(const Circle &other){this->area=other.area;this->round=other.round;this->bj=other.bj;cout<<"拷贝赋值"<<endl;return *this;}Circle & operator=(Circle &&other){this->area=other.area;this->round=other.round;this->bj=other.bj;cout<<"移动赋值"<<endl;return *this;}void zc(){this->round=this->bj*2*(3.14);cout<<"周长="<<round<<endl;}void mj(){this->area=this->bj*this->bj*(3.14);cout<<"面积="<<area<<endl;}void show(){cout<<"周长="<<round<<endl;cout<<"面积="<<area<<endl;}
};
class Rect:public Shape
{
private:double hight;double wight;
public:Rect() {}Rect(double h,double w):hight(h),wight(w){cout<<"有参构造"<<endl;}~Rect(){cout<<"析构函数"<<endl;}Rect(const Rect &other):Shape(other.round,other.area),hight(other.hight),wight(other.wight){cout<<"拷贝构造"<<endl;}Rect & operator=(const Rect &other){this->area=other.area;this->round=other.round;this->hight=other.hight;this->wight=other.wight;cout<<"拷贝赋值"<<endl;return *this;}Rect & operator=(Rect &&other){this->area=other.area;this->round=other.round;this->hight=other.hight;this->wight=other.wight;cout<<"移动赋值"<<endl;return *this;}void zc(){this->round=(this->hight+this->wight)*2;cout<<"周长="<<round<<endl;}void mj(){this->area=this->hight*this->wight;cout<<"面积="<<area<<endl;}void show(){cout<<"周长="<<round<<endl;cout<<"面积="<<area<<endl;}
};
int main()
{Circle c1(10);c1.zc();c1.mj();Circle c2(c1);c2.show();Circle c3;c3=c1;c3.show();Circle c4;c4=move(c3);c3.show();c4.show();cout<<"******************************************"<<endl;Rect r1(10,5);r1.mj();r1.zc();Rect r2=r1;r2.show();Rect r3;r3=r1;r3.show();return 0;
}

http://www.lryc.cn/news/164805.html

相关文章:

  • Django应用部署实战:从开发到生产,全程解析
  • 群晖NAS如何在内网部署HTTPS服务让浏览器信任证书
  • crAPI靶场学习记录
  • 知识图谱实战应用28-基于py2neo的ICD-11疾病分类的知识图谱的查询与问答实战应用
  • 20.Xaml GroupBox控件 ---->带标题的内容控件
  • 基于CycleGAN的山水风格画迁移
  • ​@Cacheable 注解​
  • vue3+ts项目打包后的本地访问
  • 探索程序员需要掌握的算法?
  • 性能测试 —— Jmeter定时器
  • mp4视频太大怎么压缩?几种常见压缩方法
  • 论文复制ChatGPT按钮被发表,撤回后再曝多个类似案例;Midjourney 生成大师级的人像
  • Python自动化测试 史上最全的进阶教程
  • centos pip失效
  • Java——》ThreadLocal
  • 如何做好利益相关方的期望管理?
  • 【K8S系列】深入解析k8s网络插件—Canal
  • 从单页面应用角度去解决不跳转页面,也能更改浏览器url地址
  • Linux:keepalived + ipvsadm
  • Linux基础命令(示例代码 + 解释)
  • 巨人互动|Google企业户Google“自动采纳建议”应该如何使用
  • 元宇宙全球市场规模到2030年将达9805亿美元!
  • 《向量数据库指南》——向量数据库内核面临的技术挑战及应对措施
  • API对接中需要注意的事项
  • linux 6中4T磁盘识别并分区格式化
  • WebServer 解析HTTP 响应报文
  • 开利网络参与“大湾区独角兽创投大赛”进行蚓链数字生态项目路演
  • 前端的8种跨域解决方案
  • Linux知识点 -- 网络编程套接字
  • 逆向大漠插件/用VB6.0实现后台鼠标移动和后台鼠标左键点击