0 喜歡 0 不喜歡
2.6k 瀏覽

Please remember our 05-04 Lab Exercise 3: Write class shape with width and height

How to use Abstract class to solve the problem?

Write class shape with width and height following a constructor that gives value to them.

Then define two sub-classes triangle and rectangle.

Those sub-classes can calculate the area of the shape area().

Remember, Rectangle area = (width * height)

Triangle area = (width * height)/2

#include <iostream>
using namespace std;
// Base class
class Shape 
{
public:
   // Your code: pure virtual function providing interface framework.
   // constructor and setWidth(int w) and setHeight(int h) functions here
protected:
   int width;
   int height;
};
// Derived classes
class Rectangle: public Shape
{
public:
   // Your code:    int getArea()
};
class Triangle: public Shape
{
public:
   // Your code:    int getArea()
};
int main(void)
{
   Rectangle Rect;
   Triangle  Tri;
   int w,h;
   cin >>w>>h;
   Rect.setWidth(w);
   Rect.setHeight(h);
   cin >>w>>h;
   Tri.setWidth(w);
   Tri.setHeight(h);
   cout << Rect.getArea() << ";" << Tri.getArea() << endl; 
   return 0;
}

 

[練習] Coding (C) - 最新提問 分類:C++ |
ID: 24855 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 2.6k 瀏覽

17 個回答

0 喜歡 0 不喜歡
內容已隱藏
#include ** * *** ** * **

using * ** * std;

// Base class

class Shape

{

public:
* *** * * ** * Your code: pure virtual function * * *** *** * *
** * * * ** *** ** * * ** and w) and ** * * h) * * ** here
* ** * * *** * *** ****** w){width = w;}
*** * **** * * ** ** ** * * = h;}
* * *
* *** * ** * ** * width;
* * ******* **** ***** * height;

};

// Derived classes

class * * public Shape

{

public:
* * * * * *** Your code: * ** *** * ** *** ** *
**** **** * * * ******* *** ** ** area;
** * * * * * * * ** ** = * ***
**** ** * *** area;
** *** * * *

};

class **** ** public Shape

{

public:
* * * * * ** Your code: ** ** ** ** ** *
*** * * * * * *
*** ** ***** ** * * area;
* ** **** **** * ***** * = (width * ****
* * * **** * ****** *** * area;
** * *** * * ****

};

int **

{
****** ** ** **** ** Rect;
** * ** * * * * * * **** **
** ***** **** * ** w,h;
* * * * *** **** ** * *** * * * ** **
* * ** ** ****** ** **** * **
* * *** ** * ****** ** **
* *** * * ** ***** ** ** * * *** **** **
**** * *** *** ** ** ** **
* ** ** * * ** ** ** ** *
** *** *** * **** * *** *** **** ** * * ***** * * ******* * ** ** ** ** endl;
** * *** * * ** *** * * 0;

}
最新回答 用戶: (-298 分)
0 喜歡 0 不喜歡
內容已隱藏
#include ** ** **

using * *** std;

// Base class

class Shape

{

public:
* *** ** * ******* **** * Your code: pure virtual function * ** ** ** *
* ** * ** * * *** ** ** and ** w) and * ** h) *** ** here
* ** *** * **** **** * w){width = w;}
* * **** *** **** * * = h;}
** **
* **** ** *** * ** width;
* * **** ** ** height;

};

// Derived classes

class * * public Shape

{

public:
* ** * ******* ** ** Your code: ** * ** * *** ** ** *
* ** * ***** ***** *** ** area;
* * ** * **** ** ** * * * = ** ** **
** **** ****** ** * * area;
* ** ** **

};

class * * * public Shape

{

public:
* ** *** ** * Your code: ** * * ****** * * ** *
* * *** *** * *** * * ***
* ** ** ** ****** *** *** area;
* * * * * * * *** = (width * * *
* ***** * ** * **** * * area;
***** *** *

};

int ** *

{
* * *** *** Rect;
** ** ** **** ** * * * * ** ** *
* ********** * * w,h;
* *** **** *** * * **** * ** *
**** *** ** * ** ** *
** **** *** ****** ** **
** ***** ****** * * * ***** ***
** ** *** ** * ** * **
* ** * ** * **** *** ** * *
*** *** * *** ** * ** *** * *** * ** ** *** ** * * * * ** * * * * ** * ** ****** endl;
* * * * * ** ** 0;

}
最新回答 用戶: (-215 分)
0 喜歡 0 不喜歡
內容已隱藏
#include * ** *** ** **

using ******* std;

// Base class

class Shape

{

public:
** *** ***** * Your code: pure virtual function * * * ********
* ***** ** ********* *** *** and ** *** * w) and ****** * * h) * * here
* * *********** ***** * * w){width = w;}
**** ** * ** ******* * = h;}
* **
* * * * * * * * width;
* ** ** * **** height;

};

