quiz: Polymorphism & qsort

本文通过一个指针数组实例,展示了如何利用多态特性处理不同形状类的面积计算,并通过qsort函数对这些形状按面积进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

深入理解指针
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;
}
解释://studentc.h #ifndef STUDENTC_H_ #define STUDENTC_H_ #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;valarray&gt; class Student { private: // 定义别名 typedef std::valarray&lt;double&gt; ArrayDb; std::string name; ArrayDb scores; std::ostream &amp; arr_out(std::ostream &amp; os) const; public: Student(): name(&quot;Default&quot;), scores() {} // 防止隐式转换 explicit Student(const std::string &amp; s) : name(s), scores(){}; explicit Student(int n) : name(&quot;Default&quot;), scores(n) {} Student(const std::string &amp; s, int n): name(s), scores(n){} Student(const std::string &amp; s, const ArrayDb &amp; a): name(s), scores(a){} Student(const char * str, const double *pd, int n): name(str), scores(pd, n){} // 析构函数 ~Student() {} double Average() const; const std::string &amp; Name() const; double &amp; operator[](int i); double operator[](int i)const; // 友元函数 friend std::istream &amp; operator&gt;&gt;(std::istream &amp; is, Student &amp; stu); friend std::istream &amp; getline(std::istream &amp; is, Student &amp; stu); friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp; os, Student &amp; stu); }; #endif // studentc.cpp #include &quot;student.h&quot; using std::ostream; using std::endl; using std::istream; using std::string; double Student::Average() const { if(scores.size() &gt; 0) return scores.sum() / scores.size(); else return 0; } const string &amp; Student::Name() const { return name; } double &amp; Student::operator[](int i) { // 使用的是 valarray&lt;double&gt;::operator[]()方法 return scores[i]; } double Student::operator[](int i) const { return scores[i]; } ostream &amp; Student::arr_out(ostream &amp; os) const { int i; int lim = scores.size(); if(lim &gt; 0) { for(i = 0; i &lt; lim; i++) { os &lt;&lt; scores[i] &lt;&lt; &quot; &quot;; if(i % 5 == 4) os &lt;&lt; endl; } if(i % 5 != 0) os &lt;&lt; endl; } else { os &lt;&lt; &quot; empty array &quot;; } return os; } // 友元函数 istream &amp; operator&gt;&gt;(istream &amp; is, Student &amp; stu) { // 调用的是string的operator&gt;&gt;() is &gt;&gt; stu.name; return is; } istream &amp; getline(istream &amp; is, Student &amp; stu) { // 使用的是string的友元getline(istream &amp;, const string &amp;) getline(is, stu.name); return is; } ostream &amp; operator&lt;&lt;(ostream &amp; os, Student &amp; stu) { os &lt;&lt; &quot;Scores for &quot; &lt;&lt; stu.name &lt;&lt; &quot;: &quot; &lt;&lt; endl; // 使用的是私有方法arr_out stu.arr_out(os); return os; }// use_stuc.cpp // compile with studentc.cpp #include &lt;iostream&gt; #include &quot;student.h&quot; using std::cin; using std::cout; using std::endl; void set(Student &amp; sa, int n); const int pupils = 3; const int quizzes = 5; int main() { // 使用quizzes初始化Student, 然后再初始化一个ada数组里面是三个Student对象 Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)}; int i; for(i = 0; i &lt; pupils; i++) set(ada[i], quizzes); cout &lt;&lt; &quot;Student List : &quot; &lt;&lt; endl; for(i = 0; i &lt; pupils; i++) cout &lt;&lt; ada[i].Name() &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; &quot;Results:&quot;; for(i = 0; i &lt; pupils; i++) { cout &lt;&lt; endl &lt;&lt; ada[i]; cout &lt;&lt; &quot;average: &quot; &lt;&lt; ada[i].Average() &lt;&lt; endl; } cout &lt;&lt; &quot;Done&quot; &lt;&lt; endl; return 0; } void set(Student &amp; sa, int n) { cout &lt;&lt; &quot;Please enter the student&#39;s name : &quot;; getline(cin, sa); cout &lt;&lt; &quot;Please enter &quot; &lt;&lt; n &lt;&lt; &quot; quiz scores: &quot; &lt;&lt; endl; for(int i = 0; i &lt; n; i++) cin &gt;&gt; sa[i]; while(cin.get() != &#39;\n&#39;) continue; }编写他们三个的makefile文件
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值