8 like 0 dislike

Please remember our Midterm 2 (2016/05/11): Assignment 3.

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

 

asked in Polymorphism by (11.8k points)
reshown by | 55 views
0 0
Called for Help
0 0
Called for Help
0 1
Called for Help
0 0
Called for Help
0 0
Called for Help

33 Answers

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

using namespace std;

// Base class

class Shape

{

public:
*** * * ** ***** pure virtual function * * ** * *
***** * * *** * * * int getArea() = 0;
** *** * ** * **** * * * * w)
* ** *** * *
* * ******* * ** ** * * * * * * *** * = w;
* *** * * *
* * *** * *** * **** h)
*** *** * * **
** ** * * * * *** ***** * * **** = h;
* ** * * * ** *****
**** * **
** * ******* * width;
** ** ****** ** *** height;

};

// Derived classes

class **** public Shape

{

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

* **** * *** ** *** ** ****** ** * (width * height);

**** ** ** **** *

};

class Triangle: public Shape

{

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

** *** * * * * * * ** * * *** * * (width * ***

* **** ** * ** * *

};

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 * * * * * * **

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



void *** * ** h){
* * ** * * *** * **** * = h;
* * * and ** *** w) and ** ** * h) ** ** here
* * *
*** *** ** * *** **** * * width;
**** **** * *** * * height;

};

// Derived classes

class public Shape

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

};

class * ** public Shape

{
* **
** * * * *** * ***
**** * **** *** * * ** ** * ***
** * * ***** // Your code: * * *** * * * **

};

int ** * *

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

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

using * std;

// Base class

class Shape

{

public:
** ** * * ** * Your code: pure virtual function *** ****** * * **

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




void **** * h){
** ** * * ** * * = h;
* ******* ** * and * * w) and ** *** h) ** here
****
* * ** * width;
* * * ** * ** * * height;

};

// Derived classes

class * public Shape

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

};

class ** public Shape

{
**
* * * *** * * *** *
* **** ** * ** * * * ** ** * * * *
*** *** ** * * // Your code: * * * *** ** *

};

int ******

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

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

using *** std;

// Base class

class Shape

{

public:
*** ** * * * ** ** * Your code: pure virtual function * ** ** ** * * *****

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



void * *** h){
*** ****** ** * * * * = h;
** * * * * * * and ** * w) and * ****** h) * ** * here
* *
* ** ** ** * ** * width;
* * *** * ** ** * * * height;

};

// Derived classes

class ** * public Shape

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

};

class **** public Shape

{
*****
* **** ** **
* **** ** *** ******* * ** **
* *** * // Your code: * * * **** ** *** * ***

};

int *

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

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

using namespace std;

// Base class

class Shape

{

public:
** **** * * * * * pure virtual function *** * ** ***
** * * * * * * * int getArea() = 0;
********** * **** * * ** * w)
* * ** ** * * ** ****
* ** * ** * * ** * *** *** * = w;
* ***** ** ** *** *
*** ***** ** ** * *** * h)
* * ** ** * *
* * * **** * ** ** ***** ****** * = h;
* ** ** * * **
** *
* *** * * * * width;
***** ** * ** ** * height;

};

// Derived classes

class ** * public Shape

{

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

***** ** * ** **** * * *** * * ** * (width * height);

** * ** ** **** * * *

};

class Triangle: public Shape

{

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

** * ** **** *** ** * * ** (width * ** **

** * ** ** *

};

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:
* ** * * * * * * pure virtual function *** ** * *
* * ** ***** ** int getArea() = 0;
* * *** * * **** * ** ** * w)
* * * *** ***
*** * ** * * * * *** **** *** * = w;
* * ** * * *
** * ** ** * * *** * * ** * h)
* ** *** **
** * * * ****** ** ** *** **** = h;
* * ** ** *
* * *
* * ***** * * * * width;
** * * * *** * * height;

};

// Derived classes

class ** ** public Shape

{

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

**** * * * * **** * * * * * ** **** (width * height);

* ** * * **

};

class Triangle: public Shape

