题目描述:

写一个程序计算正方体、球体和圆柱体的表面积和体积

程序代码:

#include<iostream>
#define PAI 3.1415
using namespace std;
class Shape
{
public:
virtual void ShapeName()=0;
virtual void area()
{
return ;
}
virtual void volume()
{
return ;
}
};
class Cube:public Shape
{
public:
Cube(float len):length(len){};
void ShapeName()
{
cout<<"Cube:"<<endl;
}
void area()
{
double s = 6*length*length;
cout<<"Area:"<<s<<endl;
}
void volume()
{
double v = length*length*length;
cout<<"Volume:"<<v<<endl;
}
private:
float length;
};
class Sphere:public Shape
{
public:
Sphere(float r):radius(r){};
void ShapeName()
{
cout<<"Sphere:"<<endl;
}
void area()
{
double s = 4*radius*radius*PAI;
cout<<"Area:"<<s<<endl;
}
void volume()
{
double v = (4*radius*radius*radius*PAI)/3;
cout<<"Volume:"<<v<<endl;
}
private:
float radius;
};
class Cylinder:public Shape
{
public:
Cylinder(float r,float h):radius(r),length(h){};
void ShapeName()
{
cout<<"Cylinder:"<<endl;
}
void area()
{
double s = radius*radius*PAI + 2*PAI*radius*length;
cout<<"Area:"<<s<<endl;
}
void volume()
{
double v = radius*radius*PAI*length;
cout<<"Volume:"<<v<<endl;
}
private:
float radius;
float length;
};
int main()
{
Shape * pt;
pt = new Cube(2);
pt->ShapeName();
pt->area();
pt->volume();
cout<<"==========================="<<endl;
pt = new Sphere(2);
pt->ShapeName();
pt->area();
pt->volume();
cout<<"==========================="<<endl;
pt = new Cylinder(2,2);
pt->ShapeName();
pt->area();
pt->volume();
cout<<"==========================="<<endl;
}

结果输出:

Cube:
Area:24
Volume:8
===========================
Sphere:
Area:50.264
Volume:33.5093
===========================
Cylinder:
Area:37.698
Volume:25.132
===========================

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

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

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

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

    题目描述: 编写一个程序,设计一个产品类Product,其定义如下: class Product { public: Product(char *n,int p,int q); //构造函数 ~Pro ...

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

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

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

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

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

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

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

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

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

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

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

    题目描述: 写一个程序计算三角形,正方形和圆形3种图形的面积 程序代码: #include<iostream> #include<cmath> #define PAI 3.14 ...

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

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

随机推荐

  1. 将sqlServer上的数据库文件进行盘目的迁移

    在数据库客户端创建数据库时要改为.mdf文件,因为附加问价时附加的是.mdf文件: 在里选中相应的数据库 右键->任务-分离 在 剪切到相应的想放置的盘目. 例如迁移到E盘下: 在数据库-> ...

  2. 'cl.exe' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    1.首先找到vcvars32.bat文件,一般在C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin文件夹下 2.打开cmd黑窗 ...

  3. 关于Cocos2d-x中多边形物理刚体的设置

    1.如果想要设置某个物体有多边形的刚体,这样可以更精确地进行碰撞检测,可以用以下的方法 auto hero = PlaneHero::create(); addChild(hero, 0, HERO_ ...

  4. Graying the black box: Understanding DQNs

    Zahavy, Tom, Nir Ben-Zrihem, and Shie Mannor. "Graying the black box: Understanding DQNs." ...

  5. CMutex、CCriticalSection、CSemaphore、CEvent、WaitForSingleObject 的小例子

    一.CMutex CMutex mutex; mutex.Lock(); // 互斥的动作 // mutex.Unlock(); 二.CCriticalSection CCriticalSection ...

  6. e578. Setting the Clipping Area with a Shape

    This example demonstrates how to set a clipping area using a shape. The example sets an oval for the ...

  7. Linux中利用LVM实现分区动态扩容

    使用命令: pvscan vgdisplay lvdisplay vgremove vgextend lvresize -l resize2fs 从物理磁盘,创建lvm逻辑分区 pvcreate vg ...

  8. asp.net 后端验证

    using EntryRegistration.Filters; using EntryRegistration.Models.Entity; using System; using System.C ...

  9. andriod sdk 安卓模拟器修改imei码,位置信息

      imei码就是手机卡的信息一段15位数字,就好像pc的mac地址.很多app注册会检测你是否是手机登录的,就会读取你的imei码,如果读取不到,就说明你可能是用平板等移动设备上网的. app也可以 ...

  10. SQL Server 删除数据库所有表和所有存储过程

    场景: SQL Server中,需要删除所有表或所有存储过程时,手动的方式只能逐个进行删除,耗个人时间,所以想弄个语句来实现这样的需求.   如果由于外键约束删除table失败,则先删除所有约束: - ...