Exercise 2: Extending the circle and sphere classes

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.

Adding the getCircumference method to the circle class

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.

To add the getCircumference method to the circle class:
  1. In the diagram editor, in the classdiagram.dnx diagram, right-click the circle class; then click Add C/C++ > Method.
  2. In the Create C++ Method window, in the Name field, type getCircumference.
  3. From the Return type list, select double and click Finish.
  4. In the circle class, double-click the getCircumference method and in the code editor, specify the body of the getCircumference method as follows:
    {return pi * (2 * getSize());};
  5. In the code editor, add the following line of code to the print method:
    << "\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.

Adding the getCircumference method to the sphere 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:
  1. In the diagram editor, in the classdiagram.dnx diagram, right-click the sphere class and click Add C/C++ > Method.
  2. In the Create C++ Method window, in the Name field, type getCircumference.
  3. From the Return type list, select double and click Finish.
  4. In the sphere class, double-click the getCircumference method and, in the code editor, specify the body of the getCircumference method as follows:
    {return pi * (2 * getSize());};
  5. In the code editor, add the following line of code to the print method:
    << "\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.

Running the Shapes application

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:
  1. In the C/C++ Project Explorer view, double-click the main.cpp class.
  2. In the code editor, in the main body of the program, add the following code:
    //instantiate and run the sphere class
    sphere sp;
    sp.print();
    sp.set();
    sp.print();
  3. To save and build the project, click File > Save.
  4. Click Run > Run.
  5. In the Run window, from the Configurations list, double-click C/C++ Local.
  6. In the Project field, type Shapes.
  7. In the C/C++ Application field, click Browse and select the Shapes.exe executable file in the Shapes\debug directory.
  8. Click Run.

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.