{

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

** *** * * * * * * * ** * ** * (width * * ***** *

** *** ** * * *

};

int *** * **

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

* * * * 0;

}
answered by (10.8k points)
0 like 0 dislike
Hidden content!
using namespace std;

// Base class

class Shape

{

public:
*** ** **** ** *** pure virtual function providing interface *
** * * * ** ** int getArea() = 0;
* * * *** * ** * * ** w)
** * ******* * ** *
* * **** ***** * * * * ** ** * * * * = w;
*** * * * * ***
* ** * * * **** *** * * * * h)
* * * * ** *
******* * * ** * * * * * *** = h;
* ***** ** * * **
* *
* ** * **** **** width;
** ** *** *** height;

};

// Derived classes

class * ** public Shape

{

public:
* * * * ** * * ***** *
*** * ** ***** *

* ***** * ** * * ** **** *** **** (width * height);

** * * * * ***** ***

};

class Triangle: public Shape

{

public:
** * *** * * * **
* *** * * *** *

* ** * *** ****** ** *** ** (width * *** *

* ** *** ** ** * * *

};

int **

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

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

}
answered by (10.8k points)
0 like 0 dislike
Hidden content!
#include * * * * * **** *

using namespace std;

class Shape

{

public:
* * ** * * * ** * ** : width(), height() {}
* * * * * * *** * * * * *** int w)
** ******** * *****
* ** * ** * * ** *** * *** * * * ** ** = w;
** ** * * ** * *****
*** ** * ** ** ** * ** **** int h)
**** ** * * * *** *
* ****** * ** ** ** * * * * * ***** *** * *** ** ** = h;
* * *** * * *

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

};

class Rectangle : public Shape

{

public:


* * * ** ** ** **** getArea()
* *** ** * ** * ** * *
** *** * ** **** ** *** * *** * *** * **** width * height;
** * ** **** ** ** *** *

};



class Triangle : public Shape

{

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

};

int main(void)

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

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

using ** * * std;

// Base class

class Shape

{
****
***** * ***** * int * **** // Your code: pure **** ** * * ** * ** **



void *** *** ***

void * * * * * ** *** and * * * w) and **** h) ** here
**
* * * * ** * * *** width;
* ** * ** * *

};

// Derived classes

class *** *** public Shape

{
** *
* *** ***** ** * ** ** * * // Your code: * * * ** * * ** *

};

class *** * public Shape

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

};

int * * *

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

}
answered by (10 points)
0 like 1 dislike
Hidden content!
#include *** * *



using namespace std;



class Shape

{

public:
* *** * * * ** * *** * **
** ** ** * * * ****** int getArea() = 0;
* ******** ** * * * ***
*** * * * *** * * * * a = 0, int b = 0)
* ** ** * * *** *** * *
***** * * **** ** * ****** ******* * *** ** * = a;
*** * * * ** ** **** * ** * * * ** * ** = b;
** ** * * *** ** *
**** ** ** * * * setWidth(int w)
* * * ** ** * * *** *
* * ***** ******** *** * * * ******* = w;
* ** ** * * * ******** **
* * * ***** * *
* * ** * ** * *** ** setHeight(int h)
* * ** * ** ** ** * ****
** * * *** * ** * * *** * *** *** *** * *** = h;
* * ** *** ** * * ***
* * ***** * ** * **
* * ** * *

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

};





class Rectangle: public Shape

{



public:
** * *** * * * * ** *
** * **** * * ** ** * ** ****** *
* * ****** **** ***
** ******** * * * ****** int getArea()
* * * ** **** * * ** *
* * * * * * ** ** * ** ** * * ** ** ******* * * * (width * height);
* ** ** * * *

};



class Triangle: public Shape

{

public:
* **** * * * * * *
** *** * ** **** * * * *
** * **** * **** ** ** * int getArea()
** * * * * * * * *
* *** ** * * **** * ** *** * * * ** ((width * height)/2);
* *** ****** * ***

};



int main(void)

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

}
answered by (-605 points)
Get it on Google Play Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.158.79.137
©2016-2018

Related questions

8 like 0 dislike
35 answers
8 like 0 dislike
35 answers
2,974 questions
57,433 answers
29,089 comments
938 users