In this exercise, you use C++ visualizer 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++ visualizer to view the hierarchy of the C++ Shapes project. You can also use the C++ visualizer 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.
The formula that calculates the circumference of a circle is pi x r x r, where r is the radius of the circle. The global constant pi is stored in the base shape class.
{return pi * (2 * getSize());};
<< "\n\tCircumference = " << getCircumference()
You have now added the getCircumference method to the circle class. The getCircumference method uses the size variable from the getSize method and the global constant pi to calculate the circumference. You also modified the print method to print the output of the getCircumference method.
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.
{return pi * (2 * getSize());};
<< "\n\tCircumference = " << getCircumference()
You have now used the UML visual development tools and the code editor to add the getCircumference method to both the circle and sphere classes.
You can run the application by modifying the main.cpp class. The main.cpp class is the driver for the C++ shapes application.
//instantiate and run the sphere class sphere sp; sp.print(); sp.set(); sp.print();
The shapes program 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.
To continue, proceed to Exercise 3: Creating the cone class.