日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C與C++之異同

 重金屬男孩 2007-06-10
C與C++異同
C++在任何地方均可宣告變數(shù)
輸出入方式改用cin與cout
#include方式不加上.h
名稱空間(namespace)
C++存在預(yù)設(shè)的類別與物件
參考(reference)
inline函數(shù)
函式預(yù)設(shè)參數(shù)
函式重載(overloading)
1-3
C與C++異同
C++之動(dòng)態(tài)記憶體配置使用new/delete
C++提供例外處理
新的轉(zhuǎn)型方法
新的資料型態(tài)bool
1-4
基本概念
語法
-所有C語言中之指令在C++ 均可使用
-C++為物件導(dǎo)向(object oriented)之程式語言
檔名
-C語言的副檔名為.c,而C++的副檔名為.cpp
注解
-在C語言中注解系以/* 為開始,以*/為結(jié)束
-在C++中//之後一整行均為注解
1-5
宣告變數(shù)
C只能在區(qū)塊開始時(shí)宣告變數(shù)
-例:
for (i=1;i<=5; i++){
int x;
}
C++ 可在程式任何地方宣告變數(shù)
-例:
for (int i=0;istring[i]=string2[i];
-例:
c=a+b;
int x;
x=a-b;
1-6
輸入與輸出
在C中常用的輸出入指令為printf與scanf
-在使用時(shí)需要#include
-printf與scanf 在做變數(shù)輸出入時(shí)必須搭配格式使用
在C++中常用的輸出入指令為cout與cin
-在使用時(shí)需要#include
-新的C++語法為#include 搭配namespace std使

-cout與cin在輸出時(shí)直接照輸出入順序使用
在C++中只需#include 則printf與scanf等C
語言函數(shù)亦可使用
1-7
C++之輸出: cout
在C++中的輸出使用cout,不必考慮變數(shù)的型態(tài).直
接在cout後面加入<<再寫上欲輸出之值.
若有多個(gè)值要輸出則連續(xù)用<<加上欲輸出值即可
-例:
cout << "value";
cout << x;
cout << "Value" <cout <<"Value" << x <<"\n";
cout << "Value" << x << ‘\n‘;
cout << "Value" <
再寫上欲輸出之值.
-例:
cin >> answer;
若有多個(gè)值要輸出則連續(xù)用>>加上欲輸出值即可,且
變數(shù)間使用空白做隔開
-例:
cin >> x >> y;
1-9
C++之輸入: cin
輸入一整行至一字串中
-cin.getline(字串,輸入字串的最大長(zhǎng)度)
-cin.getline(str,80);
從鍵盤輸入單一字元
-ch=cin.get();
1-10
Program Hello
// This program outputs the message
//
// Hello!
//
// to the screen
#include
using namespace std;
int main() {
cout <<"Hello!"<< endl;
return 0;
}
1-11
名稱空間(namespace)
namespace用途在於區(qū)分同名稱之變數(shù)與函式,使其
均可存在并使用
語法
namespace 名稱空間名稱{
變數(shù)與函式之宣告
}
namespace與類別寫法相類似但不需在最後加上分號(hào)
1-12
名稱空間的使用
定義於名稱空間之變數(shù)與函式一定要經(jīng)過using 才可
使用.若不使用using則需直接指定
名稱空間之使用
-using namespace 名稱空間名稱
單獨(dú)using 一個(gè)變數(shù)或函式
using 名稱空間:: 變數(shù)名稱;
using 名稱空間:: 函式名稱;
1-13
名稱空間同時(shí)使用之問題
同時(shí)using 多個(gè)namespace 時(shí)其變數(shù)與函式不可重
復(fù)
-例:
namespace X{
void f(){ cout<<" In X::f()\n";}
}
namespace Y{
void f(){ cout<<" In Y::f()\n";}
}
不可在程式中同時(shí)使用
using namespace X; 與using namespace Y;
1-14
名稱空間的例子
#include
namespace X{
void f(){
cout<<" In X::f()\n";
}
void g(){
cout<<" In X::g()\n";
}
}
namespace Y{
void f(){
cout<<" In Y::f()\n";
}
}
using namespace Y // 使用名稱Y
using X::g; // 單獨(dú)using
void main(){
f(); // 使用Y當(dāng)中的f()
g();
X::g(); // 直接指定
}
1-15
cin / cout 之四種寫法
寫法1:不使用namespace 的觀念
#include
cout <<"Hello\n";
寫法2:使用namespace std
#include
using namespace std;
cout <<"Hello\n";
1-16
cin / cout 之四種寫法
寫法3:只using cin 和cout
#include
using std::cin;
using std::cout;
using std::endl;
cout <<"Hello\n";
寫法4:直接使用std::cin 與std::cout
#include
std::cout << "Hello\n";
1-17
C++提供之類別與參數(shù)
C++語言本身提供內(nèi)定之類別與物件
-類別
-字串類別string
-輸入檔案類別ifstream
-輸出檔案類別ofstream
-物件
-輸出物件cout
-輸入物件cin
-錯(cuò)誤訊息輸出物件cerr
1-18
字串類別
C++語言本身提供內(nèi)定之類別與物件
-類別
-字串類別string
-輸入檔案類別ifstream
-輸出檔案類別ofstream
-物件
-輸出物件cout
-輸入物件cin
-錯(cuò)誤訊息輸出物件cerr
1-19
檔案類別
檔案類別(使用時(shí)必須#include )
-輸入檔之檔案類別ifstream
-輸出檔之檔案類別ofstream
檔案使用之順序
-宣告物件ifstream afile("test.txt");
-開檔afile.open();
-讀寫資料afile >> x;
-關(guān)檔afile.close();
由ifstream與ofstream 宣告之物件可直接使用>>與<< 做輸出入
1-20
參考(Reference)
宣告為參考時(shí)并不多占記憶體,只是與原有之變數(shù)占
同一塊記憶體.
參考所宣告的只是一個(gè)名稱,必須設(shè)定初值為現(xiàn)有之
變數(shù)
參考的語法
-type & identifier1=identifier2;
-則identifier1與identifier2表相同的變數(shù),有相同之記憶體位置
例:
int a,b;
int&alt=a; // alt 為a 之參考
alt=b; // 等於做a=b 的動(dòng)作
alt++; // 等於做a++ 的動(dòng)作
1-21
參數(shù)傳遞方式
程式語言的參數(shù)傳遞方式有
-傳值呼叫(call by value)
-傳址呼叫(call by address)
-傳參考呼叫(call by reference)
C語言提供傳值呼叫與傳址呼叫,而C++則多了傳參
考呼叫
1-22
傳值呼叫(call by value)
傳值呼叫(call by value)
-以「拷貝」的方式,主程式內(nèi)的物件資料與副程式內(nèi)的物件
資料分占不同的記憶體
缺點(diǎn)
-傳遞速度慢
-需要較多的記憶體空間
1-23
傳值呼叫之實(shí)例(兩數(shù)交換)
#include
using namespace std;
void swap(int, int); // 傳值呼叫
void main() {
int i=7, j=-3;
swap(i,j);
cout << "i = " << i << endl; // 將輸出i=7
<< "j = " << j << endl; // 將輸出j=-3,兩者未交換
}
void swap(int a, int b) { // 傳值呼叫
int t;
t = a;
a = b;
b = t;
}
1-24
傳址呼叫(call by address)
傳址呼叫(call by address)
-以類別物件指標(biāo)來傳遞物件,主程式內(nèi)的物件資料與副程式
內(nèi)的物件資料占同一塊的記憶體
優(yōu)點(diǎn)
-傳遞速度快
-不占記憶體空間
缺點(diǎn)
-語法較為復(fù)雜
1-25
傳址呼叫之實(shí)例(兩數(shù)交換)
#include
using namespace std;
void swap(int*, int*); // 傳址呼叫
void main() {
int i=7, j=-3;
swap(&i,&j);// 傳入i 與j 的位址
cout << "i = " << i << endl; // 將輸出i=-3
<< "j = " << j << endl; // 將輸出j=7,成功地做數(shù)值交換
}
void swap(int *a, int *b) { // 傳址呼叫
int t;
t = *a;
*a = *b;
*b = t;
}
1-26
傳參考呼叫(call by reference)
傳參考呼叫(call by reference)
-與傳址呼叫(call by address)一樣,主程式內(nèi)的物件資料與副
程式內(nèi)的物件資料占同一塊的記憶體傳
優(yōu)點(diǎn)
-傳遞速度快
-不占記憶體空間
問題
-副程式中可以將主程式中的變數(shù)值改掉
-解決方式: 在副程式的參數(shù)列加const宣告
1-27
傳參考呼叫之實(shí)例(兩數(shù)交換)
#include
using namespace std;
void swap(int&, int&); // 傳參考呼叫
void main() {
int i=7, j=-3;
swap(i,j);
cout << "i = " << i << endl; // 將輸出i=-3
<< "j = " << j << endl; // 將輸出j=7 ,成功地做數(shù)值交換
}
void swap(int &a, int &b) { // 傳參考呼叫
int t;
t = a;
a = b;
b = t;
}
1-28
回傳值之處理
一般的函數(shù)若有回傳值,則會(huì)在函數(shù)呼叫處理完後於
呼叫處產(chǎn)生一個(gè)看不見的暫時(shí)變數(shù)
-函式
int add(int x, int y) { return x+y; }
-呼叫方式
-型態(tài)1:
add(a,b);//將暫時(shí)變數(shù)丟棄
-型態(tài)2:
printf("gcd=%d\n",add(a,b));//將暫時(shí)變數(shù)印出來
-型態(tài)3:
c=add(a,b);//將暫時(shí)變數(shù)拷貝到c
1-29
回傳型態(tài)為參考
回傳值型態(tài)為參考時(shí),於呼叫并不會(huì)產(chǎn)生額外的變數(shù)
,而是使用回傳指令return 後的記憶體
回傳值型態(tài)為參考時(shí),return 不能使用當(dāng)?shù)刈償?shù)與常
數(shù)
回傳值為參考時(shí),可寫在等號(hào)之左邊
例:
int& mydata(int data[], int n){
return data[n];
}
則可寫mydata(5)=2;
1-30
回傳型態(tài)為參考之實(shí)例
#include
using namespace std;
int& access_data(int a[], int n){
return a[n-1];
}
void main(){
int data[]={10,20,30,40,50};
cout << "The fourth data is "<access_data(data,1)=100;
for (int i=0; i<5; i++)
cout<<"data["<必須改定義為
#define sq(x) (x)*(x)
1-32
inline函式
為解決#define 定義函式的缺點(diǎn),在C++中可使用
inline 指令定義函數(shù)
inline傳回值型態(tài)函式名稱(參數(shù)列)
{
// 函數(shù)內(nèi)容
}
C++在程式中遇到呼叫inline函式時(shí),會(huì)去檢查其內(nèi)的
參數(shù)型態(tài)與個(gè)數(shù),并以列內(nèi)函數(shù)展開
inline函式的優(yōu)點(diǎn): 執(zhí)行速度比一般函式快,缺點(diǎn): 無法
像一般函數(shù)精簡(jiǎn)執(zhí)行檔長(zhǎng)度
1-33
inline 函式之實(shí)例
#include
using namespace std;
inline void swap(int&, int&); // 定義swap 函式為inline 函式
void main() {
int i=7, j=-3;
swap(i,j);
cout <<"i = "<< i << endl
<<"j = "<< j << endl;
}
void swap(int& a, int& b) {
int t;
t = a;
a = b;
b = t;
}
1-34
參數(shù)預(yù)設(shè)值
在C++中,函數(shù)之參數(shù)可以有預(yù)設(shè)值.但只有在參數(shù)
列的尾部才可使用.如此一來,可以改變傳入?yún)?shù)的
個(gè)數(shù),毋需對(duì)每一個(gè)輸入?yún)?shù)做設(shè)定
例:
void abc(float k, int j, int i=5)// 對(duì)
void xyz(float k=1,int j, int i) // 錯(cuò)
void err_use(float k, int j=2, int i) // 錯(cuò)

