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

1/14 C++

练习:将图形类的获取周长和获取面积函数设置成虚函数,完成多态

再定义一个全局函数,能够在该函数中实现:无论传递任何图形,都可以输出传递的图形的周长和面积

#include <iostream>using namespace std;
class Shape
{
protected:double ZC;double MJ;
public:Shape(double a,double b):ZC(a),MJ(b){}~Shape(){}Shape(const Shape &other):ZC(other.ZC),MJ(other.MJ){}Shape &operator=(const Shape&other){if(this!=&other){this->ZC=other.ZC;this->MJ=other.MJ;}return *this;}virtual double get_zc(){return ZC;}virtual double get_mj(){return MJ;}void show(){cout<<"矩形周长"<<ZC<<endl;cout<<"矩形面积"<<MJ<<endl;}
};
class Jx:public Shape
{
private:double k;double g;
public:Jx(int c,int d):Shape(2*(c+d),c*d),k(c),g(d){cout<<"矩形构造"<<endl;}~Jx(){cout<<"矩形析构"<<endl;}Jx(const Jx& other):Shape(other),k(other.k),g(other.g){cout<<"矩形拷贝构造"<<endl;}Jx &operator=(const Jx&other){if(this!=&other){Shape::operator=(other);this->k=other.k;this->g=other.g;}cout<<"矩形拷贝赋值"<<endl;return *this;}void show(){cout<<"矩形::k="<<k<<"  g="<<g<<endl;cout<<"矩形周长"<<ZC<<endl;cout<<"矩形面积"<<MJ<<endl;}double get_zc(){return ZC;}double get_mj(){return MJ;}
};
class Yuan:public Shape
{
private:int r;
public:Yuan(int c):Shape(3.14*c,3.14*c*c),r(c){cout<<"圆构造"<<endl;}~Yuan(){cout<<"圆析构"<<endl;}Yuan(const Yuan& other):Shape(other),r(other.r){cout<<"圆形拷贝构造"<<endl;}Yuan &operator=(const Yuan&other){if(this!=&other){Shape::operator=(other);this->r=other.r;}cout<<"圆形拷贝赋值"<<endl;return *this;}void show(){cout<<"圆形::r="<<r<<endl;}double get_zc(){return ZC;}double get_mj(){return MJ;}};
void display(Shape &s)
{cout<<"面积:"<<s.get_mj()<<"  周长"<<s.get_zc()<<endl;
}
int main()
{Jx s1(2,8);             //构造s1.show();                         //调用的是继承下来的show函数cout<<"********************************"<<endl;Shape *ptr=&s1;ptr->show();cout<<"********************************"<<endl;display(s1);cout<<"********************************"<<endl;Yuan r1(2);             //构造r1.show();                        //调用的是继承下来的show函数cout<<"********************************"<<endl;Shape *ptr2=&r1;ptr2->show();cout<<"********************************"<<endl;display(r1);cout<<"********************************"<<endl;return 0;
}

2> 手动实现一个循环顺序队列