// Derived classes

class * * public Shape

{

public:
* * ***** Your code: *** ** *** * ***
*** * ** * * * ** area;
* *** ******* ** * ** * **** ** = * *** * *
* * * ********** * * *** *** area;
* ** * * * *

};

class * public Shape

{

public:
** **** ****** ** Your code: ** *** *** ** ***
* * ** * ** *** ** *
** ** * **** * * ** ***** * * area;
** ** ** ** ******* * = (width * **
*** * * **** *** * ** * ** area;
** * ** * **

};

int ** *

{
* **** ***** * *** * Rect;
** * ** *** ** ** * ** ** *
*** * **** * * * w,h;
* * ** * ** ******* * * ** * ** * * * * *
* * *** *** * * * * * *
** *** ** *** * **** * **
* * *** * ** * * ** * * ****** * * *
***** * ** * ** *** ** * ***
* *** ** **** ** ** *
* ******* *** * * ** * **** ** * * ** ** *** *** ** * ** * * * * * *** * endl;
* ** * *** ** * ** ** 0;

}
最新回答 用戶: (237 分)
0 喜歡 0 不喜歡
內容已隱藏
#include ***** ***

using ** ** * std;

// Base class

class Shape

{

public:
* *** ** * ** * Your code: pure virtual function * * * * *
* *** * ** **** * ** * * and * ** * w) and *** ** h) ** * * here
* **** * **** * ** * ** w){width = w;}
* * ** * * ***** * * * * * ** * * * = h;}
* *
**** * ** * ** * width;
** ***** * *** ** height;

};

// Derived classes

class * public Shape

{

public:
* *** * * *** ** ** * Your code: * ** * * * * * * *** * **
*** * * * ** ** * * *** area;
* * *** *** **** * * ** = * * *
*** *** *** * * *** ** * area;
* ** * *

};

class * ** public Shape

