欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > c++ 手写queue循环队列

c++ 手写queue循环队列

2025/1/17 14:11:45 来源:https://blog.csdn.net/sbhsjjs/article/details/145147430  浏览:    关键词: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;//子类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();
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com