使用C ++中的继承的正方形,圆形和矩形的区域

时间:2017-09-09 18:16:46

标签: c++

我的C ++程序存在问题。我需要找到正方形,圆形和矩形的区域。我把所有东西都用圆形和方形向下,但是矩形和形状(继承结构)给了我上面提到的问题。我一直在撞墙试图解决这个问题,所以如果有人能帮助我,我会非常感激。我的代码是:

main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Rectangle.h"

using namespace std;

int main()
{
double radius = 0;
double length = 0;
double width = 0;
Square square;
Circle circle;
Rectangle rectangle;
int option;

cout << "Calculating the Area" << endl << endl;

do
{
cout << "Pick a shape in which you would like the area of:" << endl;
cout << "1: Square" << endl;
cout << "2: Circle" << endl;
cout << "3: Rectangle" << endl;
cout << "4: Exit" << endl;
cout << "Please enter your choice: ";
cin >> option;

switch(option)
{
case 1:
    {
        cout << endl;
        cout << "Please enter the length of one side of the square: " << endl;
        cin >> length;
        Square square(length);
        cout << "The area of the square is: " << square.getArea() << "\n\n";
        break;
    }
case 2:
    {
        cout << endl;
        cout << "Please enter the radius of the circle: ";
        cin >> radius;
        circle.setRadius(radius);
        cout <<  "The area of the circle is: " << circle.getArea() << "\n\n";
        break;
    }
case 3:
    {
        cout << endl;
        cout << "Please enter the length of one side of the rectangle: ";
        cin >> length;
        rectangle.setLength(length);
        cout << "Please enter the width of one side of the rectangle: ";
        cin >> width;
        rectangle.setWidth(width);
        cout << "The area of the rectangle is: " << rectangle.getArea();
    }
}
}
while (option != 4);
    cout << "Bye!" << endl;

}

shape.h

#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED

class Shape {
public:
    double getArea();
};

#endif // SHAPE_H_INCLUDED

shape.cpp

#include "shape.h"

Shape::Shape() {
    area = 0;
}

double Shape::getArea() {
    return area;
}

rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include <iostream>
#include "shape.h"

class Rectangle : public Shape
{
public:
Rectangle (double length = 0, double width = 0);
double getLength = 0;
double getWidth = 0;
void setLength(double length);
void setWidth(double width);
double getArea();
private:
double length;
double width;
};


#endif // RECTANGLE_H_INCLUDED

rectangle.cpp

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include <iostream>
#include "shape.h"

class Rectangle : public Shape
{
public:
Rectangle (double length = 0, double width = 0);
double getLength = 0;
double getWidth = 0;
void setLength(double length);
void setWidth(double width);
double getArea();
private:
double length;
double width;
};


#endif // RECTANGLE_H_INCLUDED

我只包括我遇到麻烦的人。看到我如何知道我的其他工作,这是我上周做的一个程序的重写。每当我尝试构建它时,我都会遇到这两个错误。

隐式声明的'Shape :: shape()'的定义 区域未在此范围内声明。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

1)将Shape类更改为

class Shape {
public:
    Shape();
    double getArea();
    double area;
};

您尚未定义类的构造函数和area

2)你在Rectangle.h和Rectangle.cpp中编写了相同的代码。写入Rectangle

类中方法的Rectangle.cpp实现

答案 1 :(得分:0)

非常感谢你们。我修复了我编译的错误并运行它,一切似乎都没问题。所以再次感谢你。我也将.h文件放在我的.cpp占位符中,我很抱歉。这是我真实的.cpp文件,你们任何想知道。

#include <iostream>
#include "Rectangle.h"

using namespace std;

Rectangle::Rectangle(double len, double wid)
{
length = len;
width = wid;
}
double getLength();

void Rectangle::setLength(double len) {
length = len;
}

double getWidth();

void Rectangle::setWidth(double win) {
width = win;
}
double Rectangle::getArea() {
return width * length;
}
相关问题