Question:
Write a program that defines the abstract base class shape and derives five Derived classes: cricle (circle, square (square, rectangle (rectangle), trapezoid (trapezoid ), triangle (triangle ). use virtual functions to calculate the area of several images and calculate their sum. A base class pointer array is required to point each element of the base class to a derived class object.
Hope my friends can help me improve the program !!~
Program:
# Include <iostream>
# Include <math. h>
Using namespace STD;
Class shape/* abstract base class */
{
Public:
Virtual float area () const {return 0 ;}
Virtual void shapename () const = 0;
Virtual void show () const = 0;
};
Class circle: Public shape/* derived class circle */
{
PRIVATE: Float R;
Float X, Y;
Public: Circle () {r = 0; X = 0; y = 0 ;}
Circle (float a, float B, float c) {r = A; X = B; y = C ;}
Float area () const
{
Return (3.14 * r * R );
}
Void shapename () const
{
Cout <"shapename: Circle" <Endl;
}
Void show () const/* output circle information */
{
Shapename ();
Cout <"R:" <r <Endl;
Cout <"Area:" <area () <Endl;
}
};
Class square: public circle/* Square of the derived class */
{
PRIVATE:
Float;
Public:
Square () {A = 0 ;}
Square (float B) {A = B ;}
Float area () const
{
Return (A * );
}
Void shapename () const
{
Cout <"shapename: Square" <Endl;
}
Void show () const/* output square information */
{
Shapename ();
Cout <"A:" <A <Endl;
Cout <"Area:" <area () <Endl;
}
};
Class rectangle: Public Square/* rectangle of the derived class */
{
PRIVATE:
Float a, B;
Public:
Rectangle () {A = 0; B = 0 ;}
Rectangle (float C, float d) {A = C; B = D ;}
Float area () const
{
Return (A * B );
}
Void shapename () const
{
Cout <"shapename: rectangle" <Endl;
}
Void show () const/* output giant information */
{
Shapename ();
Cout <"A:" <A <Endl;
Cout <"B:" <B <Endl;
Cout <"Area:" <area () <Endl;
}
};
Class trapezoid: Public shape/* derived class trapezoid */
{
PRIVATE:
Float a, B, h;
Public:
Trapezoid () {A = 0; B = 0; h = 0 ;}
Trapezoid (float C, float D, float e) {A = C; B = D; H = E ;}
Float area () const
{
Return (a + B) * h)/2;
}
Void shapename () const
{
Cout <"shapename: Trapezoid" <Endl;
}
Void show () const/* output trapezoid information */
{
Shapename ();
Cout <"A:" <A <Endl;
Cout <"B:" <B <Endl;
Cout <"H:" <H <Endl;
Cout <"Area:" <area () <Endl;
}
};
Class triangle: Public shape/* triangle of the derived class */
{
PRIVATE:
Float a, B, c;
Public:
Triangle () {A = 0; B = 0; C = 0 ;}
Triangle (float D, float E, float f) {A = D; B = E; C = f ;}
Float area () const/* calculate the Triangle Area */
{
Float S;
Float S = (A + B + C)/2;
S = SQRT (S * (S-A) * (S-B) * (s-c ));
Return S;
}
Void shapename () const
{
Cout <"shapename: Triangle" <Endl;
}
Void show () const/* length and area of the three sides of the output triangle */
{
Shapename ();
Cout <"A:" <A <Endl;
Cout <"B:" <B <Endl;
Cout <"C:" <C <Endl;
Cout <"Area:" <area () <Endl;
}
Int istriangle ();/* determine whether it is a triangle */
};
Int triangle: istriangle ()
{
If (a + B)> C & (B + C)> A & (A + C)> B)/* use the sum of the two sides to determine if the sum is greater than the third side */
{
Cout <"A =" <A <"b2 =" <B <"c =" <C <". "<Endl;/* output side length */
Return 1;
}
Else
{
Cout <"the length of the input side does not meet the triangle condition" <Endl;/* incorrect side length input */
Return 0;
}
}
Int main ()
{Int I;
Shape * P [5];
For (I = 0; I <5; I ++)
P [I] = NULL;
Float a, B, c;
Do {
Cout <"/T ******************************** * ***** "<Endl;
Cout <"/T select the menu" <Endl;
Cout <"/T 1. Calculate the circular area" <Endl;
Cout <"/T 2. Positive Square area" <Endl;
Cout <"/T 3. rectangular area" <Endl;
Cout <"/T 4. trapezoid area" <Endl;
Cout <"/T 5. Triangle Area" <Endl;
Cout <"/T 6. Calculate the sum of all shapes and graphics areas." <Endl;
Cout <"/T 0. Exit" <Endl;
Cout <"/T ******************************** * ***** "<Endl;
Cin> I;
Switch (I)
{
Case 1 :{
Cout <"enter the circle radius R, X, and Y :";
Cin> A> B> C;
Circle C (A, B, C );
P [0] = & C;
P [0]-> show ();
} Break;
Case 2 :{
Cout <"Enter the side length of a square :";
Cin> B;
Square S (B );
P [1] = & S;
P [1]-> show ();
} Break;
Case 3 :{
Cout <"Enter the length and width of the rectangle A, B :";
Cin> A> B;
Rectangle R (A, B );
P [2] = & R;
P [2]-> show ();
} Break;
Case 4 :{
Cout <"Enter the upper bottom, lower bottom, high a, B, H :";
Cin> A> B> C;
Trapezoid tra (A, B, C );
P [3] = & TRA;
P [3]-> show ();
} Break;
Case 5 :{
Cout <"Enter the three sides of A, B, C :";
Cin> A> B> C;
Triangle tri (A, B, C );
P [4] = & tri;
If (TRI. istriangle ()/* determines whether the conditions on the three sides of the triangle are met */
P [4]-> show ();
} Break;
Case 6: {float K = 0;
For (I = 0; I <5; I ++)
{If (P [I] = NULL)
K = K + 0;
Else K = K + P [I]-> area ();}
Cout <"Total area:" <k <Endl;
}
Break;
}
} While (I! = 0 );
Return 0;
}