abc(5,3) 與abc(2,4,1) 均可使用
1-35
函式預(yù)設(shè)參數(shù)之實(shí)例
#include
using namespace std;
void fo( int val,
float f = 12.6, // f 的預(yù)設(shè)值為12.6
char c = ‘\n‘, // c 的預(yù)設(shè)值為‘\n‘
string msg = "Error" ) // msg 的預(yù)設(shè)值為"Eerror"
{
return;
}
void main() {
fo( 14, 48.3f, ‘\t‘, "OK" );
fo( 14, 48.3f, ‘\t‘ );
fo( 14, 48.3f );
fo( 14 );
}
1-36
函式預(yù)設(shè)參數(shù)之實(shí)例(4個(gè)以下整數(shù)之相加)
#include
using namespace std;
int add( int x, int y= 0, int z = 0, int w=0) {
return x+y+z+w;
}
int main() {
cout << " addition of 8,6,4,15 is "<cout << " addition of 8,6,4 is "<< add( 8, 6, 4);
cout << " addition of 8,6 is "<< add( 8, 6);
cout << " addition of 8 is "<< add(8);
return 0;
}
1-37
重載(overloading)
C++允許同一個(gè)名稱的函數(shù)可擁有不同的參數(shù)型態(tài),
由呼叫處之函式輸入?yún)?shù)型態(tài)決定執(zhí)行哪一個(gè)函式
對(duì)於同一名稱之函式必須使得呼叫該函式者能唯一決

