0 like 0 dislike
1.2k 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
| 1.2k views

17 Answers

0 like 0 dislike
Hidden content!
#include *** * ** *



using ****** std;



// Base class

class Shape **



public:
* * ******* *** ***** pure virtual function *** * * * *
** * *** ** *** * * int ** * = 0;


* * * * * **** * ** w) {
* *** * * * ** * * * *** * ** ** * = w;
** *** * * ***


** * * * * *** * *** *** ** * * h) {
* * ***** * * *** ****** ***** *** * = h;
**** * ** ** ** **


*
* ** * ** * * width;
*** * * ** * * height;

};



// Derived classes

class ** ** public Shape {



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

};



class * public Shape {


*
* * * * * ** *** {
* * * ** * ** *** * ** * *** **** (width * * *
****** * * ** **

};



int ** * {
* * *** *** ******** ** * Rect;
* **** * * ** * * ** *
* * *** * * w,h;
* ** * *** ****** ** * ** ** ** * **** ****
* ** * * *** ****** * *
*** * * * ***** * *** *
** ** * * * * * * * ***** * **** **
** * * *** *
* * * * ***** **
** * * * * * * Print the area of the object.
********** * *** **** ** * ** ******* * *** * ** * * *** * * * **** *** ** **** * ** * * ** * endl;
*** * * ** ** ** 0;

}
answered by (-48 points)
0 like 0 dislike
Hidden content!
* ** * *** ***

using ** ** std;

// Base class

class Shape

{

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

};

// Derived classes

class public Shape

{
** * *
**** * * * **** Your code: ** **** * ** * *
* * ** * * * *** * * ** **
**** *** * *** * * * return * ** ****
* * * ** * * **

};

class * public Shape

{
* ** *
* *** **** ***** ** Your code: * * * ** ** * ** * * **
*** ** * **** ** * *
* ****** *** ** * ***** ** * ** return * * ***
** *** ** *

};
* * **

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

}

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

}



int * *

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

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

}
answered by (-134 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 (-581 points)
0 like 0 dislike
Hidden content!
#include * ** * ** *

using namespace std;

// Base class

class Shape

{

public:
* *** * * ** * ** setWidth(int W)
* * ** *** ******
* * *** ** ******* * **** * * ** ******
* *** * ** **
* ** * *** * * ** ** setHeight(int H)
** **** **** ***
** ** * * ** ** *** * * * * *** * **
*** * * * * **** **


*** *** * **** getH(void)
** **** *** * *
** * * * ** * *** ** ** ****** * * * *** * ** height;
* * * ****** ** *
* ** * * **** * * getW(void)
* ** ** ** ** *
*** * * **** * * * * ** ** **** width;
* *** * ***** * *



private:
* ***** * ** *** * * width;
**** * * * * height;

};

// Derived classes

class Rectangle: public Shape

{

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

};

class Triangle: public Shape

{

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

};

int main(void)

{
* * *** ** *** * ** ** Rect;
**** *** **** * *  Tri;
** * * ******* *** ** * w,h;
* *** * ** ****** * * ** * * *** *** **
* * * * * * * * ** ** **
*** ** *** * ******* ** *
* ** * * * **** * **** **** ** * * ***
*** *** ** * * * ** **** *
** * *** * * * * *
*** *** * ** **** * * * * Rect.getArea() * ****** * *** **** *** * * ** Tri.getArea() * ** * endl;
** * **** * 0;

}
answered by (-324 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 (-384 points)
0 like 0 dislike
Hidden content!
#include ** * *

using namespace std;

// Base class

class Shape

{

public:


**** * ** ** *** ** * setWidth(int w)
* ** **** *
** * ** *** ** * * ** ***** * ***** = w;
* * * * * ***
* * * * * * *** * * ** ** h)
****** ** *** *** ***
* * * * ****** * **** *** * * * * * *** * ** = h;
* *** * *

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

};

// Derived classes

class Rectangle: public Shape

{

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

};

class Triangle: public Shape

{

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

};

int main(void)

{
***** * * * * **** * Rect;
* ** * ** **** * *
* ** * * * * * * w,h;
*** * ** ** *** ***** *** ** * **** ** *
* * * * ** *** * * **
** ** * * * ** **** **
* * * * * * ***** * * ** * * ** ** *** ***
** *** * ** * ** * *
** ** ** * * ***
* ** ** *** ** ****** *** ** *** * * ** * * * ** * *** * *** Tri.getArea() ***** ** * endl;
** * * * * * * *** ** 0;

}
answered by (-364 points)
0 like 0 dislike
Hidden content!
#include ********* *

using namespace std;

// Base class

class Shape

{

public:
** * * * *** ** * * Your code: pure virtual function **** interface * *
** ** * * * *** ** and *** * ** w) and ***** * h) **** here
* * **** * * *** ** * int * **** = 0;


** ** **** ** ** * ** * w){
* ** * * ** * * * * * * * * ** * * * * ** = w;
* * * * *** ** ***
****** ** **** ********** h){
* * * ** *** * * * **** *** ** ** * *** * = h;
*** * * *


* *
* * * * * ** *** ** ** width;
******* * * * * * * height;

};

// Derived classes

class *** public Shape

{

public:
********* * **** Your code: **** **** ****** ** * **
**** ** **** *** ** ** ** *
** ** ** * **** * * * **** * ***** *** *** * * width * height;
* * ** ********

};

class * public Shape

{

public:
** * *** ** * ***** * * Your code: * ** * ** getArea()
** * * ** ****** * *
** *** * * * * ** ** ** ** * * * * *** * * ** width * height / 2;
* * * * *** *

};

int **

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

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

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
| 1.2k views
0 like 0 dislike
27 answers
[Exercise] Coding (C) - asked Apr 27, 2017 in C++
ID: 24389 - Available when: Unlimited - Due to: Unlimited
| 1.8k views
0 like 0 dislike
1 answer
[Exercise] Coding (C) - asked Apr 27, 2017 in C++
ID: 24390 - Available when: Unlimited - Due to: Unlimited
| 381 views
12,783 questions
183,443 answers
172,219 comments
4,824 users