github链接:https://github.com/leijing000/object-oriented/tree/master/Circle
一、题目描述:
Create a program that asks for the radius of a circle and prints the area of that circle .
Use cin
and cout
.
The whole program should be divided into two source files(cpp) .
Hand in the source files and the head files which you create .
二、代码实现(包含一个头文件和两个源文件)
- Circle.h(声明函数,#define)
#define pi 3.1415
double calculate(double radius);
- function.cpp(实现函数功能)
#include"Circle.h"
double calculate(double radius)
{
double area;
area = pi*radius*radius;
return area;
}
- main.cpp(计算圆的面积,调用函数)
#include"Circle.h"
#include<iostream>
using namespace std;
int main()
{
double area;
double radius;
cout << "Please enter the radius of the circle:";
cin >> radius;
area = calculate(radius);
cout << "The area of the circle is: " << area << endl;
return 0;
}
三、遇到的一些问题:
-
出现这个问题的原因是我在define 后面加了分号,将分号去掉就可以了。
-
写的过程中还出现了重定义的问题,我将在头文件中定义的变量放到了main.cpp里定义就可以了。