0 thích 0 k thích
3.8k đã xem

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;
}

 

[Exercise] Coding (C) - đã hỏi trong C++
ID: 24855 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 3.8k đã xem

17 Trả lời

0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (-298 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (-215 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (237 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (126 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (-298 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (-249 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (-136 điểm)
0 thích 0 k thích
Hidden content!
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;

}
trả lời bởi (-136 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (-189 điểm)
0 thích 0 k thích
Hidden content!
#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;

}
trả lời bởi (-368 điểm)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.159.115.10
©2016-2026

Những câu hỏi liên quan

0 thích 0 k thích
15 trả lời
[Exercise] Coding (C) - đã hỏi ngày 1 tháng 6 năm 2017 trong C++
ID: 24854 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 3.8k đã xem
0 thích 0 k thích
27 trả lời
[Exercise] Coding (C) - đã hỏi ngày 27 tháng 4 năm 2017 trong C++
ID: 24389 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 5.8k đã xem
0 thích 0 k thích
1 trả lời
[Exercise] Coding (C) - đã hỏi ngày 27 tháng 4 năm 2017 trong C++
ID: 24390 - Xem được từ: Không giới hạn - Hiệu lực đến: Không giới hạn
| 1.3k đã xem
12,783 câu hỏi
183,442 trả lời
172,219 bình luận
4,824 thành viên