C++使用纯虚函数完成正方形和圆形的求面积运算。
2013-10-30 13:59
393 查看
#include <iostream>
using namespace std;
class Shape
{
public:
virtual float area()=0;
};
class Rectangle:public Shape
{
private:
float length;
float withd;
public:
Rectangle(float _l=0,float _w=0)
{
length=_l;
withd=_w;
}
float area()
{
return length*withd;
}
};
class Circle:public Shape
{
private:
float radius;
public:
Circle(float _r=0)
{
radius=_r;
}
float area()
{
return 3.14*radius*radius;
}
};
int main(int argc, const char * argv[])
{
Rectangle *rectobj;
rectobj=new Rectangle(10.23f,20.33f);
Circle *circle;
circle=new Circle(102.f);
cout<<"The rectangle area is :"<<rectobj->area()<<endl;
cout<<"The cirlce area is :"<<circle->area()<<endl;
return 0;
}
using namespace std;
class Shape
{
public:
virtual float area()=0;
};
class Rectangle:public Shape
{
private:
float length;
float withd;
public:
Rectangle(float _l=0,float _w=0)
{
length=_l;
withd=_w;
}
float area()
{
return length*withd;
}
};
class Circle:public Shape
{
private:
float radius;
public:
Circle(float _r=0)
{
radius=_r;
}
float area()
{
return 3.14*radius*radius;
}
};
int main(int argc, const char * argv[])
{
Rectangle *rectobj;
rectobj=new Rectangle(10.23f,20.33f);
Circle *circle;
circle=new Circle(102.f);
cout<<"The rectangle area is :"<<rectobj->area()<<endl;
cout<<"The cirlce area is :"<<circle->area()<<endl;
return 0;
}
相关文章推荐
- C++使用纯虚函数完成正方形和圆形的求面积运算。
- C++实现求取圆形 长方形 正方形 面积选择功能
- 使用eclipse+DevC++完成JNI调用(2)
- 设计一个形状类Shape,方法:求周长和求面积 形状类的子类:Rect(矩形),Circle(圆形) Rect类的子类:Square(正方形)
- C++中使用Mongo执行count和distinct运算
- 使用C++的string实现高精度加法运算
- [JNI] Eclipse直接完成JAVA调用C/C++ (Eclipse上使用CDT结合MinGW)
- 多态,纯虚函数,动态关联,矩形三角形圆形面积
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试
- [JNI] Eclipse直接完成JAVA调用C/C++ (Eclipse上使用CDT结合MinGW)
- 一个抽象类Shape 里边有两个抽象方法 计算面积 和 周长; Shape由三角形和圆形继承,利用多态的特性设计完成不同子类的功能
- [JNI] Eclipse直接完成JAVA调用C/C++ (Eclipse上使用CDT结合MinGW)
- C++ gmp大数运算库 window下配置使用
- cocos-js 使用clipingnode 将正方形图片裁切成圆形图片
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。
- [JNI] Eclipse直接完成JAVA调用C/C++ (Eclipse上使用CDT结合MinGW)
- c++学习笔记:使用锋利的sstringstream完成类型转换
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型,能够利用接口和多态性计算几何图形的面积和周长并显示。
- [JNI] Eclipse直接完成JAVA调用C/C++ (Eclipse上使用CDT结合MinGW)
- 在C/C++代码中使用SSE等指令集的指令(5)SSE进行加法运算简单的性能测试