单独编译

来源:互联网 发布:嘉拉提亚的凯旋知乎 编辑:程序博客网 时间:2024/06/11 23:47
///头文件中常包含的内容:///函数原型///使用#define或const定义的符号常量///结构声明///类声明///模板声明///内联函数///不要将函数定义或变量声明放到头文件中,可能带来麻烦///例如:若在头文件中包含一个函数定义,然后在其它两个文件///(属于同一程序)中包含该头文件,则同一个程序中将包含同一个///函数的两个定义,除非函数是内联的,否则将出错。///*********///在IDE中,不要将头文件加入到项目列表中,也不要在源代码文件///中使用#include来包含其他源代码文件。/*#include <iostream>using namespace std;int main(){    return 0;}*//// coordin.h -- structure templates and function-prototypes///structure-templates#ifndef COORDIN_H_#define COORDIN_H_struct polar{    double distance;    double angle;};struct rect{    double x;    double y;};///prototypes 原型:polar rect_to_polar(rect xypos);void show_polar(polar dapos);#endif // COORDIN_H_/*Enter the x and y values: 120 80distance = 144.222, angle = 33.6901 degreesNext two numbers (q to quit): 120 50distance = 130, angle = 22.6199 degreesNext two numbers (q to quit): qBye!Process returned 0 (0x0)   execution time : 24.975 sPress any key to continue.*/

///file1.cpp -- example of a three-file program#include<iostream>#include"coordin.h" ///structure templates, function prototypesusing namespace std;int main(){    rect rplace;    polar pplace;    cout << "Enter the x and y values: ";    while(cin >> rplace.x >> rplace.y) /// slick use of cin    {        pplace = rect_to_polar(rplace);        show_polar(pplace);        cout << "Next two numbers (q to quit): ";    }    cout << "Bye!\n";    return 0;}

///file2.cpp -- contains functions called in file1.cpp#include<iostream>#include<cmath>#include"coordin.h"///convert rectangular to polar coordinatespolar rect_to_polar(rect xypos){    using namespace std;    polar answer;    answer.distance =        sqrt(xypos.x * xypos.x + xypos.y * xypos.y);    answer.angle = atan2(xypos.y, xypos.x);    return answer; /// return a polar structure}///show polar coordinates, converting angle to degreesvoid show_polar (polar dapos) /// dapos 名字{    using namespace std;    const double Rad_to_deg = 57.29577951;    cout << "distance = " << dapos.distance;    cout << ", angle = " << dapos.angle * Rad_to_deg;    cout << " degrees\n";}

0 0