#include <iostream>
using namespace std;class SeqQueue
{
private:int *table;    //顺序队列指针int head;           //队头下标int tail;           //队尾下标int lenth;          //实际长度int size;           //队容量
public://无参构造SeqQueue():table(new int[100]), head(0), tail(0), lenth(0), size(100) {}//有参构造SeqQueue(int size):table(new int[size]) ,head(0), tail(0), lenth(0), size(size){}//析构函数~SeqQueue(){delete [] table;}//拷贝构造函数SeqQueue(const SeqQueue &R):table(new int[R.size]), head(R.head), tail(R.tail), lenth(R.lenth), size(R.size){//拷贝数据for(int i = 0; i < lenth; i++){this->table[(head+i)%size] = R.table[(head+i)%size];}}//拷贝赋值函数SeqQueue &operator=(const SeqQueue &R){//申请与拷贝源相同大小的内存空间delete [] this->table;this->table = new int[R.size];//拷贝数据this->head = R.head;this->size = R.size;this->tail = R.tail;this->lenth = R.lenth;for(int i = 0; i < lenth; i++){this->table[(head+i)%size] = R.table[(head+i)%size];}return *this;}//访问第一个元素int &front(){return this->table[this->head];}//访问最后一个元素int &back(){return this->table[this->tail-1];}//判空bool is_empty(){if(this->head == this->tail){return true;}else{return false;}}//判满bool is_full(){if((this->tail+1)%size == this->head){return true;}else{return false;}}//查看队列容量大小int get_size(){return this->size;}//查看队列实际长度int get_lenth(){return lenth;}//尾插一个元素void push(int e){if(is_full()){cout << "队列已满" << endl;return;}else{this->table[this->tail] = e;this->tail = (this->tail+1)%size;this->lenth++;}}//修改最后一个元素void emplace(int e){this->table[this->tail-1] = e;}//删除首个元素void pop(){if(is_empty()){cout << "队列已空" << endl;return;}else{this->head = (this->head+1)%size;this->lenth--;}}//展示void show(){int count = 0;for(int i = 0; i < lenth; i++){cout << this->table[(head+i)%size] << " ";count++;if(count % 5 == 0){cout << endl;}}if(count % 5 != 0){cout << endl;}}
};int main()
{SeqQueue A;for(int i = 0; i < 10; i++){A.push(i);}cout << "初始队列:" << endl;A.show();cout << "出队3个元素:" << endl;for(int i = 0; i < 3; i++){A.pop();}A.show();SeqQueue B = A;cout << "将A的数据拷贝给B" << endl;B.show();cout << "修改B的最后一个元素" << endl;B.emplace(100);B.show();cout << "其中" << endl;cout << "第一个元素:" << B.front() << endl;cout << "最后一个元素:" << B.back() << endl;cout << "队列容量:" << B.get_size() << endl;cout << "队列长度:" << B.get_lenth() << endl;return 0;
}

X-Mind

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

相关文章:

  • java springboot3.x jwt+spring security6.x实现用户登录认证
  • YOLOv5训练长方形图像详解
  • 【2025最新】Poe保姆级订阅指南,Poe订阅看这一篇就够了!最方便使用各类AI!
  • type1-100,2 words
  • Leetcode 377. 组合总和 Ⅳ 动态规划
  • 计算机网络(五)——传输层
  • 【SQL】进阶知识 -- 删除表的几种方法(包含表内单个字段的删除方法)
  • 【搭建JavaEE】(3)前后端交互,请求响应机制,JDBC数据库连接
  • 项目概述、开发环境搭建(day01)
  • 车联网安全--TLS握手过程详解
  • 【python】OpenCV—Extract Horizontal and Vertical Lines—Morphology
  • Redis十大数据类型详解
  • Open FPV VTX开源之betaflight配置
  • AT32 bootloader程序与上位机程序
  • 数据结构与算法之栈: LeetCode 151. 反转字符串中的单词 (Ts版)
  • 使用 configparser 读取 INI 配置文件
  • idea 自动导包,并且禁止自动导 *(java.io.*)
  • RK3588-NPU pytorch-image-models 模型编译测试
  • 低代码从“产品驱动”向“场景驱动”转型,助力数字化平台构建
  • 相加交互效应函数发布—适用于逻辑回归、cox回归、glmm模型、gee模型
  • 用gpg和sha256验证ubuntu.iso
  • 深入解析 ZooKeeper:分布式协调服务的原理与应用
  • 【Rust自学】11.10. 集成测试
  • 对当前日期进行按年、按月、按日的取值
  • 【Rust自学】12.2. 读取文件
  • C++内存泄露排查
  • Http 响应状态码 前后端联调
  • 48_Lua错误处理
  • shell脚本回顾1
  • 【3】管理无线控制器