简体   繁体   English

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

[英]Area of a square, circle, and rectangle using inheritance in C++

I am having an issue with my program in C++.我在 C++ 中的程序有问题。 I need to find the area of a square, circle, and rectangle.我需要找到正方形、圆形和矩形的面积。 I have everything down with the circle and square but the rectangle and the shape (inheritance structure) are giving me the aforementioned issues.我把所有的东西都放在了圆形和方形上,但是矩形和形状(继承结构)给了我上述问题。 I have been beating my head against a wall trying to figure this out so if anyone could help me I would greatly appreciate it.我一直在努力解决这个问题,所以如果有人可以帮助我,我将不胜感激。 My code is:我的代码是:

 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形状.h

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

shape.cpp形状.cpp

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

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

I only included the ones I am having trouble with.我只包括了我遇到问题的那些。 Seeing as how I know my other ones work and that this is a rewrite of a program I did last week.看看我是如何知道我的其他人工作的,这是对我上周所做的程序的重写。 Every time I try to build it I get these two errors.每次我尝试构建它时,我都会遇到这两个错误。

Definition of implicitly-declared 'Shape::shape()' area was not declared in this scope.在此 scope 中未声明隐式声明的“Shape::shape()”区域的定义。

Any help would be greatly appreciated.任何帮助将不胜感激。

1) change your Shape class as 1)将您的形状 class 更改为

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

You have not defined constructor of class and area您尚未定义 class 和area的构造函数

2) You wrote same code in Rectangle.h and Rectangle.cpp. 2) 您在 Rectangle.h 和 Rectangle.cpp 中编写了相同的代码。 Write in Rectangle.cpp implementations of methods in class Rectangle在 Rectangle.cpp 中编写 class 矩形中方法的实现

Thank you guys so much.非常感谢你们。 I have fixed my errors compiled and ran it and everything seems to be fine with it.我已经修复了编译并运行它的错误,一切似乎都很好。 So once again thank you.所以再次感谢你。 Also I put my.h file in my.cpp placeholder I am sorry about that.我也将 my.h 文件放在 my.cpp 占位符中,对此我很抱歉。 Here is my real.cpp file for any of you wondering.这是我的 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