{

public:
* ** * ** * *** * Your code: * * * * * * *
* * * ** * * ** * * *
* ** *** ** * ** area;
* ** **** * * * **** *** = (width * * *
* * * ***** ** * * * area;
*** * ** ** ******

};

int *

{
* ** ************ * **** Rect;
**** ***** * *** ** * *
*** ***** * ** * * w,h;
* *** *** *** * * * * ** *
* * ** ** ** ** **
* **** *** * *** * ** *
**** ** *** * ** * * *** *************
** * * ***** * ******
** * *** * **** ** * *
*** ** * * ** * * * * ** *** * ** ** * ** **** * * *** * * * * *** * * * ** ***** endl;
* * * ** 0;

}
最新回答 用戶: (126 分)
0 喜歡 0 不喜歡
內容已隱藏
#include * ** * * * *** *



using ** std;



class Shape

{
* ***
* **** ** ** * *** * * width, height;

public:
** * ** ** * ** * set_data (float a, float b)
** *** * * **
* * * ** ******** *** * ** ** * = a;
* * * * ** * * * * * ** * ******** ** * *** * * * = b;
* * **** * * ******* * **

};



class *** public Shape

{

public:
* ** ** * * * *** * *** * area ()
* ** * * *** * ** ** *
* *** * * * * * * *** *** ** * * * * (width * height);
** ** ** ** * ** *****

};



class * **** public Shape

{
*
* * * ************* * * area ()
* * * ** * ** * * * *
*** ** * * * * **** *** * * ** ** *** ** (width * height / 2);
*** ** * *** ** * **

};



int main (){
*** ** * * *** ***
* * ** * * *** * ** ** rect;
*** ** ** ** * * * * * * tri;
** * * * *** *** * *** ** *** (5,3);
* ** ** ** ****** * ** * (2,5);
*** * ******** * **** ***** **** *** * ***** * * * ****** * ** *** * ** ** * endl;
* * ***** * * ** * ** 0;

}
最新回答 用戶: (-298 分)
0 喜歡 0 不喜歡
內容已隱藏
#include *** * ****

using namespace std;

class Shape

{

public:
*** * * * *** * * int setWidth(int n1){
* ****** * ** * *** ****
* ** * * * * **
* * ** * * * * *** * ***** int setHeight(int n2){
***** * ** ** ***** ** * *
*** * ****** * ****
** ** * * * * * getWidth(){
** * * **** ** * *** * width;
* ** **** * * * **
* ** **** * getHeight(){
** * * * *** ** * * *** ** height;
* * * * ***** **

protected:
* *** * * *** width;
* * * ** * * * height;

};



class Rectangle: public Shape

{

public:
* **** ** ** getArea(){
* ** **** ** * ** * ** ** ** *** * ** * * * **** *
** * * * ****

};

class Triangle: public Shape

{

public:
* ***** * ****** * * getArea(){
** ** * **** * * * * * ** ** * *** **** ** **
* * ** ** **

};

int main(void)

{
********** ** * **** Rect;
* * *** *** * **** * * **
** **** ** ** * * w,h;
** *** *** * * * ** * *** **
**** * ***** ***** * ***
* * ** ** ***** ** ** **
*** * ** ** ** ** * **** ** * *
* ** **** ** ** *** * *
** * *** **** **** *
** *** ** ** * ** * * ** * *** * * ** * ** ** * ** **** ******* **** * * * * ** * * endl;

* * *** * * 0;

}
最新回答 用戶: (-249 分)
0 喜歡 0 不喜歡
內容已隱藏
#include * ******** * *

using namespace std;

// Base class

class Shape

{

public:
******** * ** ** * Your code: pure virtual function *** *** * * ***** *
** ** *** * ** ** * * and ** * * w) and * ** h) ** here
* * ** * * * **** x);
** * * ***** *** ** *** * * * y);
** ***
* * **** **** * ****** width;
* **** ** * ** height;

};

// Derived classes

class * public Shape

{

public:
* ** ** * ** ** * Your code: ***** ** * * ** * *
**** ** **** *** **
* * * ** * **** * * ** * ** * * * ***
* * * *** * **

};

class Triangle: public Shape

{

public:
* ** * *** * **** Your code: **** *** ***** * * * * *
* ** ** * * *** *** * *
** *** * * ****** ****** ***** *** ** * ** ** *
* * * * ** ** * **

};
* *

int * * * x){
** * * **** * ** ******* *** = x;

}

int *** * * * *** y){
*** ***** * ** ***** * * *** ** = y;

}



int ***

{
* * *** ** Rect;
*** *** ** ** ** ** **** ***
* * * ******* * w,h;
** ** *** * * ** * ** *
** *** **** * **** *
* **** * * *** * * * *
* **** ** * *** * * * ** *** * ** * ***
** *** * * * * * *** * * ****
* * *** **** *****
* **** * ** * ** ** * * * * ** ** * ** **** * * ** *** *** * * * ***** endl;

**** * **** ***** ** ** 0;

}
最新回答 用戶: (-136 分)
0 喜歡 0 不喜歡
內容已隱藏
using * ** * std;

// Base class

class Shape

{

public:
** * *** * ** * * Your code: pure virtual function * ** * * * * *
* ** ********* * * * **** and **** * *** w) and * ** h) functions here
* * * ****** ** * * ** * x);
* ** *** ** * * * * * * y);
* ***
* *** * ** **** * * width;
** * ** ** * * height;

};

// Derived classes

class * ** public Shape

{

public:
* ** * **** * * Your code: * * * ** **** getArea()
* * ** * * * * *
* ** *** ** * * * * ****** *** *** * * * * * ****
* * * ** * ** * ***

};

class * * public Shape

{

public:
*** ** ****** * Your code: * * * *** ***** *
* *** * ** * * * *** * * *
* ** **** * * ** **** * * * *** * * ** ** ***** * **
**** * * * *** *** ***

};
*** *

int ** ** * **** x){
** **** * ** * * * ***** * = x;

}

int * *** * * * y){
* * ***** * *** * * * * = y;

}



int **

