//定義一個基類Shape,在此基礎上派生出Rectangle和Circle,兩者都有getArea()函式計算物件的面積。使用Rectangle類創建一個派生類Square。請完善下面的程式。//Hint//1、在Rectangle類中有長和寬兩個資料成員,在Circle類中有一個資料成員即半徑。//2、本題圓周率使用3.14
#include <iostream>
using namespace std;
#include <string>
class Shape {
public:
virtual float getArea() = 0;
virtual ~Shape() {}};
class Circle :public Shape
{public:
int r;
public:
Circle(int r)
{ this->r = r; }
virtual float getArea()
{ return (float)3.14 * r * r; }
};
class Rectangle :public Shape
{public:
int length;
int wide;public:
Rectangle(int length,int wide)
{
this->length = length;
this->wide = wide; }
virtual float getArea()
{ return (float)length * wide; }
};
class Square :public Rectangle
{ public:
Square(int wide):Rectangle(wide,wide)
{ }
virtual float getArea() {
return (float)wide*wide; }
};//你的代碼將被嵌在這里
int main()
{ Shape* ps; ps = new Circle(5);
cout << "The area of the Circle is " << ps->getArea() << endl;
delete ps;
Rectangle* pr;
pr = new Rectangle(5, 6);
cout << "The area of the Rectagle is " << pr->getArea() << endl;
delete pr;
Square s(8);
pr = &s;
cout << "The area of the Square is " << pr->getArea() << endl;
delete pr;
return 0;
}
//輸出://Sample Output
The area of the Circle is 78.5
The area of the Rectagle is 30
The area of the Square is 64
uj5u.com熱心網友回復:
Linux:行程意外退出會在當前目錄下產生‘core’檔案或形如‘core.數字’的檔案比如‘core.1234’
使用命令
gdb 運行程式名 core或core.數字
進入gdb然后使用bt命令
可以查看行程意外退出前函式呼叫的堆疊,內容為從上到下列出對應從里層到外層的函式呼叫歷史。
如果行程意外退出不產生core檔案,參考“ulimit -c core檔案最大塊大小”命令
Windows:
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。
uj5u.com熱心網友回復:
調過了,最后一步出錯,程式到最后不會自動結束
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/228624.html
標籤:C++ 語言
上一篇:大一新生小白求助
下一篇:求這個程式的代碼
