1
C/C++程式設計討論區 / Re: 抽象類別實作介面
« 於: 2012-06-20 17:29 »
不是作業耶
是參加國家考試的題目
我有寫了一個程式,但執行不出來,不知是哪裡寫錯了?
可以麻煩高手指教指教嗎?手邊沒有參考書..xd..
01#include <iostream>
02using namespace std;
03const double pi=3.14159;
04class shape {
05 public:
06 //shape( char *whatis = "shape");
07 virtual int circumference() = 0;
08 virtual int area() = 0;
09}
10class circle : public shape {
11 public:
12 int circumference(int r) {
13 return (int)(2* pi* r);
14 }
15 int area(int r) {
16 return (int)(r*r* pi);
17 }
18}
19class rectangle : public shape {
20 public:
21 int circumference(int L, int W ) {
22 return (int)((L+W)*2);
23 }
24 int area(int L, int W ) {
25 return (int)(L*W);
26 }
27}
28int main(){
29 circle c1;
30 rectangle r1;
31 cout<< "圓週長"<<c1. circumference(6)<<endl;
32 cout<< "圓面積"<<c1.area (6)<<endl;
33 cout<< "方週長"<<c1. circumference(3,4)<<endl;
34 cout<< "方面積"<<c1.area (3,4)<<endl;
35system("pause");
36return 0;
37}
是參加國家考試的題目
我有寫了一個程式,但執行不出來,不知是哪裡寫錯了?
可以麻煩高手指教指教嗎?手邊沒有參考書..xd..
01#include <iostream>
02using namespace std;
03const double pi=3.14159;
04class shape {
05 public:
06 //shape( char *whatis = "shape");
07 virtual int circumference() = 0;
08 virtual int area() = 0;
09}
10class circle : public shape {
11 public:
12 int circumference(int r) {
13 return (int)(2* pi* r);
14 }
15 int area(int r) {
16 return (int)(r*r* pi);
17 }
18}
19class rectangle : public shape {
20 public:
21 int circumference(int L, int W ) {
22 return (int)((L+W)*2);
23 }
24 int area(int L, int W ) {
25 return (int)(L*W);
26 }
27}
28int main(){
29 circle c1;
30 rectangle r1;
31 cout<< "圓週長"<<c1. circumference(6)<<endl;
32 cout<< "圓面積"<<c1.area (6)<<endl;
33 cout<< "方週長"<<c1. circumference(3,4)<<endl;
34 cout<< "方面積"<<c1.area (3,4)<<endl;
35system("pause");
36return 0;
37}