重慶網(wǎng)絡(luò)網(wǎng)站推廣網(wǎng)絡(luò)營銷公司排行榜
9.1 單獨編譯
Visual Studio中新建頭文件和源代碼
通過解決方案資源管理器,如圖所示:
分成三部分的程序(直角坐標(biāo)轉(zhuǎn)換為極坐標(biāo))
頭文件coordin.h
#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 // 如果被定義過了(啥也不做)
源代碼file1(放置main函數(shù),調(diào)用其他函數(shù))
#include <iostream>
#include "coordin.h" // 尖括號表明去系統(tǒng)目錄找,雙引號表明去當(dāng)前目錄找
using namespace std;int main(){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 numbers (q to quit): ";}cout << "Bye!" << endl;return 0;
}
源代碼file2(其他函數(shù)的定義)
#include <iostream>
#include <cmath>
#include "coordin.h"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;
}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" << endl;
}
9.2 存儲持續(xù)性、作用域和鏈接性
全局變量和局部變量
頭文件support.h
#ifndef __SUPPORT_H__
#define __SUPPORT_H__
#include <iostream>extern double warming; // 聲明外部變量(不要賦值)void update(double dt);
void local(void);#endif
源代碼external.cpp
#include <iostream>
#include "support.h"
using namespace std;double warming = 0.3;int main(){cout << "Global warming is " << warming << endl;update(0.1);cout << "Global warming is " << warming << endl;local();return 0;
}
源代碼support.cpp
#include "support.h"
using namespace std;void update(double dt) {warming += dt;cout << "Updating global warming to " << warming << endl;
}void local(void) {double warming = 0.8; // 局部變量,只在local內(nèi)部可見cout << "Local warming is " << warming << endl;// 作用域解析運算符(::),放在變量名前表示使用變量的全局版本cout << "But global warming is " << ::warming << endl;
}
static限定符用于全局變量
源代碼twofile1.cpp
#include <iostream>
using namespace std;int tom = 3;
int dick = 30;
static int harry = 300; // 僅在twofile1.cpp可見
void remote_access(void);int main() {cout << "main() reports the following addresses: " << endl;cout << "&tom = " << &tom << endl;cout << "&dick = " << &dick << endl;cout << "&harry= " << &harry << endl;remote_access();return 0;
}
源代碼twofile2.cpp
#include <iostream>
using namespace std;extern int tom; // 聲明為外部變量(來自twofile1.cpp)
static int dick = 10; // 只在twofile2.cpp中可見
int harry = 200; // 新建一個全局變量void remote_access(void) {cout << "remote_access() reports the following addresses:" << endl;cout << "&tom = " << &tom << endl;cout << "&dick = " << &dick << endl;cout << "&harry= " << &harry << endl;
}
static限定符用于局部變量(統(tǒng)計字符串的字符個數(shù))
#include <iostream>
using namespace std;const int ArSize = 10;
void strcount(const char * str);int main(){char input[ArSize];char next;cout << "Enter a line: " << endl;cin.get(input, ArSize);while (cin) {cin.get(next);// 如果輸入的內(nèi)容大于10個字符,則用cin.get全部消耗掉while (next != '\n')cin.get(next);strcount(input);cout << "Enter next line (empty line to quit):" << endl;cin.get(input, ArSize);}cout << "Bye!" << endl;return 0;
}void strcount(const char * str) {// 局部變量加static,無論調(diào)用多少次這個函數(shù),該變量只會在第一次初始化static int total = 0;int count = 0;while (*str) {count++;str++;}total += count;cout << count << " characters." << endl;cout << total << " characters total." << endl;
}
定位new運算符的使用
#include <iostream>
#include <new>
using namespace std;const int BUF = 512;
const int N = 5;
char buffer[BUF];int main(){double *pd1, *pd2;int i;cout << "Calling new and placement new: " << endl;// 用常規(guī)new運算符為N個double類型開辟內(nèi)存空間pd1 = new double[N];// 用定位new運算符為N個double類型開辟內(nèi)存空間pd2 = new (buffer) double[N];// 賦值for (int i = 0; i < N; i++) {pd2[i] = pd1[i] = 1000 + 20.0 * i;}cout << "pd1 = " << pd1 << ", buffer = " << (void *)buffer << endl;// 打印pd1和pd2的地址for (int 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 (int i = 0; i < N; i++) {pd4[i] = pd3[i] = 1000 + 40.0 * i;}for (int 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]; // 刪了重新new(申請和之前相同的地方)pd2 = new(buffer + N * sizeof(double)) double[N]; // 地址往后偏移5個double類型的長度for (int i = 0; i < N; i++) {pd2[i] = pd1[i] = 1000 + 60.0 * i;}for (int i = 0; i < N; i++) {cout << pd1[i] << " at " << &pd1[i] << ";";cout << pd2[i] << " at " << &pd2[i] << endl;}delete[] pd1;delete[] pd3; // pd2和pd4是定位new開辟出來的,delete不能用于定位newreturn 0;
}
9.3 名稱空間
當(dāng)名稱空間和聲明區(qū)域定義了相同名稱(偽代碼)
namespace Jill{double bucket(double n) {...}double fetch;struct Hill {...};
}
char fetch; // 全局變量
int main(){using namespace Jill; // 使用using編譯指令Hill Thrill; // 創(chuàng)建一個Jill::Hill 的結(jié)構(gòu)double water = bucket(2); // 使用Jill::bucket()double fetch; // 不會出錯,Jill::fetch被隱藏cin >> fetch; // 讀入一個數(shù)據(jù)到局部變量fetchcin >> ::fetch; // 讀入一個數(shù)據(jù)到全局變量fetchcin >> Jill::fetch; // 讀入一個變量到Jill::fetch...
}int foom(){Hill top; // 會出錯Jill::Hill crest; // 可用
}
名稱空間示例(打印人名及欠款)
頭文件namesp.h
#pragma once
#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 sunDebts(const Debt ar[], int n);
}
源代碼namessp.cpp
#include <iostream>
#include "namesp.h"int main() {using debts::Debt;using debts::showDebt;Debt golf = { {"Micheal", "Jordan"}, 120.0 };showDebt(golf);return 0;
}
源代碼namesp.cpp
#include <iostream>
#include "namesp.h" // 頭文件放結(jié)構(gòu)體類型、函數(shù)原型聲明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 sunDebts(const Debt ar[], int n) {double total = 0;for (int i = 0; i < n; i++) {total += ar[i].amount;}return total;}
}