0 like 0 dislike
3.5k views

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) - asked in C++
ID: 24855 - Available when: Unlimited - Due to: Unlimited
| 3.5k views

17 Answers

0 like 0 dislike
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;

}
answered by (-298 points)
0 like 0 dislike
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;

}
answered by (-215 points)
0 like 0 dislike
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;

}
answered by (237 points)
0 like 0 dislike
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;

}
answered by (126 points)
0 like 0 dislike
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;

}
answered by (-298 points)
0 like 0 dislike
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;

}
answered by (-249 points)
0 like 0 dislike
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;

}
answered by (-136 points)
0 like 0 dislike
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;

}
answered by (-136 points)
0 like 0 dislike
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;

}
answered by (-189 points)
0 like 0 dislike
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;

}
answered by (-368 points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.159.115.32
©2016-2026

Related questions

0 like 0 dislike
15 answers
[Exercise] Coding (C) - asked Jun 1, 2017 in C++
ID: 24854 - Available when: Unlimited - Due to: Unlimited
| 3.5k views
0 like 0 dislike
27 answers
[Exercise] Coding (C) - asked Apr 27, 2017 in C++
ID: 24389 - Available when: Unlimited - Due to: Unlimited
| 5.3k views
0 like 0 dislike
1 answer
[Exercise] Coding (C) - asked Apr 27, 2017 in C++
ID: 24390 - Available when: Unlimited - Due to: Unlimited
| 1.2k views
12,783 questions
183,442 answers
172,219 comments
4,824 users