搜索
您的当前位置:首页正文

C++ - 多态多个实例

来源:二三娱乐

基本思路

  • 魔兽
    • 为每个怪物类编写 Attack、FightBack 和 Hurted 成员啊含糊。
    • Attact 函数表现攻击动作,攻击某个怪物,并调用被攻击怪物的 Hurted 函数,以减少攻击怪物的生命值,同时也调用被攻击怪物的 FightBack 成员函数,遭受被攻击怪反击。
    • Hurted 函数减少自身生命值,并且表现手上动作。
    • FightBack 成员函数表现反击动作,并调用被反击对象的成员函数,使被反击对象受伤。
    • 设置怪物基类,其他类从基类派生而来。
  • 多个图形类
/*****多态实例1*******/
class CShape {
    public:
        virtual double Area() = 0;
        virtual void PrintInfo() = 0;
};
class CRectangle:public CShape {
    public:
        int w,h;
        virtual double Area();
        virtual void PrintInfo();
};
double CRectangle::Area(){
    return w*h;
}
void CRectangle::PrintInfo(){
    cout<<"Rectangle:"<<Area()<<endl;
}
class CCircle:public CShape {
    public:
        int r;
        virtual double Area();
        virtual void PrintInfo();
};
double CCircle::Area(){
    return 3.14*r*r;
}
void CCircle::PrintInfo()
{
    cout<<"Circle:"<<Area()<<endl;
}
class CTriangle:public CShape {
    public:
        int a,b,c;
        virtual double Area();
        virtual void PrintInfo();
};
double CTriangle::Area(){
    double p = (a+b+c)/2.0;
    return sqrt(p*(p-a)*(p-b)*(p-c));
}
void CTriangle::PrintInfo(){
    cout<<"Triangle:"<<Area()<<endl;
}
//基类指针数组
CShape*pShapes[100];
int MyCompare(const void*s1,const void*s2);
int MyCompare(const void*s1,const void*s2){
    double a1,a2;
    CShape **p1;//s1,s2是 void*,不可写“*s1”来取得s1指向的内容
    CShape **p2;
    //s1,s2是指向 pShapes 数组中的元素,元素类型是CShape*,即 s1,s2是指向指针的指针
    p1 = (CShape**)s1;
    p2 = (CShape**)s2;
    //那么想取得他们所指向的内容只能.*p1
    a1 = (*p1)->Area();
    a2 = (*p2)->Area();
    if (a1<a2) {
        return -1;
    }else if (a2<a1){
        return 1;
    }else{
        return 0;
    }
}
//调用
int main(int argc, const char * argv[]) {
    /*****多态实例1*******/
    int i;
    int n;
    CRectangle*pr;
    CCircle *pc;
    CTriangle*pt;
    cin>>n;
    for (i = 0; i<n; i++) {
        char c;
        cin>>c;
        switch (c) {
            case 'R':
                pr = new CRectangle();
                cin>>pr->w>>pr->h;
                pShapes[i] = pr;
                break;
            case 'C':
                pc = new CCircle();
                cin>>pc->r;
                pShapes[i] = pc;
                break;
            default:
                pt = new CTriangle();
                cin>>pt->a>>pt->b>>pt->c;
                pShapes[i] = pt;
                break;
        }
    }
    //对基类数组进行排序
    qsort(pShapes, n, sizeof(CShape*), MyCompare);
    for (i = 0; i<n; i++) {
        pShapes[i]->PrintInfo();
    }
    return 0;
}
  • ***构造函数和析构函数中调用虚函数,不是多态。编译时即可确定,调用的函数是自己的类或基类中定义的函数,不会等到运行时才决定调用自己的还是派生类的函数。***
    
/*****多态实例2*******/
class Base {
public:
    void func1(){this->fun2();}
    virtual void fun2(){cout<<"Base::func2()"<<endl;}
};
class Derived:public Base {
public:
    virtual void fun2(){cout<<"Derived:func2()"<<endl;}
};
//调用
int main(int argc, const char * argv[]) {
    /*****多态实例2*******/
    Derived d;
    Base* pBase = &d;
     //在非构造函数,非析构函数的成员函数中调用虚函数,是多态。
    pBase->func1();
    //构造函数和析构函数中调用虚函数,不是多态。编译时即可确定,调用的函数是自己的类或基类中定义的函数,不会等到运行时才决定调用自己的还是派生类的函数。
    return 0 ;
}
  • 实例3
/*****多态实例3*******/
class myclass {
public:
    virtual void hello(){cout<<"hello from myclass"<<endl;};
    virtual void bye(){cout<<"bye from myclass"<<endl;}
};
//派生类中和基类中虚函数同名同参的函数,不加 virtual 也能自动成为虚函数
class son:public myclass {
public:
    void hello(){cout<<"hello from son"<<endl;}
    son(){hello();};
    ~son(){bye();};
};
class grandson:public son {
public:
    void hello(){cout<<"hello from grandson"<<endl;};
    void bye(){cout<<"bye from grandson"<<endl;}
    grandson(){cout<<"constructing grandson"<<endl;};
    ~grandson(){cout<<"destructing grandson"<<endl;};
};
int main(int argc, const char * argv[]) {
    /*****多态实例3*******/
    //生成 granson 对象
    grandson gson;
    //生成 son 的指针
    son*s ;
    //将son 的指针指向 grandson 对象
    s = &gson;
    //调用 s 指针所指向对象的 hello 方法
    s->hello();
    return 0;
}
//输出
hello from son
constructing grandson
hello from grandson
destructing grandson
bye from myclass
Top