c++ 手写queue循环队列

ops/2025/1/16 23:59:18/

继承与多态

继承

父子出现同名的成员问题

#include <iostream>using namespace std;
//父子类中出现重名成员
//定义一个父类
class Father{
public:string name;
protected:int pwd;
private:int money;
public:Father(){cout<<"Father::构造"<<endl;}Father(string n,int p,int m):name(n),pwd(p),money(m){cout<<"Father::有参"<<endl;}};
//定义子类
class Son:public Father{
public:int name;//可以重名
};
class Test{
public:Father f;
};int main()
{Father father;Son son;Test test;//使用子类成员和父类成员的同名函数cout<<"&son="<<&son.name<<endl;//子类namecout<<"&father="<<&son.Father::name<<endl;///访问父类namecout<<"&test="<<&test<<endl;cout<<"&test.f="<<&test.f<<endl;return 0;
}

关于继承中特殊成员函数

#include <iostream>using namespace std;
//父子类中出现重名成员
//定义一个父类
class Father{
public:string name;
protected:int pwd;
private:int money;
public:Father(){cout<<"Father::构造"<<endl;}Father(string n,int p,int m):name(n),pwd(p),money(m){cout<<"Father::有参"<<endl;}~Father(){cout<<"析构"<<endl;}//拷贝构造Father(const Father & other):name(other.name),pwd(other.pwd),money(other.money){cout<<"Father::拷贝构造"<<endl;}//拷贝赋值Father&operator=(const Father&other){if(this!=&other){this->name=other.name;this->pwd=other.pwd;this->money=other.money;}cout<<"Father::拷贝赋值函数"<<endl;return *this;}void show(){cout<<"Father::name = "<<name<<endl;cout<<"Father::pwd = "<<pwd<<endl;cout<<"Father::money = "<<money<<endl;}};
//定义子类
class Son:public Father{private:string toy;
public:Son(){cout<<"Son::无参构造"<<endl;}~Son(){cout<<"Son::析构函数"<<endl;}Son(const Son& other):Father(other),toy(other.toy){cout<<"Son::拷贝构造函数"<<endl;}Son(string n, int p, int m, string t): Father(n, p, m), toy(t){cout<<"Son::有参构造"<<endl;}void show(){//        cout<<"Father::name = "<<name<<endl;//        cout<<"Father::pwd = "<<pwd<<endl;//        cout<<"Father::money = "<<money<<endl;         //子类中无法访问父类的私有成员Father::show();                     //调用父类提供的show函数cout<<"toy = "<<toy<<endl;}};int main()
{Son s1("xx",123,5,"a");s1.show();Son s2(s1);s2.show();return 0;
}

练习:将图形类的获取周长和获取面积函数设置成虚函数,完成多态 再定义一个全局函数,能够在该函数中实现:无论传递任何图形,都可以输出传递的图形的周长和面积

#include <iostream>using namespace std;class Shape{double perimeter;double area;
public://构造Shape(){cout<<"Shape::无参"<<endl;}//拷贝构造Shape(const Shape & other):perimeter(other.perimeter),area(other.area){cout<<"拷贝构造函数"<<endl;}//拷贝赋值Shape & operator =(const Shape & other){if(this==&other){return *this;}perimeter=other.perimeter;area=other.area;return *this;}~Shape(){cout<<"shape::析构"<<endl;}virtual void show(){cout<<"perimeter"<<perimeter<<endl;cout<<"area"<<area<<endl;}double getPer(){return perimeter;}double getArea(){return area;}void setPer(double p){this->perimeter=p;}void setArea(double a){this->area=a;}};class Rectangle:public Shape
{
private:int wide;int high;
public:Rectangle(){}Rectangle(int w,int h):wide(w),high(h){}~Rectangle(){}Rectangle(const Rectangle &other):Shape(other),wide(other.wide),high(other.high){}Rectangle & operator= (const Rectangle &other){if(this != &other){Shape::operator=(other);this->wide = other.wide;this->high = other.high;}return *this;}//重写父类虚函数void show(){cout<<"矩形 长:"<<wide<<" 宽:"<<high<<endl;cout<<"perimeter"<<get_perimeter()<<endl;cout<<"area"<<get_area()<<endl;}int get_perimeter(){this->setPer( 2.0*(wide+high));return Shape::getPer();}int get_area(){this->setArea( 1.0*wide*high);return Shape::getArea();}
};//定义一个圆类
class Circle:public Shape
{
private:int radius;
public:Circle(){}Circle(int r):radius(r){}~Circle(){}Circle(const Circle &other):Shape(other),radius(other.radius){}Circle & operator= (const Circle &other){if(this != &other){Shape::operator=(other);this->radius = other.radius;}return *this;}//重写父类虚函数void show(){cout<<"圆形 半径:"<<radius<<endl;cout<<"perimeter"<<get_perimeter()<<endl;cout<<"area"<<get_area()<<endl;}int get_perimeter(){this->setPer( 2*3.14*radius);return Shape::getPer();}int get_area(){this->setArea( 3.14*radius*radius);return Shape::getArea();}};
void display( Shape & shape){shape.show();
}
int main()
{Rectangle rec(10,20);Circle cir(10);display(rec);display(cir);return 0;
}

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

#include<iostream>
using namespace std;
class myqueue{
int front;//头
int rear;//尾部
int maxSize;//最大容量
int *data;//存储元素public:
myqueue():front(0),rear(0),maxSize(0),data(NULL){}
//有参构造最大k
myqueue(int k):front(0),rear(0),maxSize(k+1),data(new int[k+1]){
cout<<"有参构造"<<endl;
}
//拷贝构造
myqueue(const myqueue & other):front(other.front),rear(other.rear),maxSize(other.maxSize),data(new int[other.maxSize]){memcpy(data,other.data,sizeof(int)*maxSize);
}//拷贝赋值
myqueue & operator = (const myqueue &other){if(this==&other) return *this;front=other.front;rear=other.rear;maxSize=other.maxSize;int *temp=new int[maxSize];delete [] data;data=temp;return *this;
}int myfront(){return data[front];
}
int back(){if(empty()){return -1;}return data[(rear-1+maxSize)%maxSize];
}
bool empty(){return rear==front;
}
bool full(){return (rear+1)%maxSize==front;
}
int size(){return (front-rear+maxSize)%maxSize;
}
//入队
bool push(int val){
if(full()) return false;
data[rear]=val;
rear=(rear+1)%maxSize;
return true;
}
//出队
int pop(){if(empty()) return -1;int res=data[front];front=(front+1)%maxSize;return res;
}
void show(){for(int i=front;i!=rear;i=(i+1)%maxSize){cout<<data[i]<<" ";}cout<<endl;
}
};int main(){myqueue a(5);a.push(1);a.push(2);a.push(3);a.push(4);a.push(5);a.pop();a.push(6);a.show();
}

http://www.ppmy.cn/ops/150667.html

相关文章

从玩具到工业控制--51单片机的跨界传奇【2】

咱们在上一篇博客里面讲解了什么是单片机《单片机入门》&#xff0c;让大家对单片机有了初步的了解。我们今天继续讲解一些有关单片机的知识&#xff0c;顺便也讲解一下我们单片机用到的C语言知识。如果你对C语言还不太了解的话&#xff0c;可以看看博主的C语言专栏哟&#xff…

如何通过 Zero Trust 模型防止内外部威胁?

随着网络攻击方式的不断演化&#xff0c;传统的安全防护措施逐渐暴露出明显的不足。无论是企业内部员工的操作失误&#xff0c;还是外部黑客的精心策划&#xff0c;传统的“边界防御”模式已不再能够有效地应对日益复杂的网络威胁。为了应对这些挑战&#xff0c;Zero Trust&…

OpenGL中Shader LOD失效

1&#xff09;OpenGL中Shader LOD失效 2&#xff09;DoTween的GC优化 3&#xff09;开发微信小程序游戏有没有类似Debug真机图形的方法 4&#xff09;射线和Mesh三角面碰撞检测的算法 这是第418篇UWA技术知识分享的推送&#xff0c;精选了UWA社区的热门话题&#xff0c;涵盖了U…

【编程语言】C/C++语言常见标准和规范

C/C 是两种功能强大且广泛使用的编程语言。尽管它们没有像 Java 那样强制性的命名规则&#xff0c;但为了提高代码的可读性和可维护性&#xff0c;遵循一些普遍认同的编程规范和标准仍然是非常重要的。本文将探讨 C/C 编程中的一些命名规范及标准&#xff0c;以帮助开发者编写更…

C#上位机通过CAN总线发送bin文件

让gpt生成一段代码用来把bin文件通过can总线发出去 c#代码还是比较强大的&#xff0c;各种功能基本都是一两行代码就实现了&#xff0c;这里记录一下对这个代码的理解和解读 主要代码如下&#xff0c;传入bin文件的地址即可将其从指定的can通道发送出去&#xff1a; public …

windows wsl ubuntu22 远程桌面连接

转载链接&#xff1a;https://canwdev.github.io/VM%E8%99%9A%E6%8B%9F%E6%9C%BA/WSL/wsl2%20wslg%20%E9%85%8D%E7%BD%AE%E5%B9%B6%E5%BC%80%E5%90%AF%E8%BF%9C%E7%A8%8B%E6%A1%8C%E9%9D%A2%28xrdp%29/ Wsl2 wslg 配置并开启远程桌面(xrdp) 准备工作 推荐到微软应用商店下载最…

ruoyi-cloud docker启动微服务无法连接nacos,Client not connected, current status:STARTING

ruoyi-cloud docker启动微服务无法连接nacos&#xff0c;Client not connected, current status:STARTING 场景 当使用sh deploy.sh base来安装mysql、redis、nacos环境后&#xff0c;紧接着使用sh deploy.sh modules安装微服务模块&#xff0c;会发现微服务无法连接nacos的情…

初学stm32 --- DAC输出三角波和正弦波

输出三角波实验简要&#xff1a; 1&#xff0c;功能描述 通过DAC1通道1(PA4)输出三角波&#xff0c;然后通过DS100示波器查看波形 2&#xff0c;关闭通道1触发(即自动) TEN1位置0 3&#xff0c;关闭输出缓冲 BOFF1位置1 4&#xff0c;使用12位右对齐模式 将数字量写入DAC_…