单独编译
将函数原型 各种声明 以及符号常量 和内联函数放在头文件中
一个CPP文件包含main函数以调用其他函数
另外可以有多个CPP文件定义功能函数
头文件
#ifndef __COORDIN_H__
#define __COORDIN_H__
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
polar rect_to_polar(rect xypos);
void show_polar(polar dapos);
#endif
main.cpp
#include <iostream>
#include "coordin.h"
using namespace std;
int main(void)
{
rect rplace;
polar pplace;
cout << "Enter the x and y values: ";
while(cin >> rplace.x >> rplace.y)
{
pplace = rect_to_polar(rplace);
show_polar(pplace);
cout << "Next two number (q to quit):";
}
return 0;
}
cpp
#include <iostream>
#include <cmath>
#include "coordin.h"
using namespace std;
polar rect_to_polar(rect xypos)
{
polar answer;
answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
answer.angle = atan2(xypos.y, xypos.x);
return answer;
}
void show_polar(polar dapos)
{
const double Rad_to_deg = 57.29577951;
cout << "Distance = " << dapos.distance << endl;
cout << "Angle = " << dapos.angle * Rad_to_deg << " degree" << endl;
}
存储连续性、作用域和链接性
自动存储——局部变量 在函数开始运行时被创建 函数执行结束时被释放
静态存储——全局变量、static修饰的静态变量 在程序运行的整个过程都存在
动态存储——new运算符分配的内存 直到使用delete释放或者程序运行结束
链接性为外部的变量可以在文件之间共享 链接性为外部的变量只能在一个文件的函数中共享
自动变量无链接性所以不能共享
自动变量作用范围
#include <iostream>
using namespace std;
void oil(int x);
int main(void)
{
int texas = 31;
int year = 2011;
cout << "In main(), texas = " << texas << ", ";
cout << "&texas = " << &texas << endl;
cout << "In main(), year = " << year << ", ";
cout << "&year = " << &year << endl;
oil(texas);
cout << "In main(), texas = " << texas << ", ";
cout << "&texas = " << &texas << endl;
cout << "In main(), year = " << year << ", ";
cout << "&year = " << &year << endl;
return 0;
}
void oil(int x)
{
int texas = 5;
cout << "In oil(), texas = " << texas << ", ";
cout << "&texas = " << &texas << endl;
cout << "In oil(), x = " << x << ", ";
cout << "&x = " << &x << endl;
{
int texas = 113;
cout << "In block, texas = " << texas << ", ";
cout << "&texas = " << &texas << endl;
cout << "In block, x = " << x << ", ";
cout << "&x = " << &x << endl;
}
cout << "Post-block, texas = " << texas << ", ";
cout << "&texas = " << &texas << endl;
}
使用外部变量
头文件
#ifndef __SUPPORT_H__
#define __SUPPORT_H__
#include <iostream>
extern double warming;
void update(double dt);
void local(void);
#endif
main.cpp
#include <iostream>
#include "support.h"
using namespace std;
double warming = 0.3;
int main(void)
{
cout << "Global warming is " << warming << " degrees." << endl;
update(0.1);
cout << "Global warming is " << warming << " degrees." << endl;
local();
cout << "Global warming is " << warming << " degrees." << endl;
return 0;
}
功能函数
#include "support.h"
using namespace std;
void update(double dt)
{
warming += dt;
cout << "Updating global warming to " << warming << " degrees." << endl;
}
void local(void)
{
double warming = 0.8;
cout << "Local warming = " << warming << " degrees" << endl;
cout << "But global wamring = " << ::warming << " degrees" << endl;
}
静态持续性、内部链接性
#include <iostream>
using namespace std;
int tom = 3;
int dick = 30;
static int harry = 300;
void remote_access(void);
int main(void)
{
cout << "main() reports the following addresses: " << endl;
cout << "&tom = " << &tom << ". &dick = " << &dick << ". &harry = " << &harry << endl;
remote_access();
return 0;
}
#include <iostream>
using namespace std;
extern int tom;
static int dick = 10;
int harry = 200;
void remote_access(void)
{
cout << "remote_access() reports the following addresses: " << endl;
cout << "&tom = " << &tom << ". &dick = " << &dick << ". &harry = " << &harry << endl;
}
#include <iostream>
using namespace std;
const int ArSize = 10;
void strcount(const char *str);
int main(void)
{
char input[ArSize];
char next;
cout << "Enter a line: " << endl;
cin.get(input, ArSize);
while(cin)
{
cin.get(next);
while(next != '\n')
cin.get(next);
strcount(input);
cout << "Enter next line (empty line to quit): " << endl;
cin.get(input, ArSize);
}
cout << "ByeBye" << endl;
return 0;
}
void strcount(const char *str)
{
static int total = 0;
int count = 0;
while(*str++)
count++;
total += count;
cout << count << " characters" << endl;
cout << total << " characters total" << endl;
}
const类型的全局变量链接性为内部
覆盖
#include <iostream>
#include <new>
using namespace std;
const int BUF = 512;
const int N = 5;
char buffer[BUF];
int main(void)
{
double *pd1, *pd2;
int i;
cout << "Calling new and placement new: " << endl;
pd1 = new double[N];
pd2 = new(buffer) double[N];
for(i = 0; i < N; i++)
pd2[i] = pd1[i] = 1000 + 20.0 * i;
cout << "pd1 = " << pd1 << ", buffer = " << (void *)buffer << endl;
for(i = 0; i < N; i++)
{
cout << pd1[i] << " at " << &pd1[i] << "; ";
cout << pd2[i] << " at " << &pd2[i] << endl;
}
cout << "\nCalling new and placement new a second time: " << endl;
double *pd3, *pd4;
pd3 = new double[N];
pd4 = new(buffer) double[N];
for(i = 0; i < N; i++)
pd4[i] = pd3[i] = 1000 + 40.0 * i;
for(i = 0; i < N; i++)
{
cout << pd3[i] << " at " << &pd3[i] << "; ";
cout << pd4[i] << " at " << &pd4[i] << endl;
}
cout << "\nCalling new and placement new a third time: " << endl;
delete[] pd1;
pd1 = new double[N];
pd2 = new(buffer + N*sizeof(double)) double[N];
for(i = 0; i < N; i++)
pd2[i] = pd1[i] = 1000 + 60.0 * i;
for(i = 0; i < N; i++)
{
cout << pd1[i] << " at " << &pd1[i] << "; ";
cout << pd2[i] << " at " << &pd2[i] << endl;
}
delete[] pd1;
delete[] pd3;
return 0;
}
delete用于释放动态内存 不能释放数组这种静态内存
名称空间
#include <string>
namespace pers
{
struct Person
{
std::string fname;
std::string lname;
};
void getPerson(Person &rp);
void showPerson(const Person &rp);
}
namespace debts
{
using namespace pers;
struct Debt
{
Person name;
double amount;
};
void getDebt(Debt &rd);
void showDebt(const Debt &rd);
double sumDebts(const Debt ar[], int n);
}
#include <iostream>
#include "namesp.h"
namespace pers
{
using std::cout;
using std::cin;
void getPerson(Person &rp)
{
cout << "Enter first name: ";
cin >> rp.fname;
cout << "Enter last name: ";
cin >> rp.lname;
}
void showPerson(const Person &rp)
{
cout << rp.lname << ", " << rp.fname;
}
}
namespace debts
{
void getDebt(Debt &rd)
{
getPerson(rd.name);
std::cout << "Enter debt: ";
std::cin >> rd.amount;
}
void showDebt(const Debt &rd)
{
showPerson(rd.name);
std::cout << ": $" << rd.amount << std::endl;
}
double sumDebts(const Debt ar[], int n)
{
double total = 0;
for(int i = 0; i < n; i++)
total += ar[i].amount;
return total;
}
}
#include <iostream>
#include "namesp.h"
int main(void)
{
using debts::Debt;
using debts::showDebt;
Debt golf = {{"Micheal", "Jordan"}, 120.0};
showDebt(golf);
return 0;
}