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.
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.
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 cone class implements the getColor and setColor methods that the base shape class defines.
You have now added the getColor and setColor 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.
You have just added the getSize and setSize 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.
You have now added the getRadius and setRadius 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.
You have now added the surfaceArea and volume methods to the cone class.
The cone class implements the set and print methods that the base shape class defines.
{ cout << "Cone:" << "\n\tLength = " << getSize() << "\n\tArea = " << surfaceArea() << "\n\tVolume = " << volume() << "\n\tColor = " << getColor() << "\n\n"; };
You have now added the print and set methods to the cone class.
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.
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); }
You have now completed the Shapes project by adding the set method to the cone.cpp class file.
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.
//instantiate and run the cone class cone c; c.print(); c.set(); c.print();
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.