quiz: Polymorphism & qsort
来源:互联网 发布:windows程序设计书籍 编辑:程序博客网 时间:2024/06/05 04:49
深入理解指针
CShape *pShapes[MAX];//指针数组 ATTATION
#include <iostream>
#include <cmath>
using namespace std;
class CShape{
public:
virtual double area(){
return 0;
}
virtual void PrintArea(){}
};
class CTriangle:public CShape{
public:
int a;
int b;
int c;
virtual double area(){
double p = (a + b + c)*0.5;
return sqrt(p*(p - a)*(p - b)*(p - c));
}
void PrintArea(){
cout << "Triangle:" << area() << endl;
}
};
class CRectangle :public CShape{
public:
int width;
int height;
virtual double area(){
return width*height;
}
void PrintArea(){
cout << "Rectangle:" << area()<< endl;
}
};
class CCircle :public CShape{
public:
int r;
const double pi = 3.14;
virtual double area(){
return pi*r*r;
}
void PrintArea(){
cout << "Circle:" << area() << endl;
}
};
#define MAX 100
int MyCompare(const void *ele1_,const void *ele2_){
CShape **p1, **p2;
p1 = (CShape **)ele1_;
p2 = (CShape **)ele2_;
double a1, a2;
int tmp;
a1 = (*p1)->area();
a2 = (*p2)->area();
if (a1 < a2)
tmp = -1;
else if (a1 > a2)
tmp = 1;
else
tmp = 0;
return tmp;
}
int main(){
int n;
CRectangle *pr;
CCircle *pc;
CTriangle *pt;
CShape *pShapes[MAX];
cin >> n;
for (int i = 0; i < n; i++){
char c;
cin >> c;
switch (c){
case 'R':
pr = new CRectangle();
cin >> pr->height >> pr->width;
pShapes[i] = pr;
break;
case 'T':
pt = new CTriangle();
cin >> pt->a >> pt->b >> pt->c;
pShapes[i] = pt;
break;
case 'C':
pc = new CCircle();
cin >> pc->r;
pShapes[i] = pc;
break;
}
}
qsort(pShapes, n, sizeof(CShape*), MyCompare);
for (int i = 0; i < n; i++)
pShapes[i]->PrintArea();
return 0;
}
0 0
- quiz: Polymorphism & qsort
- Polymorphism
- Polymorphism
- polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- Polymorphism
- polymorphism
- polymorphism
- QSORT
- qsort
- QSort
- qsort
- qsort
- qsort
- Redis内存使用优化与存储
- 程序设计——意图与逻辑(二)
- Android--活动的实践
- CArray 2&CHugeInt
- class MyString
- quiz: Polymorphism & qsort
- 《Android 4 编程入门经典》读书笔记 之 简单进度对话框
- POJ_S1E01_Enumeration & Basic Promblems
- POJ_S1E02_Recursion&DP(1)
- 【机房收费系统重构版】一键检查、清空控件text属性
- POJ_S1E02_Recursion&DP(2)
- POJ_S1E02_Recursion&DP(3)
- POJ_S1E02_Recursion&DP(4)
- POJ_S1E03_DFS&BFS(1)