《C++ primer plus》学习笔记——第九章内存模型和名称

版权声明:扫我头像就可以向我提问,共同提高:) https://blog.csdn.net/u011436427/article/details/87912706

一、单独编译

(1)C++为在内存中存储数据方面提供了多种选择。
可以选择数据保留在内存中的时间长度(存储持续性)以及程序的哪一部分可以访问数据等等。
(2)C++名称空间是另一种控制访问权的方式。
(3)通常,大型程序都由多个源代码文件组成,这些文件可能共享一些数据。
这样的数据涉及到程序文件的单独编译

1.单独编译

(1)和C语言一样,C++也允许甚至鼓励程序员将组件函数放在独立的文件中。
甚至也可以单独地编译这些文件,然后将他们链接成可执行的程序。

(2)通常,C++编译器既编译程序,也管理链接器。如果只修改了一个文件,则可以只重新编译该文件,然后将它与其它文件的编译版本链接。

(3)举一个eg,从中了解一下编译的细节:
在第七章中的<2.另一个处理结构的函数示例(值传递)>中,
在这里插入图片描述
解决办法如下:
在这里插入图片描述

所以,最终的结果是:
将原程序分为了三部分:
(a)头文件:包含结构声明和使用这些结构的函数原型
(b)源代码文件:包含与结构有关的函数的代码
(c)源代码文件:包含调用与结构相关的函数代码

(4)请不要将函数定义或者变量声明放到头文件中。 如果同一个程序中将包含同一个函数的两个定义,除非函数是内联的,否则,这将出错。
头文件中常常包含的内容如下:
在这里插入图片描述

(5)#include“coordin.h”与#include<coordin.h>的区别为:
在这里插入图片描述
注意:只需将源代码文件加如到项目中,而不用加如到头文件
这是因为#include指令管理头文件,另外,不要使用#include来包含.cpp的源代码文件,这样会导致多重声明
在这里插入图片描述

(6)在UNIX系统中编译由多个文件组成的C++程序的流程是:
在这里插入图片描述

(7)对于头文件的管理而言,
在这里插入图片描述
在这里插入图片描述

(8)最终的程序代码如下:
在coordin.h的头文件中的代码如下所示:

// coordin.h -- structure templates and function prototypes
// structure templates
#ifndef COORDIN_H_
#define COORDIN_H_

struct polar
{
    double distance;    // distance from origin
    double angle;        // direction from origin
};
struct rect
{
    double x;        // horizontal distance from origin
    double y;        // vertical distance from origin
};

// prototypes
polar rect_to_polar(rect xypos);
void show_polar(polar dapos); 

#endif

在file1.cpp的头文件中的代码如下所示:


// file1.cpp -- example of a three-file program
#include <iostream>
#include "coordin.h" // structure templates, function prototypes
using 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";



// keep window open in MSVC++

    cin.clear();
    while (cin.get() != '\n')
        continue;
    cin.get();

    return 0; 
}

在file2.cpp的头文件中的代码如下所示:

// file2.cpp -- contains functions called in file1.cpp
#include <iostream>
#include <cmath>
#include "coordin.h" // structure templates, function prototypes

// convert rectangular to polar coordinates
polar 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;      // returns a polar structure
}

// show polar coordinates, converting angle to degrees
void show_polar (polar 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";
}

在这里插入图片描述

说明:
(a)注意一下,下面的C++专属控制台暂停代码的写法:
在这里插入图片描述
如果, 代码写成下面:
说明:while里面的语句只要不换行,一直可以cin.get(),如果换行了,就跳出while
在这里插入图片描述
则:
在这里插入图片描述

如果:
说明:如果while中的代码一直是换行(只检测换行符),那么就一直cin.get(),如果输入数字,再按下换行,就会跳出
在这里插入图片描述
则:
在这里插入图片描述

(b)
在这里插入图片描述

(9)C++多个库的链接
在这里插入图片描述

二、存储持续性、作用域和链接性

猜你喜欢

转载自blog.csdn.net/u011436427/article/details/87912706