我需要帮助。已经为此工作了两个星期。任何建议将不胜感激。谢谢。
这里是作业:
研究如何计算圆形和正方形的面积。
使用C ++应用程序来计算圆形和正方形的面积。您应该将代码组织到头文件(Circle.h,Square.h)和实现文件(Circle.cpp。Square.cpp。main.cpp)中。
我无法使用的代码:
Main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
using namespace std;
int main()
{
double radius = 0;
int length = 0;
Square square;
Circle circle;
int ch;
switch(ch)
{
case 1:
{
cout << "\nEnter radius of the circle: ";
cin >> radius;
cout << "\nThe Area of the circle is "<< circle.getArea();
break;
}
case 2:
{
cout << "\nPlease Enter the length of the Square's side: ";
cin >> length;
cout << "\The Area of the square is "<< square.getArea();
break;
}
return 0;
}
Square.h
#include <iostream>
#include "Circle.h"
#include "Square.h"
using namespace std;
int main()
{
double radius = 0;
int length = 0;
Square square;
Circle circle;
int ch;
switch(ch)
{
case 1:
{
cout << "\nEnter radius of the circle: ";
cin >> radius;
cout << "\nThe Area of the circle is "<< circle.getArea();
break;
}
case 2:
{
cout << "\nPlease Enter the length of the Square's side: ";
cin >> length;
cout << "\The Area of the square is "<< square.getArea();
break;
}
return 0;
}
Circle.h
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
using namespace std;
class Circle
{
private:
double radius, area;
public:
Circle(double radius = 0);
double getRadius()const;
void setRadius(double radius);
double getArea()const;
};
#endif // CIRCLE_H_INCLUDED
square.cpp
#include <cmath>
#include <iostream>
#include "Square.h"
using namespace std;
Square::Square(int len)
{
length = len;
}
int Square::getLength()const
{
return length;
}
void Square::setLength(int len)
{
length = len;
}
int Square::getArea() const
{
return length * length;
}
circle.cpp
#include <cmath>
#include <iostream>
#include "Circle.h"
using namespace std;
Circle::Circle(double r)
{
radius = r;
}
double Circle::getRadius()const
{
return radius;
}
void Circle::setRadius (double r)
{
radius = r;
}
double Circle::getArea() const
{
return radius * radius * 3.1415926;
}
好吧,我看到的是您没有为ch分配任何内容不是Java,而是假定一些未分配的变量为0。您要么要求用户输入,要么将值分配给ch。
cout<<"Enter choice: ";
cin>>ch;