{
*** ** ** * * * Rect;
* * * ****** * ** *
** * * * **** * * w,h;
** * * * ** * * * ***
** ** * ** *** * *** * * *
** ** *** * * **** **** *
* * *** ** *** ** *** * *** **
* ** * * ** * ** * *****
* ** * * * **** ***
* ** ** **** *** ** *** *** * ** * * * * * * *** * ***** **** * ***** endl;

*** * ** ** **** * *** 0;

}
最新回答 用戶: (-136 分)
0 喜歡 0 不喜歡
內容已隱藏
#include * * * *

using * * * ** std;

// Base class

class Shape

{

public:
* * * *** * ** * Your code: pure virtual function ** ** ** ***
*** *** * * * ****** * * *** * and * ** w) and * * h) *** here
* ** *** ** w){width = w;}
* * **** * * ** * * * *** * = h;}
* **
* * * *** *** * width;
*** * *** * * height;

};

// Derived classes

class * *** public Shape

{

public:
* *** * * * *** Your code: * ** ** *** ** * ** * *
** * * **** * * ** * ****** area;
** * * ****** *** ** = * ** * *
**** ** ** **** ***** ** * area;
* * * ** ** **

};

class *** public Shape

{

public:
* *** * * Your code: * * **** *** ** * * * ***
* * * ** * ***
** * * * ** ** * *** * area;
* ** *** ****** * = (width * * **
* ** ** * * **** * **** area;
** ** ** * **

};

int **

{
*** *** ** * ****** Rect;
**** * * * * *** * *
** ***** *** * ** * w,h;
**** ****** ***** * * ** ** * *** **
**** ******* ** * *
* *** ** ** ** **** *****
** * * ***** * * * * *** ****** * * **
**** * ****** *** ***
** *** ***** * * **** *
* ** * * *** ** * ** ** * * ** * *** * * * * ** * ** ** * * ** * * * ** ** * endl;
* ** ** * * * 0;

}
最新回答 用戶: (-189 分)
0 喜歡 0 不喜歡
內容已隱藏
#include ** **** **

using namespace std;

// Base class

class Shape

{

public:
* ** * * ********** * Your code: pure virtual function providing interface framework.




*** * ** ***** **** *
** * * * ** * * *** * *** ** * * *
* * * * ** * ** * * * * * **** *** * * *
* *** **** *
**** ***** ** * * *** *** in){
* * **** * * *** ** * * * ** ** **
** * *
**** * * * * * * ** * * ***** in){
***** ** * *** ** *** *** * **** **** ** **
* ** *** * ***
*** **** *** ** constructor and * * * * w) and * *** * h) functions here
****
** * * ***** ** * * * * * width;
* * * ** ** * ** height;

};

// Derived classes

class Rectangle: public Shape

{

public:
******* * * ** * Your code: * * ** * * **** ***** getArea()
*** **** ** * **** * *** ****
*** * *** ** *** **** *** **** * **** *
** * **** * * *** *



};

class Triangle: public Shape

{

public:
*** ** * * * * * * * Your code: *** *** ***** **** * getArea()
** ***** * * ** * * * *
** ****** * ***** * ** * ** **** ** * * * ** * * *** *
** ***** ** *



};

int main(void)

{
** ** * **** * * * Rect;
* ** ** ***** * **** * *** * *
* ****** ****** ** w,h;
* ***** * * * ** ** ** * ** * *
* **** * *** * ** **
**** ***** * * * * ** * *
* *** * * ***** ** ** *** * *
******** * * ** ** * * ****
* * * **** * * * * **
* * **** * * *** * ** *** ** * * ** * * * ** ** ** ** ** * * *** * *** *** endl;
* * * ** * *** * ** 0;

}
最新回答 用戶: (-368 分)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:108.162.241.185
©2016-2025

相關問題

0 喜歡 0 不喜歡
15 回答
[練習] Coding (C) - 最新提問 6月 1, 2017 分類:C++ |
ID: 24854 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 2.6k 瀏覽
0 喜歡 0 不喜歡
27 回答
[練習] Coding (C) - 最新提問 4月 27, 2017 分類:C++ |
ID: 24389 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 4k 瀏覽
0 喜歡 0 不喜歡
1 回答
[練習] Coding (C) - 最新提問 4月 27, 2017 分類:C++ |
ID: 24390 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 928 瀏覽
12,783 問題
183,442 回答
172,219 留言
4,824 用戶