首页 > 编程语言 >编程一小时2023.4.22

编程一小时2023.4.22

时间:2023-04-22 21:22:49浏览次数:37  
标签:22 area double 编程 virtual class Shape 2023.4 public

1.

#include<iostream>
using namespace std;
#define PI 3.1415926
class Shape{
public:
virtual double area()=0;
};

class Circle:public Shape
{
private:
double radius;
public:
Circle(double a)
{
radius=a;
}
virtual double area()
{
return PI*radius*radius;
}
};

class Square:public Shape
{
private:
double x;
public:
Square(double a)
{
x=a;
}
virtual double area()
{
return x*x;
}
};

class Rectangle:public Shape
{
private:
double l,w;
public:
Rectangle(double a,double b)
{
l=a;
w=b;
}
virtual double area()
{
return l*w;
}
};

class Trapezoid:public Shape
{
private:
double d1,d2,h;
public:
Trapezoid(double a,double b,double c)
{
d1=a;
d2=b;
h=c;
}
virtual double area()
{
return (d1+d2)*h/2;
}
};

class Triangle:public Shape
{
private:
double d,h;
public:
Triangle(double a,double b)
{
d=a;
h=b;
}
virtual double area()
{
return d*h/2;
}
};

int main()
{
double x1,x2,x3,x4,x5,x6,x7,x8,x9;
cin>>x1>>x2>>x3>>x4>>x5>>x6>>x7>>x8>>x9;
Shape*shape[5]={new Circle(x1),new Square(x2),new Rectangle(x3,x4),new Trapezoid(x5,x6,x7),new Triangle(x8,x9)};
double s;
s=shape[0]->area()+shape[1]->area()+shape[2]->area()+shape[3]->area()+shape[4]->area();
cout<<"total of all areas = "<<s;
return 0;
}

2.

#include<iostream>
#include<iomanip>
using namespace std;
class Shape {
public:
virtual void display() = 0;
};
class Circle :public Shape {
double radium;
public:
Circle(double r) :radium(r) {}
virtual void display();
};
class Rectangle :public Shape {
double lenth, width;
public:
Rectangle(double l, double w) :lenth(l), width(w) {}
virtual void display();
};
class Triangle :public Shape {
double high, bottom;
public:
Triangle(double h, double b) :high(h), bottom(b) {}
virtual void display();
};
void Circle::display() {
cout << fixed << setprecision(2) << 3.1415 * radium * radium << endl;;
}
void Rectangle::display() {
cout << fixed << setprecision(2) << lenth * width << endl;
}
void Triangle::display() {
cout << fixed << setprecision(2) << 0.5 * high * bottom << endl;
}
int main()
{
double radium, high, lenth, width, bottom;
cin >> radium >> lenth >> width >> high >> bottom;
Shape* p[3];
p[0] = new Circle(radium);
p[1] = new Rectangle(lenth, width);
p[2] = new Triangle(high, bottom);
for (int i = 0; i < 3; i++) {
p[i]->display();
delete p[i];
}
}

 

标签:22,area,double,编程,virtual,class,Shape,2023.4,public
From: https://www.cnblogs.com/zbl040721/p/17344004.html

相关文章

  • 实验三 控制语句与组合数据类型应用编程
    实验任务1源代码1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print('\n用集合存......
  • 2022.4.22编程一小时打卡
    一、问题描述:请编写一个计数器Counter类,对其重载运算符“+”。二、解题思路:首先编写一个Counter类,然后,进行编写运算符“+”的重载,最后,进行代码的运行编译进行验证。三、代码实现:1#include<iostream>2#include<string>3usingnamespacestd;4classCounter5{......
  • 每日总结2023/4/22
    今天对图形界面做了优化,增加了切换账号,点击选择时间        ......
  • 控制语句与组合数据类型应用编程
    importrandomprint('用列表存储随机整数:')lst=[random.randint(0,100)foriinrange(5)]print(lst)print('\n用集合存储随机整数:')s1={random.randint(0,100)foriinrange(5)}print(s1)print('\n用集合存储随机整数:')s2=set()whilelen(s2)......
  • 实验3 控制语句与组合数据类型应用编程
    实验任务1task1.py1importrandom23print('用列表存储随机整数:')4lst=[random.randint(0,100)foriinrange(5)]5print(lst)67print('\n用集合存储随机整数:')8s1={random.randint(0,100)foriinrange(5)}9print(s1)1011print(......
  • 4.22趣味百题
    一问题描述 一辆固定车速的汽车,司机在上午10点看到里程表是一个对称数95859即从左往右和从右往左读一样两小时后看到里程表上出现一个新的对称数仍为对称数问该车的速度是多少新的对称数是多少?二设计思路两小时后的里程数一定大于95859但为五位数一定小于100000可以用......
  • 【CMU15-445 FALL 2022】Project #0 - C++ Primer
    关于参考&鸣谢课程官网CMU15445vscode/clionclang12cmake环境配置C++调试窗口显示“forstringvariable【CMU15-445数据库】bustubProject#0:Trie树实现(C++Primer)2022CMU15-445学习群——152391370前言按照课程要求,本文并不会给出实现代码,可以当做是我遇到问题的总......
  • Qt5.12实战之图形编程初识
    演示效果: 1.绘制条件:1.绘图设备-> QPainter2.画笔->QPen --->字体(QFont)3.画刷->QBrush-->自己定义画刷(QPixmap)4.绘制事件->QPaintEvent绘图步骤:1.重写基类的虚函数 voidpaintEvent(QPaintEvent*event); 2.在虚函数 voidpaintEvent(QPaintEvent*event)的实现函......
  • java -- 网络编程
    软件结构C/S结构:全称为Client/Server结构,是指客户端和服务器结构。常见程序有QQ、迅雷等软件。B/S结构:全称为Browser/Server结构,是指浏览器和服务器结构。常见浏览器有谷歌、火狐等。网络通讯协议网络通信协议:通信协议是对计算机必须遵守的规则,只有遵守这些规则,计算机之间......
  • 打卡 c语言趣味编程
     1.百钱百鸡#include <stdio.h>int main(){ int cock, hen, chicken; for (cock = 0; cock <= 20; cock++) { for (hen = 0; hen <= 33; hen++) { for (chicken = 0; chicken <= 100; chicken++) { if ((5 * cock + 3 * hen + chic......