1. 程式人生 > 其它 >小甲魚-C++快速入門筆記(40)之名稱空間和模組化程式設計2

小甲魚-C++快速入門筆記(40)之名稱空間和模組化程式設計2

技術標籤:小甲魚C++

使用標頭檔案

在建立了標頭檔案之後,只要把它的檔名用雙引號括起來寫在如下所示的指令裡就可以匯入它:

#include “fishc.h”

注意:自定義的標頭檔案用雙引號括起來,系統定義的標頭檔案用<>。

如果沒有給出路徑名,編譯器將到當前子目錄以及當前開發環境中的其他邏輯子目錄裡去尋找標頭檔案。為了消除這種猜測,在匯入自己的標頭檔案時可以使用相對路徑。如果標頭檔案與主程式檔案在同一個子目錄裡,則可以這麼寫:

#include “./fishc.h”

// fishc.h
// Create by 小甲魚
// 2012-09-05
 
// 演示用途
 
void call_love_fishc();
#include <iostream>
#include "fishc.h"
 
using namespace std;
 
int main()
{
	call_love_fishc();
 
	return 0;
}
 
void call_love_fishc()
{
	int i, j;
	int n = 10;
 
	for(i=1-(n>>1); i<=n; i++)
	{
		cout << "I love FISHC.com";
 
		if(i >= 0)
		{
			for(j=0; j<i; j++)
				cout << " ";
			for(j=1; j<=2*(n-i)+1; j++)
				cout << " *";
			cout << endl;
		}
		else
		{
			for(j=i; j<0; j++)
				cout << " ";
			for(j=1; j<=n+2+i+1; j++)
				cout << " *";
			for(j=1; j<=-1-2*i; j++)
				cout << " ";
			for(j=1; j<=n+2+i+1; j++)
				cout << " *";
			cout << endl;
		}
	}
}

建立實現檔案

以過載有理數運算例子:

標頭檔案:

#include <iostream>
#include <string>
#include <cmath>
 
using namespace std;
 
#ifndef _ONCE_
#define _ONCE_
 
class Rational
{
public:
	Rational(int num, int denom);  //num=分子,denom=分母
 
	Rational operator+(Rational rhs); //rhs = right hand side
	Rational operator-(Rational rhs);
	Rational operator*(Rational rhs);
	Rational operator/(Rational rhs);
 
	void print();
 
private:
	void normalize();  //負責對分數的簡化處理
 
	int numerator;   //分子
	int denominator; //分母
};
#endif

函式定義cpp

#include "rational.h"
#include <iostream>
#include <stdlib.h>
 
using namespace std;
 
Rational::Rational(int num, int denom)
{
	numerator = num;
	denominator = denom;
 
	normalize();
}
 
// normalize() 對分數進行簡化操作包括:
// 1.只允許分子為負數,如果分母為負數則把負數挪到分子部分,1/-2 == -1/2
// 2.利用歐幾里得演算法(輾轉求餘原理)將分數進行簡化:2/10=>1/5
void Rational::normalize()
{
	// 確保分母為正
	if (denominator < 0)
	{
		numerator = -numerator;
		denominator = -denominator;
	}
 
	// 歐幾里得演算法
	int a = abs(numerator);
	int b = abs(denominator);
 
	// 求出最大公約數
	while (b > 0)
	{
		int t = a % b;
		a = b;
		b = t;
	}
 
	// 分子,分母分別除以最大公約數得到最簡化分數
	numerator /= a;
	denominator /= a;
}
 
Rational Rational::operator+(Rational rhs)
{
	int a = numerator;
	int b = denominator;
	int c = rhs.numerator;
	int d = rhs.denominator;
 
	int e = a*d + b*c;
	int f = b*d;
 
	return Rational(e, f);
}
 
Rational Rational::operator-(Rational rhs)
{
	rhs.numerator = -rhs.numerator;
 
	return operator+(rhs);
}
 
Rational Rational::operator*(Rational rhs)
{
	int a = numerator;
	int b = denominator;
	int c = rhs.numerator;
	int d = rhs.denominator;
 
	int e = a*c;
	int f = b*d;
 
	return Rational(e, f);
 
}
 
Rational Rational::operator/(Rational rhs)
{
	int t = rhs.numerator;
	rhs.numerator = rhs.denominator;
	rhs.denominator = t;
 
	return operator*(rhs);
}
 
void Rational::print()
{
	if (numerator % denominator == 0)
		cout << numerator / denominator;
	else
		cout << numerator << "/" << denominator;
	
}

主函式:

#include "rational.h"
#include "rational.cpp"
#include <iostream>
#include <stdlib.h>
 
int main()
{
	Rational f1(2, 16);
	Rational f2(7, 8);
 
	// 測試有理數的加法運算
	Rational res = f1 + f2;
	f1.print();
	cout << " + ";
	f2.print();
	cout << " = ";
	res.print();
	cout << endl;
 
	// 測試有理數的減法運算
	Rational sub = f1 - f2;
	f1.print();
	cout << " - ";
	f2.print();
	cout << " = ";
	sub.print();
	cout << endl;
 
	// 測試有理數的乘法運算
	Rational mul = f1 * f2;
	f1.print();
	cout << " * ";
	f2.print();
	cout << " = ";
	mul.print();
	cout << endl;
 
	// 測試有理數的加法運算
	Rational div = f1 / f2;
	f1.print();
	cout << " / ";
	f2.print();
	cout << " = ";
	div.print();
	cout << endl;
 
	return 0;
}

注意一個問題:

類定義時防止 'class' type redefinition

在類定義時,加入這三句,防止防止標頭檔案被重複包含造成類被重複定義
class定義前加
#ifndef _ONCE_
#define _ONCE_
class定義的分號後加
#endif

引用別人的話:
防止重複包含

防止標頭檔案被重複包含造成類被重複定義
---------------------------------------------------------------

#ifndef 後面的內容可以任意,但最好唯一,並且易讀。

----------------------------------------------------------
#ifndef __SOMETHING_H__
#define __SOMETHING_H__

// 標頭檔案中的宣告

#endif
----------------------------------------------------------

任何標頭檔案都應該包含上面的內容,把真正的宣告放在其中。
這組巨集叫做“守衛巨集”,為的就是防止標頭檔案被重複包含所導致的型別被重複定義。

C前處理器

利用c++前處理器,我們可以讓標頭檔案只在這個類還沒被宣告過的情況下才宣告它。

前處理器的條件指令:

如上述的程式碼:

#ifndef _ONCE_
#define _ONCE_
class Rational{...};
#endif

作為一種固定模式,這裡使用的變數名通常與相應的檔名保持一致,把句點替換為下劃線。

於是rational.h檔案將對應RATIONAL_H

#ifndef RATIONAL_H
#define RATIONAL_H
class Rational{...};
#endif