菜鸟教程 
 http://www.runoob.com/cplusplus/cpp-polymorphism.html
派生类中和基类虚函数同名同参数的函数,不加virtual也自动成为虚函数 
 
 
 
 
 
#include<iostream>
#include<cstdio>
#include<cmath>//sqrt头文件
#include<cstdlib>//qsort头文件
#include<algorithm>//sort头文件
using namespace std;
class Shape
{
public:
    virtual double Area()=0;//纯虚函数
    virtual void Print()=0;//纯虚函数
};
class Rectangle:public Shape
{
public:
    double length,breadth;
public:
    virtual double Area();
    virtual void Print();
};
double Rectangle::Area()
{
    return length*breadth;
}
void Rectangle::Print()
{
    printf("Rectangle:%.2f\n",Area());
}
class Triangle:public Shape
{
public:
    double a,b,c;
public:
    virtual double Area();
    virtual void Print();
};
double Triangle::Area()
{
    double p=(a+b+c)/2;
    return sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式求三角形面积
}
void Triangle::Print()
{
    printf("Triangle:%.2f\n",Area());
}
class Circle:public Shape
{
public:
    #define PI 3.14
    double random;
public:
    virtual double Area();
    virtual void Print();
};
double Circle::Area()
{
    return PI*random*random;
}
void Circle::Print()
{
    printf("Circle:%.2f\n",Area());
}
int compare(const void *s1,const void *s2)//自定义比较函数
{
    double a1;
    double a2;
    Shape **p1,**p2;
    p1=(Shape**)s1;
    p2=(Shape**)s2;
    a1=(*p1)->Area();
    a2=(*p2)->Area();
    if(a1<a2)
        return -1;
    else if(a1>a2)
        return 1;
    else
        return 0;
}
bool cmp(Shape *a,Shape *b)
{
    return a->Area()<b->Area();
}//sort函数排序规则;
int main()
{
    Shape *p_shape[100];
    Rectangle *p_r;
    Triangle *p_t;
    Circle *p_c;
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        getchar();
        char c;
        cin>>c;
        switch(c)
        {
        case 'R':
            p_r=new Rectangle();
            cin>>p_r->length>>p_r->breadth;
            p_shape[i]=p_r;
            break;
        case 'T':
            p_t=new Triangle();
            cin>>p_t->a>>p_t->b>>p_t->c;
            p_shape[i]=p_t;
            break;
        case 'C':
            p_c=new Circle();
            cin>>p_c->random;
            p_shape[i]=p_c;
            break;
        }
    }
    cout<<"accomplish\n";
    qsort(p_shape,n,sizeof(Shape *),compare);
    //sort(p_shape,p_shape+n,cmp);
    for(int i=0;i<n;i++)
        p_shape[i]->Print();
    delete p_shape;
    return 0;
}
 

海伦公式求三角形面积 
 https://blog.csdn.net/chao1983210400/article/details/10521439
qsort函数用法 
 http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.html
                  
                  
                  
                  
                            
本文通过实例讲解了C++中的多态性和虚函数概念,包括如何在派生类中实现基类的纯虚函数,并展示了不同形状类的面积计算及排序。涉及知识点有:虚函数、纯虚函数、派生类、基类、多态性等。
          
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
              
            
                  
					2233
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
            


            