C ++ study Note 5

Source: Internet
Author: User

 

Add some functions to the simple graphics system: Perimeter (), area () and volume () to calculate its perimeter, area and volume (only for threedimentionalshape) respectively; a function draw () to draw a shape with different colors, thickness and styles.

A simple graphic system can be created to provide the following functions:

? Perimeter (): calculates the graph perimeter;
? Area (): calculates the image area;
? Volume (): calculates the image volume (only for 3D images );
? Draw (): draws a graph.
Note:
1. Use of polymorphism;
2. Only the feature information can be output when drawing a graph, without the need to draw a specific graph.

 

// BookProgramDebug through vcsp6

# Include <iostream>
# Include <math. h>
Using namespace STD;
Const double Pi = 3.1415926;

class shape // define the shape abstract class
{< br>
Public:
virtual float perimeter () = 0;
virtual float volume () = 0; // define the volume function
virtual float area () = 0; // define the area function
virtual void disp () = 0; // define the output function
virtual void draw () = 0;
};

Class rectangle: Public shape // defines the rectangular derived class
{
Public:
Void setvalue (int m, int N)
{
X = m;
Y = N;
}
Float area () // The definition version of the virtual function in the rectangle class
{
Sr = x * Y;
Return 1;
}
Float perimeter ()
{
PR = 2 * (x + y );
Return 1;
}
Float volume ()
{
Return 1;
}
Void disp ()
{
Cout <"rectangular area =" <Sr <Endl;
Cout <"rectangular perimeter =" <Pr <Endl;
}
Void draw ()
{
Cout <"draw a rectangle." <Endl;
}

PRIVATE:
Double SR;
Double X, Y;
Double PR;
};

Class circle: Public shape // defines the derived class of the circle
{
Public:
Void setvalue (INT m)
{
X = m;
}
Float perimeter ()
{
PC = 2 * pI * X;
Return 1;

}
Float volume ()
{
Return 1;
}
Float area () // The definition version of the virtual function in the circle class
{
SC = pI * x * X;
Return 1;
}
Void disp ()
{
Cout <"Area of the Circle =" <SC <Endl;
Cout <"circle perimeter =" <Pc <Endl;
}
Void draw ()
{
Cout <"draw a circle." <Endl;
}
PRIVATE:
Double SC;
Double X;
Double PC;

};

Class sphere: Public shape // defines the derived class of the ball
{
Public:
Void setvalue (INT m)
{
X = m;
}
Float volume ()
{
Vs = 4/3 * pI * x * X;
Return 1;
}
Float perimeter ()
{
Return 1;
}
Float area ()
{
As = 4 * pI * x * X;
Return 1;
}
Void disp ()
{
Cout <"ball volume =" <vs <Endl;
Cout <"Ball Surface Area =" <as <Endl;

}
Void draw ()
{
Cout <"draw a sphere." <Endl;
}
PRIVATE:
Double;
Double X;
Double;
};

Class cuboid: Public shape // defines the cube class
{
Public:
Void setvalue (int l, int W, int H)
{
X = L;
Y = W;
Z = h;
}
Float volume ()
{
VC = x * y * z;
Return 1;
}
Float area ()
{
AC = 2 * (x * Y + x * z + y * z );
Return 1;
}
Void disp ()
{
Cout <"cube volume =" <VC <Endl;
Cout <"cube surface area =" <AC <Endl;
}
Float perimeter ()
{
Return 1;
}
Void draw ()
{
Cout <"draw a cuboid." <Endl;
}

PRIVATE:
Double X, Y, Z;
Double VC, AC;
};

Int main ()
{
Rectangle R; // defines a rectangle class object.
Circle C; // defines a circle object.
Sphere S; // defines the ball object
Cuboid Cu; // defines the rectangular object
Int x, y, z;
Int choice; // used to receive the selected option number

Do {// loop selection Option
Cout <"1. rectangular area and perimeter" <Endl; // display the selection menu
Cout <"2. Calculate the area and perimeter of the circle" <Endl ;;
Cout <"3. Ball volume and surface area" <Endl ;;
Cout <"4. estimate the volume and surface area of the cube" <Endl ;;
Cout <"0. Exit" <Endl ;;
Cout <"select function (0-4 ):";
Cin> choice; // enter the option number.
If (choice = 1)
{Cout <"Enter the length and width of the rectangle :";
Cin> x> Y;
R. setvalue (x, y );
R. Area ();
R. Perimeter ();
R. disp ();
R. Draw ();
}

If (choice = 2)
{Cout <"enter the circle radius :";
Cin> X;
C. setvalue (X );
C. Area ();
C. Perimeter ();
C. disp ();
C. Draw ();

}
If (choice = 3)
{
Cout <"Enter the ball radius :";
Cin> X;
S. setvalue (X );
S. Volume ();
S. Area ();
S. disp ();
S. Draw ();

}
If (choice = 4)
{
Cout <"Enter the length, width, and height of the cube (in order ):";
Cin> x> Y> Z;
CU. setvalue (x, y, z );
CU. Volume ();
CU. Area ();
CU. disp ();
CU. Draw ();
}
Cout <Endl;

} While (choice! = 0 );

Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.