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

2023/09/12 qtc++

实现一个图形类(Shape) ,包含受保护成员属性:周长、面积,
                        公共成员函数:特殊成员函数书写
定义一个圆形类(Circle) ,继承自图形类,包含私有属性:半径
                        公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
                        公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。

#include <iostream>
#define pai 3.14159
using namespace std;
//图形类
class Shape
{
protected:double cir;   //周长double area;   //面积
public://无参构造函数Shape(){}//有参构造函数Shape(double a,double b):cir(a),area(b){cout<<"Shape有参构造函数"<<endl;}//析构函数~Shape(){cout<<"Shape析构函数"<<endl;}//拷贝构造函数Shape(const Shape &other):cir(other.cir),area(other.area){cout<<"拷贝构造函数"<<endl;}//定义拷贝赋值函数Shape &operator=(const Shape &other){this->cir =other.cir;this->area =other.area;return *this;}//移动赋值函数Shape &operator=(Shape &&other){this->cir =other.cir;this->area =other.area;return *this;}};//圆形类,继承图形类
class Circle:public Shape
{
private:double r;  //半径
public://无参构造函数Circle(){}//有参构造函数Circle(double c):r(c){cout<<"Circle有参构造函数"<<endl;}//析构函数~Circle(){cout<<"Circle析构函数"<<endl;}//拷贝构造函数Circle(const Circle &other):r(other.r){cout<<"拷贝构造函数"<<endl;}//定义拷贝赋值函数Circle &operator=(const Circle &other){this->r =other.r;return *this;}//移动赋值函数Circle &operator=(Circle &&other){this->r =other.r;return *this;}//获取周长double get_len(){cir = 2 * r * pai;return cir;}//获取面积double get_area(){area = pai * r * r;return area;}};//定义一个矩形类,继承自图形类
class Rect:public Shape
{
private:double lenth;  //长度double width;  //宽度
public://无参构造函数Rect(){}//有参构造函数Rect(double l,double w):lenth(l),width(w){cout<<"Rect有参构造函数"<<endl;}//析构函数~Rect(){cout<<"Rect析构函数"<<endl;}//拷贝构造函数Rect(const Rect &other):lenth(other.lenth),width(other.width){cout<<"拷贝构造函数"<<endl;}//定义拷贝赋值函数Rect &operator=(const Rect &other){this->width =other.width;this->lenth = other.lenth;return *this;}//移动赋值函数Rect &operator=(Rect &&other){this->width =other.width;this->lenth = other.lenth;return *this;}//获取周长double get_len(){cir = (width+lenth)*2;return cir;}//获取面积double get_area(){area = lenth * width;return area;}};int main()
{Circle c1(5.3);cout<<"周长是"<<c1.get_len()<<"   面积是:"<<c1.get_area()<<endl;Circle c2(c1);cout<<"周长是"<<c2.get_len()<<"   面积是:"<<c2.get_area()<<endl;Circle c3 = (2);cout<<"周长是"<<c3.get_len()<<"   面积是:"<<c3.get_area()<<endl;cout<<"*********************************************************"<<endl;Rect r1(2.4,9.9);cout<<"Rect周长是"<<r1.get_len()<<"   Rect面积是:"<<r1.get_area()<<endl;Rect r2(r1);cout<<"Rect周长是"<<r2.get_len()<<"   Rect面积是:"<<r2.get_area()<<endl;Rect r3(1.2,3.4);cout<<"Rect周长是"<<r3.get_len()<<"   Rect面积是:"<<r3.get_area()<<endl;Rect r4 = r3;cout<<"Rect周长是"<<r4.get_len()<<"   Rect面积是:"<<r4.get_area()<<endl;return 0;
}

http://www.lryc.cn/news/165273.html

相关文章:

  • 全科医学科常用评估量表汇总,建议收藏!
  • 了解消息中间件的基础知识
  • 【linux】Linux wps字体缺失、加粗乱码解决
  • 每日两题 103二叉树的锯齿形层序遍历(数组) 513找树左下角的值(队列)
  • ROS2报错:ImportError: cannot import name ‘Log‘ from ‘rosgraph_msgs.msg‘
  • 【Vue】Vue中的代码分为哪几种类型?
  • es6中includes用法
  • QT中QRadioButton实现分组C++
  • kafka实战报错解决问题
  • vite+react 使用 react-activation 实现缓存页面
  • 【android 蓝牙开发——蓝牙耳机】
  • Golang goroutine 进程、线程、并发、并行
  • 如何做到安全上网
  • 优维低代码实践:菜单
  • git merge 如何撤销
  • 解读package.json 中的功能
  • UMA 2 - Unity Multipurpose Avatar☀️四.UMA人物部位的默认颜色和自定义(共享)颜色
  • phpstorm配置php运行环境
  • 算法训练营day49|动态规划 part10:(LeetCode 121. 买卖股票的最佳时机、122.买卖股票的最佳时机II)
  • Swagger 使用教程
  • 单例模式-饿汉模式、懒汉模式
  • UG\NX二次开发 复制3元素的double数组到另一个数组 UF_VEC3_copy
  • 骨传导耳机对人体有危险吗?会损害听力吗?
  • Spring Boot @Value读不到Nacos配置中心的值。(properties配置文件)
  • Rocky Linux怎么安装mysql
  • 轻量级软件FastGithub实现稳定访问github
  • 芯科蓝牙BG27开发笔记6-精简第一个程序
  • Android8.1 hal 加载wifi ko模块流程
  • Unity SteamVR 开发教程:SteamVR Input 输入系统(2.x 以上版本)
  • PyTorch中,卷积层、池化层、转置卷积层输出特征图形状计算公式总结