2017-09-09 46 views
1

我在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()' area的定义未在此范围内声明。

任何帮助将不胜感激。

+0

为什么要在.h和.cpp中声明类(相同)?将.h添加到.cpp – Yunnosch

+0

错误表示该区域未声明。 shape.h中的类声明不声明属性“area”。矩形也不是。那么什么不清楚? – Yunnosch

+0

通常情况下,如果你有某种形式的状态,你只能使用对象。 (除非你想学习一些东西并找到一个不好的例子用例)。越短越好。 'double CircleArea(double r){return 4 * atan(1.0)* r * r; }'' - 这样简单的函数可以替换你的任何类。 – BitTickler

回答

0

1)改变你的形状类作为

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

您还没有定义的类的构造函数和area

2)您在Rectangle.h和Rectangle.cpp写了相同的代码。写在Rectangle.cpp类中的方法的实现Rectangle

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; 
} 
相关问题