1-38
重載之實(shí)例(找最大值)
#include
#include
using namespace std;
int imax(int a, int b, int c=-INT_MAX, int d=-INT_MAX){
int max_one=(a>b) a:b;
max_one=(max_one>c) max_one:c;
max_one=(max_one>d) max_one:d;
return max_one;
}
int imax(int a[],int n){ // 計(jì)算陣列中最大的元素
int max_one=a[0];
for (int i=1; imax_one) max_one=a[i];
return max_one;
}
1-39
重載之實(shí)例(找最大值)
void main(){
int x=120, y=500, z=320, w=2000;
cout <<"Max of (x,y,z)="<< imax(x,y,z)<cout <<"Max of (x,y,z,w)="<< imax(x,y,z,w)<int a[]={100, 200, 350, 140, 20};
cout <<"Max of array a="<< imax(a,5)<}
1-40
動(dòng)態(tài)記憶體配置(dynamic memory allocation)
動(dòng)態(tài)記憶體配置提供程式設(shè)計(jì)者依所需求取得與釋回
系統(tǒng)之記憶體
在C中可使用的動(dòng)態(tài)記憶體函數(shù)
-配置函式malloc, calloc, realloc(重新配置)
-釋回函式free
-使用動(dòng)態(tài)記憶體之函數(shù)時(shí)需加入#include
在C++中可使用的動(dòng)態(tài)記憶體函數(shù)
-配置new
-釋回delete
1-41
new 的用法
new 可以從自由記憶體中分配一定量之記憶體,用法
如下:
-用法一:記憶體指標(biāo)= new 資料型態(tài);
用於配置一個(gè)資料型態(tài)大小的記憶體
例: float *buf;
buf=new float; // 配置1個(gè)單精準(zhǔn)度浮點(diǎn)數(shù)
-用法二:記憶體指標(biāo)= new 資料型態(tài)[陣列數(shù)目] ;
用於配置數(shù)個(gè)資料型態(tài)大小的記憶體
例:int*p;
p=new int[20]; // 配置20個(gè)整數(shù)
1-42
delete 的用法
delete 可以釋放一塊由new所配置之記憶體,用法如

