作业代码
- #include
-
- using namespace std;
-
- class Shape
- {
- protected:
- double cir;
- double area;
- public:
- //无参构造
- Shape() {cout<<"无参构造"<
- //有参构造
- Shape(double c, double a):cir(c), area(a){cout<<"有参构造"<
- //析构函数
- ~Shape(){cout<<"析构函数"<
- //拷贝构造
- Shape(const Shape&other):cir(other.cir), area(other.area)
- {
- cout<<"拷贝构造"<
- }
- //拷贝赋值
- Shape& operator=(const Shape& other)
- {
- if(this != &other)
- {
- this->cir = other.cir;
- this->area = other.area;
- cout<<"拷贝赋值"<
- }
- return *this;
- }
- //移动赋值
- Shape& operator=(Shape&& other)
- {
- this->cir = other.cir;
- this->area = other.area;
- return *this;
- }
- };
-
- class Circle:public Shape
- {
- private:
- double rad;
- public:
- //无参构造
- Circle() {cout<<"无参构造"<
- //有参构造
- Circle(double r):rad(r){cout<<"有参构造"<
- //析构函数
- ~Circle(){cout<<"析构函数"<
- //拷贝构造
- Circle(const Circle&other):rad(other.rad){cout<<"拷贝构造"<
- //拷贝赋值
- Circle& operator=(const Circle& other)
- {
- if(this != &other)
- {
- this->rad = other.rad;
- cout<<"拷贝赋值"<
- }
- return *this;
- }
- //移动赋值
- Circle& operator=(Circle&& other)
- {
- this->rad = other.rad;
- return *this;
- cout<<"移动赋值"<
- }
- //获取周长
- double get_cir()
- {
- Shape::cir = rad*2*3.14;
- return Shape::cir;
- }
- //获取面积
- double get_area()
- {
- Shape::area = rad*rad*3.14;
- return Shape::area;
- }
- };
-
- class Rect:public Shape
- {
- private:
- double len;
- double width;
- public:
- //无参构造
- Rect() {cout<<"无参构造"<
- //有参构造
- Rect(double l, double w):len(l), width(w){cout<<"有参构造"<
- //析构函数
- ~Rect(){cout<<"析构函数"<
- //拷贝构造
- Rect(const Rect&other):len(other.len), width(other.width){cout<<"拷贝构造"<
- //拷贝赋值
- Rect& operator=(const Rect& other)
- {
- this->len = other.len;
- this->width = other.width;
- return *this;
- }
- //移动赋值
- Rect& operator=(Rect&& other)
- {
- this->len = other.len;
- this->width = other.width;
- return *this;
- }
- //获取周长
- double get_cir()
- {
- Shape::cir = (len+width)*2;
- return Shape::cir;
- }
- //获取面积
- double get_area()
- {
- Shape::area = len*width;
- return Shape::area;
- }
- };
-
- int main()
- {
- Circle c(3);
- cout<<"c_cir = "<
get_cir()< - cout<<"c_area = "<
get_area()< - Rect r(3.5, 5.5);
- cout<<"r_cir = "<
get_cir()< -
-
相关阅读:
Java之接口
矩阵运算_矩阵的协方差矩阵/两个矩阵的协方差矩阵_求解详细步骤示例
初始化一个Vue3项目
C语言——1.入门须知
从理论到实践:如何用 TDengine 打造完美数据模型
【华为IP阶段OSPF3】--- 四类特殊区域及OSPF协议特性
K8S的安装kubernetes-dashboard服务起来了,访问不到解决
模板语法2
Win系统VMware虚拟机安装配置(一)(附激活码安装包)
haproxy实验
-
原文地址:https://blog.csdn.net/mcslll/article/details/132839812