Exercise 3: Creating the cone class

In this exercise, you create the cone class by using the visual editor. The cone class represents a 3-dimensional cone.

In the previous exercise, you used the UML modeling tools to extend the circle and sphere classes. In this exercise, you use the UML modeling tools to add the cone class to the project. The cone class, which inherits from the shape3d class, calculates and displays the volume and surface area based on the radius and height of the cone. You can use the UML modeling tools to add a class to the project, and to add an attribute to a class. To edit the body of the method, double-click the method in the diagram and edit the source code in the code editor.

Adding the cone class to the Shapes project

You can add the class to the project by using the C/C++ Project Explorer view. You can identify any inheritance relationships by using the New C++/Class wizard.

To add the cone class to the Shapes project:
  1. In the C/C++ Project Explorer view, right-click the Shapes project; then click New > Class.
  2. In the New C++ Class window, in the Name field, type cone, and click Browse.
  3. In the Choose Base Class window, from the matching types list, double-click shapes3d.
  4. In the C/C++ Project Explorer view, expand cone.h, click the cone class element, and drag it into the diagram editor.

You have now added the cone class to the Shapes project. You can now use the UML modeling tools to add classes and attributes to the new class. Your diagram should look similar to the following figure:

The shapes project contains the new cone class.

Adding the getColor and setColor methods to the cone class

The cone class implements the getColor and setColor methods that the base shape class defines.

To add the getColor and setColor methods to the cone class:
  1. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Field.
  2. In the Create C++ Field window, in the Name field, type color.
  3. In the Type list, type string and click Finish.
  4. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  5. In the Create C++ Method window, in the Name field, type getColor.
  6. In the Return type list, type string, and click Finish.
  7. In the cone class, double-click the getColor method and in the code editor, specify the body of the getColor method as follows:{return color;};
  8. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  9. In the Create C++ Method window, in the Name field, type setColor, and click Finish.
  10. In the cone class, double-click the setColor method, and in the code editor, specify the signature of the method as:void setColor(string c) and the body of the setColor method as {color = c;};

You have now added the getColor and setColor methods to the cone class.

Adding the getSize and setSize methods to the cone class

The cone class implements the getSize and setSize methods that the base shape class defines. The size field stores the height of the cone.

To add the getSize and setSize methods to the cone class:
  1. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Field.
  2. In the Create C++ Field window, in the Name field, type size.
  3. From the Type list, select double and click Finish.
  4. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  5. In the Create C++ Method window, in the Name field, type getSize.
  6. In the Return type list, click the down arrow beside void, select double, and click Finish.
  7. In the cone class, double-click the getSize method, and in the code editor, specify the body of the getSize method as follows: {return size;};
  8. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  9. In the Create C++ Method window, in the Name field, type setSize, and click Finish.
  10. In the cone class, double-click the setSize method and, in the code editor, specify the body of the setSize method as follows: {size = s < 0 ? 0:S;};

You have just added the getSize and setSize methods to the cone class.

Adding the getRadius and setRadius methods to the cone class

The cone class implements the getRadius and setRadius methods the base shape class defines. The radius field stores the radius of the base of the cone. The application uses the radius to calculate the circumference and the volume of the cone.

To add the getRadius and setRadius methods:
  1. In the diagram editor, right-click the cone class; then click Add C/C++ > Field.
  2. In the Create C++ Field window, in the Name field, type radius.
  3. In the Type list, click double, and click Finish.
  4. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  5. In the Create C++ Method window, in the Name field, type getRadius.
  6. From the Return type list, select double and click Finish.
  7. In the cone class, double-click the getRadius method, and in the code editor, specify the body of the getRadius method as follows: {return radius;};
  8. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  9. In the Create C++ Method window, in the Name field, type setRadius and click Finish.
  10. In the cone class, double-click the setRadius method and, in the code editor, specify the body of the setRadius method as follows:{radius = r < 0 ? 0:r;};

You have now added the getRadius and setRadius methods to the cone class.

Adding the surfaceArea and volume methods to the cone class

The cone class implements the surface area and volume methods that the shapes3d class defines. The formula used to calculate the surfaceArea of a cone is pi * r * (r + (r2 + h2)1/2). The formula to calculate the volume of a cone is 1/3 x pi x r2 h.

To add the surfaceArea and volume methods to the cone class:
  1. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  2. In the Create C++ Method window, in the Name field, type surfaceArea.
  3. From the Return type list, select double and click Finish.
  4. In the cone class, double-click the surfaceArea method and, in the code editor, specify the body of the surfaceArea method as follows:{return pi * radius * (radius + (pow(pow(radius,2) + pow(size,2),0.5)));};
  5. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  6. In the Create C++ Method window, in the Name field, type volume and click Finish.
  7. In the cone class, double-click the volume method, and in the code editor, specify the body of the volume method as follows: {return 0.333 * pi * (pow(radius,2) * size);};

You have now added the surfaceArea and volume methods to the cone class.

Adding the set and print methods to the cone class

The cone class implements the set and print methods that the base shape class defines.

To add the set and print methods to the cone class:
  1. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  2. In the Create C++ Method window, in the Name field, type print and click Finish.
  3. In the cone class, double-click the print method and, in the code editor, specify the body of the print method as follows:
    { cout  << "Cone:"
    			<< "\n\tLength = " << getSize()
    			<< "\n\tArea   = " << surfaceArea()
    			<< "\n\tVolume = " << volume()
    			<< "\n\tColor  = " << getColor()
    			<< "\n\n";
    };
  4. In the diagram editor, in the classdiagram.dnx diagram, right-click the cone class; then click Add C/C++ > Method.
  5. In the Create C++ Method window, in the Name field, type set and click Finish.

You have now added the print and set methods to the cone class.

Editing the cone.cpp file

The cone.cpp class file contains the implementation of the set method, as well as the constructor and destructor. You modify the body of the set method to prompt the user to enter the size and radius of the cone. You must also edit the default constructor to set the initial values of the new cone class.

To edit the cone.cpp file:
  1. In the C++ Project Explorer view, double-click the cone.cpp class.
  2. In the code editor, in the cone.cpp class, specify the body of the default constructor as follows: {color="Transparent"; radius=0; size=0;}
  3. In the method declaration section, specify the body of the set method as follows:
    void cone::set()	{
    	double size;
    	double radius;
    	string color;
    	
    	cout << "Enter the height of the cone: ";
    	cin >> size; 
    	setSize(size);
    	
    	cout << "Enter the radius of the base of the cone: ";
    	cin >> radius; 
    	setRadius(radius);
    	
    	cout << "Enter the color of the cone: ";
    	cin >> color;
    	setColor(color);
    }
  4. To save the project, click File > Save.

You have now completed the Shapes project by adding the set method to the cone.cpp class file.

Running the extended application

Before you can run the application, you must add the include statement into the main.cpp class to include the new cone class. You can run the application and instantiate the new cone class by modifying the main.cpp class.

To run the application:
  1. In the C++ Project Explorer view, double-click the main.cpp class.
  2. In the declaration section of the class, below #include "tetrahedron.h", add the following include statement: #include "cone.h"
  3. In the code editor, in the main body of the class, add the following code:
    //instantiate and run the cone class 
    		cone c;
    		c.print();
    		c.set();
    		c.print();
  4. To save and build the project, click File > Save.
  5. Click Run.

The program displays the size, color, surface area, and volume of the cone shape and prompts you to specify values for the new cone instance.