繁体   English   中英

在 C++ 中使用 inheritance 的正方形、圆形和矩形的面积

[英]Area of a square, circle, and rectangle using inheritance in 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 }

形状.h

 #ifndef SHAPE_H_INCLUDED #define SHAPE_H_INCLUDED class Shape { public: double getArea(); }; #endif // SHAPE_H_INCLUDED

形状.cpp

 #include "shape.h" Shape::Shape() { area = 0; } double Shape::getArea() { return area; }

矩形.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

矩形.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

我只包括了我遇到问题的那些。 看看我是如何知道我的其他人工作的,这是对我上周所做的程序的重写。 每次我尝试构建它时,我都会遇到这两个错误。

在此 scope 中未声明隐式声明的“Shape::shape()”区域的定义。

任何帮助将不胜感激。

1)将您的形状 class 更改为

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

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

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

非常感谢你们。 我已经修复了编译并运行它的错误,一切似乎都很好。 所以再次感谢你。 我也将 my.h 文件放在 my.cpp 占位符中,对此我很抱歉。 这是我的 real.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; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM