将图形类的获取周长和获取面积函数设置成虚函数,完成多态
#include <iostream>
#include <cmath>
#define PI 3.14159
using namespace std;
// 父类:图形类
class Shape
{
protected:
double perimeter;
double area;
public:
Shape():perimeter(0),area(0) {}
Shape(double p,double a):perimeter(p),area(a) {}
virtual ~Shape() {}
// 拷贝构造函数
Shape(const Shape& other):perimeter(other.perimeter),area(other.area) {}
// 拷贝赋值运算符
Shape& operator=(const Shape& other)
{
if(this!=&other)
{
perimeter=other.perimeter;
area=other.area;
}
return *this;
}
virtual double getPerimeter() const {return perimeter;}
virtual double getArea() const {return area;}
};
// 子类:矩形类
class Rectangle:public Shape
{
private:
double width;
double height;
public:
Rectangle(double w,double h):width(w),height(h)
{
perimeter=2*(width+height);
area=width*height;
}
~Rectangle() {}
// 拷贝构造函数
Rectangle(const Rectangle& other):Shape(other),width(other.width),height(other.height) {}
// 拷贝赋值运算符
Rectangle& operator=(const Rectangle& other)
{
if(this!=&other)
{
Shape::operator=(other);
width=other.width;
height=other.height;
}
return *this;
}
double getWidth() const {return width;}
double getHeight() const {return height;}
};
// 子类:圆类
class Circle:public Shape
{
private:
double radius;
public:
Circle(double r):radius(r)
{
perimeter=2 * PI * radius;
area=PI * radius * radius;
}
~Circle() {}
// 拷贝构造函数
Circle(const Circle& other):Shape(other),radius(other.radius) {}
// 拷贝赋值运算符
Circle& operator=(const Circle& other)
{
if(this!=&other)
{
Shape::operator=(other);
radius=other.radius;
}
return *this;
}
double getRadius() const { return radius; }
};
// 全局函数:打印图形信息
void printf(const Shape& shape)
{
cout<<"周长: "<<shape.getPerimeter()<<endl;
cout<<"面积: "<<shape.getArea()<<endl;
}
int main()
{
// 创建矩形对象并测试
Rectangle rect(3.0, 4.0);
cout<<"矩形:"<<endl;
cout<<"宽: "<<rect.getWidth()<<endl;
cout<<"高: "<<rect.getHeight()<<endl;
printf(rect);
// 创建圆对象并测试
Circle circle(5.0);
cout<<"圆:"<<endl;
cout<<"半径: "<<circle.getRadius()<<endl;
printf(circle);
return 0;
}
运行结果:
手动完成一个循环顺序队列
#include <iostream>
using namespace std;
class Queue
{
private:
int* data;//存储队列元素的数组
int capacity;//队列的容量
int head;//指向队列头部的索引
int tail;//指向队列尾部的索引
int count;//当前队列中的元素数量
public:
//构造函数,初始化队列
Queue(int cap):capacity(cap),head(0),tail(0),count(0)
{
data=new int[capacity];
}
//析构函数,释放动态分配的内存
~Queue()
{
delete[] data;
}
//拷贝赋值运算符,实现深拷贝
Queue& operator=(const Queue& other)
{
if(this!=&other)
{
delete[] data;
capacity=other.capacity;
head=other.head;
tail=other.tail;
count=other.count;
data=new int[capacity];
for(int i=0;i<capacity;++i)
{
data[i]=other.data[i];
}
}
return *this;
}
// 访问第一个元素
int front()
{
if(empty())
{
cerr<<"队列为空"<<endl;
exit(EXIT_FAILURE);
}
return data[head];
}
// 访问最后一个元素
int back()
{
if(empty())
{
cerr<<"队列为空"<<endl;
exit(EXIT_FAILURE);
}
return data[(tail-1+capacity)%capacity];
}
// 判空
bool empty() const
{
return count==0;
}
// 返回容纳的元素数
int size() const
{
return count;
}
// 尾插元素
void push(int value)
{
if (count==capacity)
{
cerr<<"队列已满"<<endl;
return;
}
data[tail]=value;
tail=(tail+1)%capacity;
++count;
}
// 头删元素
void pop()
{
if(empty())
{
cerr<<"队列为空"<<endl;
return;
}
head=(head+1)%capacity;
--count;
}
// 遍历队列并展示各个元素
void traverse() const
{
if(empty())
{
cout<<"队列为空"<<endl;
return;
}
cout<<"队列中的元素:";
for(int i=0;i<count;++i)
{
cout<<data[(head+i)%capacity]<<" ";
}
cout<<endl;
}
};
int main()
{
Queue queue(5); // 创建一个容量为5的循环队列
queue.push(1); // 插入元素1
queue.push(2); // 插入元素2
queue.push(3); // 插入元素3
queue.push(4); // 插入元素4
queue.push(5); // 插入元素5
cout<<"队首元素:"<<queue.front()<<endl;
cout<<"队尾元素:"<<queue.back()<<endl;
cout<<"队列大小:"<<queue.size()<<endl;
queue.traverse();//遍历
queue.pop();//头删
cout<<"删除队首元素后队首元素:"<<queue.front()<<endl;
cout<<"删除队首元素后队列大小:"<<queue.size()<<endl;
queue.traverse();//再次遍历
return 0;
}
运行结果:
今天课堂代码实现一遍:
思维导图: