7.30定义一个公共基类Shape,实现运行时的多态性
2014-08-29 18:46
549 查看
定义一个公共基类Shape,它表示一个封闭平面几何图形。
然后,从Shape类派生出三角形类Triangle、矩形类Rectangle和圆类Circle,
在基类中定义纯虚函数show和area,分别用于显示图形信息和求相应图形的面积,
并在派生类中根据不同的图形实现相应的函数。要求实现运行时的多态性。
#include <cmath>
#include<iostream>
using namespace std;
const double PI = 3.1415926535;
class Shape
{ public:
virtual void show( ) = 0;
virtual double area( ) = 0;
};
class Rectangle: public Shape
{public:
Rectangle( ){length = 0; width = 0; }
Rectangle(double len, double wid){ length = len; width = wid; }
double area( ){return length*width;}
void show( )
{ cout << "length = " << length << '\t' << "width = " << width << endl; }
private:
double length, width;
};
class Triangle: public Shape
{public:
Triangle( ){a = 0; b = 0; c = 0;}
Triangle(double x, double y, double z){a = x; b = y; c = z;}
double area( )
{ double s = (a+b+c)/2.0;
return sqrt(s*(s - a)*(s - b)*(s - c));
}
void show( )
{cout << "a = " << a << '\t' << "b = " << b << '\t' << "c = " << c << endl;}
private:
double a, b, c;
};
class Circle: public Shape
{public:
Circle( ){radius = 0; }
Circle(double r){radius = r;}
double area( ){return PI*radius*radius;}
void show( ){cout << "radius =" << radius << endl;}
private:
double radius;
};
int main( )
{ Shape *s = NULL;
Circle c(10);
Rectangle r(6, 8);
Triangle t(3, 4, 5);
c. show( );
cout << "圆面积:" << c.area( ) << endl;
s = &r;
s -> show( );
cout << "矩形面积:" << s -> area( ) << endl;
s = &t;
s -> show( );
cout << "三角形面积:" << s -> area( ) << endl;
return 0;
}
然后,从Shape类派生出三角形类Triangle、矩形类Rectangle和圆类Circle,
在基类中定义纯虚函数show和area,分别用于显示图形信息和求相应图形的面积,
并在派生类中根据不同的图形实现相应的函数。要求实现运行时的多态性。
#include <cmath>
#include<iostream>
using namespace std;
const double PI = 3.1415926535;
class Shape
{ public:
virtual void show( ) = 0;
virtual double area( ) = 0;
};
class Rectangle: public Shape
{public:
Rectangle( ){length = 0; width = 0; }
Rectangle(double len, double wid){ length = len; width = wid; }
double area( ){return length*width;}
void show( )
{ cout << "length = " << length << '\t' << "width = " << width << endl; }
private:
double length, width;
};
class Triangle: public Shape
{public:
Triangle( ){a = 0; b = 0; c = 0;}
Triangle(double x, double y, double z){a = x; b = y; c = z;}
double area( )
{ double s = (a+b+c)/2.0;
return sqrt(s*(s - a)*(s - b)*(s - c));
}
void show( )
{cout << "a = " << a << '\t' << "b = " << b << '\t' << "c = " << c << endl;}
private:
double a, b, c;
};
class Circle: public Shape
{public:
Circle( ){radius = 0; }
Circle(double r){radius = r;}
double area( ){return PI*radius*radius;}
void show( ){cout << "radius =" << radius << endl;}
private:
double radius;
};
int main( )
{ Shape *s = NULL;
Circle c(10);
Rectangle r(6, 8);
Triangle t(3, 4, 5);
c. show( );
cout << "圆面积:" << c.area( ) << endl;
s = &r;
s -> show( );
cout << "矩形面积:" << s -> area( ) << endl;
s = &t;
s -> show( );
cout << "三角形面积:" << s -> area( ) << endl;
return 0;
}
相关文章推荐
- 写一个程序,定义抽象基类Shape,由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)、Triangle(三角形)
- 写一个程序,定义抽象基类Shape,由它派生出3个派生类
- 写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。用如下的main()函数,求出定义的几个几何体的面积和
- 第十三周【任务三】【任务3】写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。
- 写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。用如下的main()函数,求出定义的几个几何体的面积和。
- 10_9_1编写代码,定义一个基类MyClass,其中包括虚礼方法GetString(),这个方法返回存储在受保护字段myString中的字符串,该字段可以通过只写公共属性ContainedStrin
- 定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果
- 公共语言运行时的另外一个作用是可以轻松地实现跨平台。当然,条件是要在操作系统 上安装.NET Framework
- 写一个程序,定义抽象基类Shape,由它派生出5个派生类
- 《第十三周任务三》写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。用如下的mian()函数,求出定义的几个几何体
- 实现在wince挂起之前运行一个程序
- 如何实现使得一个普通用户以root身份运行命令和如何使用sudo命令?
- 进程地址空间的共享(可以实现一个程序只有一个实例运行)
- 实现同时只允许运行一个程序实例
- 一个公共的实体对象基类
- C#利用Mutex实现只运行一个程序实例
- [转]实现同时只允许运行一个程序实例
- 一个用模板实现的计数基类
- 程序终于实现跨机器运行——记VC++2005程序开发的一个常见问题
- VC运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结