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

c++ 手写queue循环队列

继承与多态

继承

父子出现同名的成员问题

#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;//子类name
   cout<<"&father="<<&son.Father::name<<endl;///访问父类name

   cout<<"&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.kler.cn/a/505550.html

相关文章:

  • Windows 10 ARM工控主板连接I2S音频芯片
  • ASP.NET Core - 缓存之分布式缓存
  • o3模型重大突破:引领推理语言模型新纪元,展望2025年AI发展新格局
  • Flutter:封装ActionSheet 操作菜单
  • Mac玩Steam游戏秘籍!
  • [Linux]Docker快速上手操作教程
  • Windows 上的 MySQL 8.4.3 和 WSL(Ubuntu)的 MySQL 8.0.40 之间配置 主从同步
  • linux系统监视(centos 7)
  • 数据结构9——二叉搜索树
  • 使用Struts2遇到的Context[项目名称]启动失败问题解决(Java Web学习笔记)
  • 虚拟线程JDK与Spring Core Reactor
  • 2025windows环境下安装RabbitMQ
  • Frida调试il2cpp的程序打印原生c#对象为json
  • Qt 5.14.2 学习记录 —— 십이 QLineEdit、QTextEdit
  • win32汇编环境,窗口程序中组合框的应用举例
  • 如何将一个数组转换为字符串?
  • 01、kafka知识点综合
  • [Linux]Docker快速上手操作教程
  • LevelDB 源码阅读:如何优雅地合并写入和删除操作
  • 【MySQL学习笔记】MySQL存储过程
  • 通信与网络安全管理之ISO七层模型与TCP/IP模型
  • 计算机后端学习路径(精华版)
  • 仪式感在会员体系建设中的重要性及AI智能名片2+1链动模式S2B2C商城小程序的应用研究
  • 神经网络基础-网络优化方法
  • Lua调用C#
  • YOLOv11 OBB 任务介绍与数据集构建要求及训练脚本使用指南