Home > Back-end >  The c subject
The c subject

Time:09-25

Write a program to define the type of abstract Shape, derived by him three classes: Circle (round), a Rectangle (rectangular), Trapezoid (Trapezoid), with a function printArea respectively output the area of the three, three graphics data in the definition of object is given,

CodePudding user response:

Pleasure, pleasure
 class Shape 
{
Public:
Virtual void printArea ()=0;
};

The class Circle: public Shape
{
Public:
Circle (radius, float) : _radius (radius) {}
~ Circle () {}

Virtual void printArea () {cout & lt; <3.1415 * (_radius ^ 2) & lt;
Private:
Float _radius;
};

Class a Rectangle: public Shape
{
Public:
A Rectangle (float length, float width) : _length (length), _width (width) {}
~ a Rectangle () {}

Virtual void printArea () {cout & lt; <_length * _width & lt;
Private:
Float _length;
Float _width;
};

The class Trapezoid: public Shape
{
Public:
Trapezoid (float float top, bottom, float height) : _top (top), _bottom (bottom), _height (height) {}
~ Trapezoid () {}

Virtual void printArea () {cout & lt; <(_top + _bottom) * _height/2 & lt;
Private:
Float _top;
Float _bottom;
Float _height;
};
  • Related