-用法一:delete 記憶體指標(biāo);
用於釋回一個(gè)資料型態(tài)大小的記憶體
float *buf;
delete buf; // 釋回buf=new float 的記憶體
-用法二: delete [] 記憶體指標(biāo);
用於釋回?cái)?shù)個(gè)資料型態(tài)大小的記憶體
int *p;
delete[] p; //釋回p=new float[] 的記憶體
1-43
new與malloc的差別
malloc 與new 對(duì)於基本資料型態(tài)(如int, char, double,
float)之配置,用途相同,僅語法不同
malloc 與free 為C之動(dòng)態(tài)記憶體配置指令,在使用時(shí)
不會(huì)去執(zhí)行建構(gòu)(constructor )與解構(gòu)(destructor)
new 與delete 為C++之動(dòng)態(tài)記憶體配置指令,在使用
時(shí)會(huì)去執(zhí)行建構(gòu)(constructor )與解構(gòu)(destructor)
new 與delete 指令對(duì)於陣列之配置或釋回,會(huì)依據(jù)所
配置或釋回之陣列元素個(gè)數(shù)而決定其建構(gòu)
(construdctor)或解構(gòu)子(destructor)之執(zhí)行次數(shù)值
1-44
例外處理(exception handling)
程式在執(zhí)行時(shí),常會(huì)遇到一些問題使程式無法往下執(zhí)

