In this exercise, you use the C++ visual development tools and the class diagram that you created in the first exercise to add an operation to the circle and sphere classes.
In the previous exercise, you used the C++ visual development tools to view the hierarchy of the C++ Shapes project. You can also use the C++ visual development tools to add classes to a project, or to add properties and methods to a class. In this exercise, you add the getCircumference method to the circle and sphere classes. The getCircumference method calculates the circumference of the shape and circle by using the radius.
Add the getCircumference method to the circle class
The formula that calculates the circumference of a circle is πr², where r is the radius of the circle. The global constant π is stored in the base shape class.
To add the getCircumference method to the circle class:
The formula that calculates the circumference of a sphere at the largest diameter is the same as the formula that you used in the previous step.
To add the getCircumference method to the sphere class:
You can run the application by modifying the main.cpp class. The main.cpp class is the driver for the C++ Shapes application.
To run the Shapes application:
You can now run the application. When you run the C++ Shapes application, it runs in the Console view and displays the following output:
Enter the radius of the sphere: 10 Enter the color of the sphere: Blue Sphere: Radius = 10 Circumference = 62.8319 Area = 1256.64 Volume = 4188.79 Color = Blue
The program displays the size and color of the current shape and prompts you to specify values for the new shape. The attributes of the new shape are displayed in the Console view. You can modify the code in main.cpp to run the circle class.