Ich habe ein Problem mit meinem Programm in C++. Ich muss die Fläche eines Quadrats, eines Kreises und eines Rechtecks finden. Ich habe alles mit Kreis und Quadrat, aber das Rechteck und die Form (Vererbungsstruktur) geben mir die oben erwähnten Probleme. Ich habe meinen Kopf gegen eine Wand geschlagen, um herauszufinden, ob jemand mir helfen könnte, ich würde es sehr schätzen. Mein Code ist:Bereich eines Quadrats, eines Kreises und eines Rechtecks unter Verwendung der Vererbung in 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
Ich habe nur diejenigen einbezogen, mit denen ich Probleme habe. Sehen, wie ich weiß, dass meine anderen arbeiten und dass dies eine Neufassung eines Programms ist, das ich letzte Woche gemacht habe. Jedes Mal, wenn ich versuche, es zu bauen, bekomme ich diese zwei Fehler.
Definition von implizit deklarierten 'Shape :: shape()' Bereich wurde in diesem Bereich nicht deklariert.
Jede Hilfe würde sehr geschätzt werden.
Warum deklarieren Sie die Klasse (identisch) in .h und .cpp? Fügen Sie die .h in die .cpp ein – Yunnosch
Fehler sagt, dass Bereich nicht deklariert ist. Ihre Klassendeklaration in shape.h deklariert das Attribut "Bereich" nicht. Rectangle auch nicht. Also was ist unklar? – Yunnosch
Normalerweise verwenden Sie nur Objekte, wenn Sie irgendeine Form von Status haben. (Es sei denn, du willst etwas lernen und ein schlechtes Beispiel finden). Kürzere ist besser. '' doppelter CircleArea (double r) {return 4 * atan (1.0) * r * r; } '' - Einfache Funktionen wie diese können jede deiner Klassen ersetzen. – BitTickler