例外發(fā)生的情況有
-開啟一個(gè)不存在的檔案
-系統(tǒng)記憶體不夠使用
-讀寫檔案時(shí)硬碟毀損
-算術(shù)運(yùn)算產(chǎn)生overflow或underflow
1-45
例外處理的語法
語法
try{
/* 要檢查的exception */
}
catch(例外之情形){
/* 處理例外的方法*/
}
throw之用法
-丟出一個(gè)例外參數(shù)出來,讓catch 去取得
1-46
例外處理之實(shí)例(配置動(dòng)態(tài)記憶體)
#include
using namespace std;
void main()
{
int *p;
try{
p=new int;
}
catch (bad_alloc){
cerr<< "Memory allocation\n";
exit(1);
}
}
1-47
函式中之例外
在函式中要將例外往外丟,可在函數(shù)prototype中的參
數(shù)後面加上throw
-例: double hmean(double a, double b) throw (char *);
往外丟出多個(gè)例外
-例: double multierr(double z) throw(char *,double);
不丟出例外
-例: double simple(double z) throw();
可丟出例外,亦可不丟
-例: double simple(double z);
1-48
例外處理之實(shí)例(調(diào)和平均數(shù))
#include
double hmean(double a, double b);
int main(){
double x, y, z;
cout <> x >> y) {
try { z = hmean(x,y); }
catch (char * s) {
cout << s << "\n";
cout << "Enter a new pair of numbers: ";
continue;
}
cout << "Harmonic mean of " << x << " and " << y<< " is " << z << "\n";
cout << "Enter next set of numbers : ";
}
1-49
例外處理之實(shí)例(調(diào)和平均數(shù))
cout << "Bye!\n";
return 0;
}
double hmean(double a, double b){
if (a == -b)
throw "bad arguments: a = -b not allowed";
// 在函式中丟出例外事件
return 2.0 * a * b / (a + b);
}
1-50
轉(zhuǎn)型
C++中提供兩種型態(tài)轉(zhuǎn)換的方式
-第一種: C語言之標(biāo)準(zhǔn)型態(tài)
-語法: (type) expression
-例:
y=(double)x; //將x轉(zhuǎn)換成double型式傳給y
-第二種: 函數(shù)形式
-語法: type(expression)
-例:
y=double(x); //將x轉(zhuǎn)換成double型式傳給y
1-51
新的轉(zhuǎn)型方式
C++提供四種新的轉(zhuǎn)型方式
-static_cast
-dynamic_cast
-const_cast
-reinterpret_cast
1-52
static_cast
static_cast 於編譯時(shí)作轉(zhuǎn)型動(dòng)作,dynamic_cast 於執(zhí)
行時(shí)作轉(zhuǎn)型動(dòng)作
語法
static_cast(object to convert)

int z = 3;
float x = static_cast(z);
1-53
const_cast
const_cast 能夠改變const 屬性

const int* find(int val, const int t[], int n){
for (int i=0; iif (t[i]==val) return &t[i];
return 0;
}
主程式
int* p; int a[]={2,4,6};
p=const_cast(find(4,a,3));
1-54
reinterpret_cast
reinterpret_cast 不理會(huì)記憶體之資料型態(tài),直接依記
憶體內(nèi)容做轉(zhuǎn)型
例:
int a;
int *p;
a=reinterpret_cast(p);
1-55
新的資料型態(tài)bool
傳統(tǒng)C語言使用整數(shù)(int) 來代表布林值.當(dāng)整數(shù)不等
於0時(shí),布林值為『真』;等於0時(shí)布林值為『偽』
C++ 提供新的資料型態(tài)bool來代表布林值.使用bool
較int節(jié)省記憶體
bool使用保留字true 代表為『真』,使用保留字false
代表布林值為『偽』
bool 變數(shù)做輸出時(shí),無法直接輸出true或false值,而
用1或0做取代
1-56
使用bool 之實(shí)例
#include
using namespace std;
void main(){
bool a[5]; // visual c, 1 bool = 1 byte
int b[5]; // visual c , 1 int =4 byte
a[0]=true; // 直接設(shè)成true
a[1]=false; // 直接設(shè)成false
int x;
cout<>x;
a[2]=(x>5); // 判斷而決定為true 或false
for (int i=0; i<3; i++) {
if (a[i]) cout << "a["<<i<<"] is True, ";
else cout<<"False, ";
cout <}
1-57
使用bool 之實(shí)例
// 整數(shù)變數(shù)
b[0]=100; // 0 true
b[1]=0; // =0 false
b[2]=(x>5);
for (i=0; i<3; i++){
if (b[i]) cout << "b["<<i<<"] is True, ";
else cout<<"False, ";
cout<}
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多