C++每日一題(多態與虛函數)


# 虛函數和多態性實驗(C++)
###  計算幾種不同的三維圖形的表面積和體積,這里定義一個抽象類Cshape,將其成員函數顯示基本圖形信息函數DispAttr()、計算表面積函數Sarea()、計算體積函數Volume()定義為虛函數。根據此抽象類派生出球體、正方體、長方體和圓柱體,並分別重新實現基類中的純虛函數來顯示輸出計算結果。
~~~C++
 #include<iostream>
using namespace std;
//定義抽象類
class Cshape
{public:

    virtual void DispAttr()=0;
    virtual double Sarea()=0;
    virtual double Volume()=0;
    double r;
    double v;
    double s;
    double h;
    double l;
    double w;
};

//定義長方體類
class Cuboid :public Cshape
{public:
    
    Cuboid ()
    {
        cout << "This is one information about  a cuboid ."<< endl;
        
    }
    virtual void DispAttr()
    {
        
        
        cout << " length:" << l << endl;
        cout << " width:" << w << endl;
        cout << "height:" << h << endl;
        cout << " Surface area:" << Sarea() << endl;
        cout << "Volume:" << Volume() << endl;
    
    }
    virtual double Sarea()
    {
        s = l*(w+h)*2+w*h*2;
        return s;
    }
    virtual double Volume()
    {
        v=l*w*h ;
        return v;
    }

    

};
//定義球體
class Globe:public Cshape
{public:
    
    Globe()
    {
        cout << "This is an information about a globe."<< endl;
        
    }
    virtual void DispAttr()
    {
        
        
        cout << "radius:" << r << endl;
        cout << "Surface area:" << Sarea() << endl;
        cout << "Volume:" << Volume() << endl;
    
    }
    virtual double Sarea()
    {
        s = 3.14*r*r;
        return s;
    }
    virtual double Volume()
    {
        v= (4.0/3)*3.14*r*r*r;
        return v;
    }
};
//定義正方體
class Cube:public Cshape
{public:
    
    Cube()
    {
        cout << "This is an information about a cube."<< endl;
        
    }
    virtual void DispAttr()
    {
        
        
        cout << "Edge Length :" << r << endl;
        cout << "Surface area:" << Sarea() << endl;
        cout << "Volume:" << Volume() << endl;
    
    }
    virtual double Sarea()
    {
        s = r*r*6;
        return s;
    }
    virtual double Volume()
    {
        v= r*r*r;
        return v;
    }
    

};
//定義圓柱體
class Cylinder :public Cshape
{public:
    
    Cylinder ()
    {
        cout << "This is one information about  a cylinder ."<< endl;
        
    }
    virtual void DispAttr()
    {
        
        
        cout << " radius:" << r << endl;
        cout << "height:" << h << endl;
        cout << " Surface area:" << Sarea() << endl;
        cout << "Volume:" << Volume() << endl;
    
    }
    virtual double Sarea()
    {
        s = 2*3.14*r*h+2*3.14*r*r;
        return s;
    }
    virtual double Volume()
    {
        v=3.14*r*r*h;
        return v;
    }

    

};

void test01()
{
    //球體
    Cshape * g1 = new Globe;
    g1->r=3;
    g1->DispAttr();
    delete g1;
    cout << "------------" << endl;
    //長方體
    g1 = new Cuboid ;
    g1->l=4;
    g1->w=3;
    g1->h=5;
    g1->DispAttr();
    delete g1;
    cout << "------------" << endl;
    //正方體
    g1 = new Cube ;
    g1->r=4;
    g1->DispAttr();
    delete g1;
    cout << "------------" << endl;
    //圓柱體
    g1 = new Cylinder ;
    g1->r=4;
    g1->h=5;
    g1->DispAttr();
    delete g1;
    
}



int main()
{
    
    test01();
    return 0;
}
~~~
### 遇到的問題

- vscode終端中文亂碼,解決方法:使用英文代替中文
- ~~~
Cshape * g1 = new Globe; //最開始沒有使用
~~~
FIGHTING!



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM