簡體   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