2017-09-09 36 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 

私は問題を抱えているものだけを含んでいました。私が他の人がどのように働いているかを知る方法を見て、これは先週私がやったプログラムの書き直しです。私はそれを構築しようとするたびに、私はこれらの2つのエラーを取得します。

暗黙的に宣言された 'Shape :: shape()'の定義 エリアはこのスコープで宣言されていません。

ご協力いただければ幸いです。

+0

なぜクラスを(同じように).hと.cppで宣言しますか? .cppに.hを含めます。 – Yunnosch

+0

エラーは、領域が宣言されていないことを示します。 shape.hのクラス宣言は、属性 "area"を宣言しません。長方形もどちらもしません。だから何が分かりませんか? – Yunnosch

+0

通常、何らかの形の状態がある場合にのみ、オブジェクトを使用します。 (あなたが何かを学び、悪い例のユースケースを見つけたいのでない限り)。短い方が良いです。 '' double CircleArea(double r){4 * atan(1.0)* r * rを返します。 } '' - そのような単純な関数は、あなたのクラスを置き換えることができます。 – BitTickler

答えて

0

1)

class Shape { 
public: 
    Shape(); 
    double getArea(); 
    double area; 
}; 
あなたはクラスのコンストラクタ定義していない

area

2)あなたはRectangle.hとRectangle.cppで同じコードを書いたように、あなたのShapeクラスを変更します。 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; 
} 
関連する問題