0

我正在为我的程序逻辑和设计类编写一个 C++ 程序。我是初学者。我们必须编写一个程序,使用超类(Shape)和子类(Circle、Rectangle 和 Triangle)来计算形状的面积。返回给我的值始终是 3.47668e-310,无论我为半径、长度、宽度、底边、高度设置什么值。谁能帮我解决我做错了什么?

首先,我的超类如下所示:

#include <iostream>

using namespace std;

#ifndef SHAPE

#define SHAPE

class Shape
{
    private:              
    // Field declaration to hold calculated area from subclass
    double shapeArea;

    public: 
       // Member functions
       // Mutator
       void setArea(double anArea)
       {
          shapeArea = anArea;
       }

       // Accessor
       double getArea()
       {
          return shapeArea;       
       }
}; // End Shape class
#endif

圆圈子类如下所示:

#include <iostream>
#include <math.h>

using namespace std;

const double PI = 3.14159;

// Function prototype
void calcArea(double &);

class Circle : public Shape
{
    private:
       // Fields
       double radius;
       double area;

       void calcArea(double &radius)
       {
          area = pi * pow(radius,2);
       }

    public:
       // Constructor sets the area
       Circle(double aRadius)
       {         
         // Call calcArea, passing aRadius
         calcArea(aRadius);

         //Local variable for area
         double anArea;
         area = anArea;

         //Call inherited setArea function to set the area
         setArea(anArea);
       }                           
}; // End Circle class

我的主要是这样的:

#include <iostream>

#include "ShapeClass.cpp"
#include "CircleClass.cpp"
#include "RectangleClass.cpp"
#include "TriangleClass.cpp"

using namespace std;

//Function prototype
void displayMenu(int &);

// Global variables
const int EXIT_CHOICE = 4;
const int MIN_CHOICE = 1; // Used to compare if selection is less than 1
                          // for displayMenu method

int main()
{
      // Declare selection variable
      int selection;

      // Declare variable to hold area
      double shapeArea;

      //Declare variables to hold user input
      double circleRadius, rectangleLength, rectangleWidth, triangleBase, triangleHeight;

      //Create objects from classes: Circle, Rectangle, Triangle
      Circle areaCircle(circleRadius);
      Rectangle areaRectangle(rectangleLength, rectangleWidth);
      Triangle areaTriangle(triangleBase, triangleHeight);

      // Get selection from user and verify they did not enter
      // option to end the program
      do
      {
        // Display menu
        displayMenu(selection);

        // Based on user selection, prompt user for required 
        // measurements of shape and return the area
        switch (selection)
        {
            case 1:
               // Prompt user for radius of the circle  
               cout << "Enter radius of circle." << endl;
               cin >> circleRadius;

               cout << "The area of the circle is " 
                    << areaCircle.getArea() << endl << endl;
               break;
            case 2:
               // Prompt user for the length of the rectangle
               cout << "Enter the length of the rectangle." << endl;
               cin >> rectangleLength;

               // Prompt user for the width of the rectangle
               cout << "Enter the width of the rectangle." << endl;
               cin >> rectangleWidth;

               cout << "The area of the rectangle is " 
                    << areaRectangle.getArea();
               cout << endl << endl;
               break;
            case 3:
               // Prompt user for the length of the base of the triangle
               cout << "Enter the length of the base of the triangle." << endl;
               cin >> triangleBase;

               // Prompt user for the height of the triangle
               cout << "Enter the height of the triangle." << endl;
               cin >> triangleHeight;

               cout << "The area of the triangle is " 
               << areaTriangle.getArea() << endl << endl;
               break;
            case 4:
               cout << "Goodbye!" << endl;
               cout << endl << endl;
               break; 
        }
      } while (selection != EXIT_CHOICE);

      system("Pause");
      return 0;
}// End main()

void displayMenu(int &select)
{
    // Prompt user for shape for which they want
    // the area calculated  
    cout << "Geometry Calculator" << endl;
    cout << "1. Calculate the area of a Circle." << endl;
    cout << "2. Calculate the area of a Rectangle." << endl;
    cout << "3. Calculate the area of a Triangle." << endl;
    cout << "4. End the program." << endl;
    cout << "Enter your selection: ";
    cin  >> select;

    // Validate user selection
    while (select < MIN_CHOICE or select > EXIT_CHOICE)
    {
        cout << "That is an invalid selection." << endl;
        cout << "Enter 1, 2, 3, or 4 to end the program." << endl;
        cin >> select;
    }                   
}// End displayMenu()      

我希望这不是太多。谢谢!

4

2 回答 2

0

你用垃圾覆盖区域。

双面积;面积=一个面积;

摆脱这些线条并将区域传递给 setArea。

于 2013-07-29T01:00:21.187 回答
0

在方法calcArea()中,成员area已被分配。您需要删除这些行

     double anArea;
     area = anArea;

此外,当您调用继承setArea()的和getArea()方法时,基类private成员shapeArea被修改但shapeArea派生类无法访问,因为public inheritance.

为了使shapeArea派生类实例可以访问,您需要将其声明为protected并删除派生类中的冗余成员area并修改相关方法calcArea()

   void calcArea(double &radius)
   {
      shapeArea = pi * pow(radius,2);
   }
于 2013-07-29T05:18:18.947 回答