题目描述:

写一个程序计算三角形,正方形和圆形3种图形的面积

程序代码:

#include<iostream>
#include<cmath>
#define PAI 3.1415
using namespace std;
class Shape
{
public:
virtual float area() //定义一个求面积的成员函数
{
return 0;
}
virtual void ShapeName() = 0;//定义一个纯虚函数
};
class Triangle:public Shape
{
public:
Triangle(float x,float y,float z):a(x),b(y),c(z){};
void ShapeName()
{
cout<<"Triangle:"<<endl;
}
float area()
{
float p = (a+b+c)/2;
float s = sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
private:
float a,b,c;
};
class Square:public Shape
{
public:
Square(float l):length(l){};
void ShapeName()
{
cout<<"Square:"<<endl;
}
float area()
{
float s = length * length;
return s;
}
private:
float length;
};
class Circle:public Shape
{
public:
Circle(float r):radius(r){};
void ShapeName()
{
cout<<"Square:"<<endl;
}
float area()
{
float s = PAI*radius*radius;
return s;
}
private:
float radius;
};
int main()
{
Shape *pt;
pt = new Triangle(3,4,5);
pt->ShapeName();
cout<<"Area:"<<pt->area()<<endl;
cout<<"================================"<<endl;
pt = new Square(2.5);
pt->ShapeName();
cout<<"Area:"<<pt->area()<<endl;
cout<<"================================"<<endl;
pt = new Circle(2.5);
pt->ShapeName();
cout<<"Area:"<<pt->area()<<endl;
return 0;
}

结果输出:

Triangle:
Area:6
================================
Square:
Area:6.25
================================
Square:
Area:19.6344

C++面向对象类的实例题目十一的更多相关文章

  1. C++面向对象类的实例题目四

    题目描述: 以面向对象的概念设计一个类,此类包含3个私有数据:unlead.lead(无铅汽油和有铅汽油)以及total(当天总收入,无铅汽油的价格是17元/升,有铅汽油的加个是16元/升),请以构造 ...

  2. C++面向对象类的实例题目十

    题目描述: 编写一个程序,其中有一个汽车类vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight放在保护段中:小车类car是它的私有派生类,其中包含载 ...

  3. C++面向对象类的实例题目九

    题目描述: 编写一个学生和老师数据输入和显示程序,学生数据有编号.姓名.班号和成绩,教师数据有编号.姓名.职称和部门. 要求将编号.姓名.输入和显示设计成一个类person,并作为学生数据操作类stu ...

  4. C++面向对象类的实例题目八

    题目描述: 编写一个程序输入3个学生的英语和计算机成绩,并按照总分从高到低排序.要求设计一个学生类Student,其定义如下: 程序代码: #include<iostream> using ...

  5. C++面向对象类的实例题目七

    题目描述: 编写两个有意义的类,使一个类嵌套在另一个类中. 分析: 本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(nubner),姓名(name)和成绩(degree), ...

  6. C++面向对象类的实例题目五

    题目描述: 编写一个程序,采用一个类求n!,并输出5!的值. 程序代码: #include<iostream> using namespace std; class CFactorial ...

  7. C++面向对象类的实例题目十二

    题目描述: 写一个程序计算正方体.球体和圆柱体的表面积和体积 程序代码: #include<iostream> #define PAI 3.1415 using namespace std ...

  8. C++面向对象类的实例题目六

    问题描述: 编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数. 程序代码: #include<iostream> ...

  9. C++面向对象类的实例题目三

    编写一个程序,设计一个满足如下要求的CData类. (1)用下面的格式输出日期:日/月/年 (2)输出在当前日期上加一天后的日期 (3)设置日期 code: #include<iostream& ...

随机推荐

  1. MySQLDump 备份 Shell 脚本

    #!/bin/sh - echo "************************************" echo "----Enterprise Cloud Da ...

  2. MySQL 索引 视图 触发器 存储过程 函数

    1.索引 索引相当于图书的目录,可以帮助用户快速的找到需要的内容. 数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万 ...

  3. git教程3-添加远程库与从远程库拷贝

    一.添加到github 1.github上创建新的库learngit.git 2.git remote add origin git@github.com:moisiet/learngit.git  ...

  4. MySQL主从复制的常用拓扑结构

    1.复制的常用拓扑结构 复制的体系结构有以下一些基本原则: (1)    每个slave只能有一个master: (2)    每个slave只能有一个唯一的服务器ID: (3)    每个maste ...

  5. 统计日志中ip出现的次数

    grep -r 'GET /weixin/weixin_izp/index.html' ./chunyun.access.log > ~/access.log cat access.log |a ...

  6. Balanced Lineup(线段树的简单了解)

    个人心得:线段树就是将一段序列拆分为一个个单独的节点,不过每俩个节点又可以联系在一起,所以就能很好的结合,比如这一题, 每次插入的时候都将这一段区间的最大最小值更新,就能大大减少时间. 这个线段树建立 ...

  7. django的工作图

  8. 七、python沉淀之路--集合

    一. 1.字符串转集合 s = 'hello' se = set(s) print(se) {'e', 'o', 'h', 'l'} 2.列表转集合 l1 = ['hello','python','n ...

  9. java bean Format注解用法

    @NumberFormat(style=Style.NUMBER)    private int number; @DateTimeFormat(pattern="yyyy-MM-dd&qu ...

  10. 机器学习:评价分类结果(Precision - Recall 的平衡、P - R 曲线)

    一.Precision - Recall 的平衡 1)基础理论 调整阈值的大小,可以调节精准率和召回率的比重: 阈值:threshold,分类边界值,score > threshold 时分类为 ...