主题:多文件方式组织
昨天看到有人发了一个与此相关的帖子,我做了一下,调试可以运行,请高手解释一下各个文件的格式功能
原题是函数重载的问题,求面积的,要求在area.h中声明各个area()函数原型,在area.cpp中定义函数,然后在Exp9_2.cpp中包含area.h,定义main()函数并执行。
//area.h中代码
#define PI 3.14159
double area(double radius=0);
double area(double a,double b);
double area(double a,double b,double h);
double area(double a,double b,double c,int);
//area.cpp
#include "area.h"
#include <iostream.h>
#include <math.h>
double area(double radius)
{
return PI*radius*radius;
}
double area(double a,double b)
{
return a*b;
}
double area(double a,double b,double h)
{
return (a+b)*h/2;
}
double area(double a,double b,double c,int)
{
double t;
t=(a+b+c)/2;
return sqrt(t*(t-a)*(t-b)*(t-c));
}
//Exp9_2.cpp
#include <iostream.h>
#include "area.h"
int main()
{
cout<<"园的面积为:"<<area(10)<<endl;
cout<<"矩形的面积为:"<<area(2,3)<<endl;
cout<<"梯形的面积为:"<<area(2,3,8)<<endl;
cout<<"三角形的面积为:"<<area(3,4,5,0)<<endl;
return 0;
}
还有,再看一些程序源代码的时候经常是多文件方式,有时候不知道怎么下手看,请高手指点,不胜感激!
原题是函数重载的问题,求面积的,要求在area.h中声明各个area()函数原型,在area.cpp中定义函数,然后在Exp9_2.cpp中包含area.h,定义main()函数并执行。
//area.h中代码
#define PI 3.14159
double area(double radius=0);
double area(double a,double b);
double area(double a,double b,double h);
double area(double a,double b,double c,int);
//area.cpp
#include "area.h"
#include <iostream.h>
#include <math.h>
double area(double radius)
{
return PI*radius*radius;
}
double area(double a,double b)
{
return a*b;
}
double area(double a,double b,double h)
{
return (a+b)*h/2;
}
double area(double a,double b,double c,int)
{
double t;
t=(a+b+c)/2;
return sqrt(t*(t-a)*(t-b)*(t-c));
}
//Exp9_2.cpp
#include <iostream.h>
#include "area.h"
int main()
{
cout<<"园的面积为:"<<area(10)<<endl;
cout<<"矩形的面积为:"<<area(2,3)<<endl;
cout<<"梯形的面积为:"<<area(2,3,8)<<endl;
cout<<"三角形的面积为:"<<area(3,4,5,0)<<endl;
return 0;
}
还有,再看一些程序源代码的时候经常是多文件方式,有时候不知道怎么下手看,请高手指点,不胜感激!