




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
概述
C++是一門(mén)混合型面向?qū)ο蟪绦蛟O(shè)計(jì)語(yǔ)言,它兼容了C語(yǔ)言并彌補(bǔ)了其缺陷,增加了面身對(duì)象的能力。其中,改造后的C語(yǔ)言是面向?qū)ο蟛糠值幕A(chǔ)。一般語(yǔ)法的改進(jìn)指針和引用函數(shù)的改進(jìn)類(lèi)和對(duì)象構(gòu)造函數(shù)和析構(gòu)函數(shù)C++程序的開(kāi)發(fā)過(guò)程
C++語(yǔ)言是一種高級(jí)程序設(shè)計(jì)語(yǔ)言,它的開(kāi)發(fā)過(guò)程與其他高級(jí)語(yǔ)言程序開(kāi)發(fā)過(guò)程類(lèi)似,一般要經(jīng)過(guò)四個(gè)步驟:
執(zhí)行編輯編譯鏈接編輯 是指把按照C++語(yǔ)法規(guī)則編寫(xiě)的程序代碼通過(guò)編輯器(BorlandC++,VisualC++,TurboC++等)輸入計(jì)算機(jī),并存盤(pán)。在存盤(pán)時(shí),C++源文件的擴(kuò)展名為.CPP。編譯 將編輯好的C++源程序通過(guò)編譯器轉(zhuǎn)換為目標(biāo)文件(OBJ文件)。即生成該源文件的目標(biāo)代碼。C++程序的開(kāi)發(fā)過(guò)程鏈接 將用戶(hù)程序生成的多個(gè)目標(biāo)代碼文件(.obj)和系統(tǒng)提供的庫(kù)文件(.lib)中的某些代碼連接在一起,生成一個(gè)可執(zhí)行文件(.exe)。執(zhí)行 把生成的可執(zhí)行文件運(yùn)行,在屏幕上顯示運(yùn)行結(jié)果。用戶(hù)可以根據(jù)運(yùn)行結(jié)果來(lái)判斷程序是否出錯(cuò)。C++程序的開(kāi)發(fā)過(guò)程C++程序的開(kāi)發(fā)過(guò)程書(shū)寫(xiě)格式
C++語(yǔ)言程序的書(shū)寫(xiě)格式自由度高,靈活性強(qiáng),隨意性大,如一行內(nèi)可寫(xiě)一條語(yǔ)句,也可寫(xiě)幾條語(yǔ)句;一個(gè)語(yǔ)句也可分寫(xiě)在多行內(nèi)。不過(guò)應(yīng)采用適當(dāng)?shù)母袷綍?shū)寫(xiě),便于人們閱讀和理解。 為了增加程序的可讀性和利于理解,編寫(xiě)程序時(shí)按如下要點(diǎn)書(shū)寫(xiě):
(1)一般情況下每個(gè)語(yǔ)句占用一行。 (2)不同結(jié)構(gòu)層次的語(yǔ)句,從不同的起始位置開(kāi)始,即在同一結(jié)構(gòu)層次中的語(yǔ)句,縮進(jìn)同樣的字?jǐn)?shù)。 (3)表示結(jié)構(gòu)層次的大括弧,寫(xiě)在該結(jié)構(gòu)化語(yǔ)句第一個(gè)字母的下方,與結(jié)構(gòu)化語(yǔ)句對(duì)齊,并占用一行。 (4)適當(dāng)加些空格和空行。C++程序的開(kāi)發(fā)過(guò)程C++語(yǔ)言程序由以下基本部分組成。1.函數(shù)一個(gè)C++程序是由若干個(gè)函數(shù)構(gòu)成的。函數(shù)分為庫(kù)函數(shù)(標(biāo)準(zhǔn)函數(shù))和自定義函數(shù)。庫(kù)函數(shù)一般是由系統(tǒng)提供的。一個(gè)完整的C++語(yǔ)言程序只有一個(gè)主函數(shù)。2.預(yù)處理命令預(yù)處理命令以位于行首的符號(hào)“#”開(kāi)始,C++提供的預(yù)處理有宏定義命令、文件包含命令和條件編譯命令三種。2.1C++程序的開(kāi)發(fā)過(guò)程3.程序語(yǔ)句一條完整的語(yǔ)句必須以分號(hào)“;”結(jié)束。程序語(yǔ)句有如下幾類(lèi):(1)說(shuō)明語(yǔ)句
用來(lái)說(shuō)明變量的類(lèi)型和初值。 如下面語(yǔ)句是把變量說(shuō)明為浮點(diǎn)數(shù)。
floata,b,c;
又如下面語(yǔ)句是把變量sum說(shuō)明為整型變量,并賦初值為零。
intsum=0;
C++程序的開(kāi)發(fā)過(guò)程(2)表達(dá)式語(yǔ)句
由一個(gè)表達(dá)式構(gòu)成一個(gè)語(yǔ)句,用以描述算術(shù)運(yùn)算、邏輯運(yùn)算、或產(chǎn)生某種特定動(dòng)作,在任何表達(dá)式最后加一個(gè)分號(hào)就構(gòu)成了一個(gè)語(yǔ)句。如下例由賦值表達(dá)式加“;”就構(gòu)成一個(gè)賦值表達(dá)式語(yǔ)句。(3)程序控制語(yǔ)句
用來(lái)描述語(yǔ)句的執(zhí)行條件與執(zhí)行順序的語(yǔ)句,C++語(yǔ)言的控制語(yǔ)句有9種,如下頁(yè)所示。其語(yǔ)句中的括號(hào)()表示其中是條件,~表示內(nèi)嵌的語(yǔ)句。C++程序的開(kāi)發(fā)過(guò)程if
()~else
條件語(yǔ)句for
()~ 循環(huán)語(yǔ)句while
()~ 循環(huán)語(yǔ)句do~while
() 循環(huán)語(yǔ)句continue
結(jié)束本次循環(huán)語(yǔ)句break
中止循環(huán)式(switch語(yǔ)句)switch
多分支選擇語(yǔ)句goto
轉(zhuǎn)移語(yǔ)句return
從函數(shù)返回語(yǔ)句C++程序的開(kāi)發(fā)過(guò)程(4)復(fù)合語(yǔ)句
復(fù)合語(yǔ)句是一種十分重要的語(yǔ)句,由大括號(hào){和}把一些說(shuō)明和語(yǔ)句組合在一起,使它們?cè)谡Z(yǔ)法上等價(jià)于一個(gè)簡(jiǎn)單語(yǔ)句;可由若干簡(jiǎn)單語(yǔ)句或復(fù)合語(yǔ)句組成。
例如:if(a>b) {c=a-b; d=c*a; }
else {c=a+b; d=c*b; }2.1C++程序的開(kāi)發(fā)過(guò)程2.1C++語(yǔ)言的特點(diǎn)
C++是Bjarne
Stroustrup
在貝爾實(shí)驗(yàn)室開(kāi)發(fā)的一種語(yǔ)言,是C語(yǔ)言的超集和擴(kuò)展。C++保留了C語(yǔ)言的有效性、靈活性、便于移植等特點(diǎn),又添加了對(duì)面向?qū)ο蟮闹С?。由C語(yǔ)言擴(kuò)展而來(lái),全面兼容C語(yǔ)言2.2C++語(yǔ)言文件擴(kuò)展名C語(yǔ)言源文件擴(kuò)展名.cC++語(yǔ)言源文件擴(kuò)展名.cpp相關(guān)頭文件擴(kuò)展名.h2.3注釋符號(hào)2.4名字空間
C++里引入namespace的目的就是為了避免污染全局名字空間,簡(jiǎn)單地說(shuō),就是為了避免和減少命名沖突。一旦一個(gè)程序?qū)懘罅?,就很難避免重名,特別是多人合作的情況下。解決相同的函數(shù)名,或變量名,或者兩個(gè)不同的庫(kù)里面有相同的函數(shù)名,相當(dāng)于一個(gè)文件域.2.4名字空間定義namespacens1{floata,b,c;fun1(){….}}使用域內(nèi)成員,需要使用域操作符號(hào)“::”
ns1::a;ns1::fun1();2.4名字空間分層嵌套定義namespacens1{namespacens2{classmatrix{…}}}
訪(fǎng)問(wèn)matrix,可寫(xiě)成n1::n2::matrix。全局名字空間2.4名字空間使用使用關(guān)鍵字using
usingns1::ns2::martix
以后在再次使用martix,可以直接使用使用usingnamespace名字空間名空間所有成員都可以被直接使用usingnamespacestdc++所有組件都在std中,通過(guò)該語(yǔ)句則可以使用標(biāo)準(zhǔn)C++庫(kù)所有成員,注意此時(shí)若使用include包含頭文件,要去掉頭文件擴(kuò)展名2.5c++語(yǔ)言的輸入輸出CC++見(jiàn):p18例2.12.6變量的定義變量初始化
C++支持兩種形式的初始化第一種形式是使用賦值操作符的顯式語(yǔ)法形式
int
ival=1024;stringproject="Fantasia2000";在隱式形式中初始值被放在圓括號(hào)中
int
ival(1024);stringproject("Fantasia2001");
在這兩種情況中ival
都被初始化為1024而project的初始值為Fantasia20002.6變量的定義變量定義可以位于程序中的任何地方2.6變量的定義允許直接使用結(jié)構(gòu)體名定義變量
structstudent{
intno;floatmath;}studentwang;2.7強(qiáng)制類(lèi)型轉(zhuǎn)換強(qiáng)制轉(zhuǎn)換類(lèi)型C的強(qiáng)制類(lèi)型轉(zhuǎn)換法(類(lèi)型名)表達(dá)式函數(shù)表示法(僅限簡(jiǎn)單類(lèi)型)
類(lèi)型名(表達(dá)式)
inti=int(1.35);動(dòng)態(tài)內(nèi)存分配(Dynamicmemoryallocation)靜態(tài)與動(dòng)態(tài)內(nèi)存分配的兩個(gè)主要區(qū)別是
靜態(tài)對(duì)象是有名字的變量,直接對(duì)其進(jìn)行操作。而動(dòng)態(tài)對(duì)象是沒(méi)有名字的變量,通過(guò)指針間接地對(duì)它進(jìn)行操作。靜態(tài)對(duì)象的分配與釋放由編譯器自動(dòng)處理,程序員需要理解這一點(diǎn),但不需要做任何事情。相反,動(dòng)態(tài)對(duì)象的分配與釋放必須由程序員顯式地管理,相對(duì)來(lái)說(shuō)比較容易出錯(cuò)它通過(guò)new
和delete
兩個(gè)運(yùn)算符來(lái)完成。2.8動(dòng)態(tài)內(nèi)存的分配與釋放動(dòng)態(tài)內(nèi)存分配(Dynamicmemoryallocation)malloc(),calloc()和free().CC++2.8動(dòng)態(tài)內(nèi)存的分配與釋放
new
運(yùn)算符動(dòng)態(tài)分配堆內(nèi)存使用形式:
指針變量=new類(lèi)型(常量); 指針變量=new類(lèi)型[表達(dá)式
];
作用:從堆分配一塊“類(lèi)型”大小的存儲(chǔ)空間,返回首地址
其中:“常量”是初始化值,可缺省
創(chuàng)建數(shù)組對(duì)象時(shí),不能為對(duì)象指定初始值
delete
運(yùn)算符釋放已分配的內(nèi)存空間使用形式:
delete指針變量;
delete[]指針變量;
其中:“指針變量”必須是一個(gè)new返回的指針2.8new和delete操作符int*p1=newint; char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;2.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p12.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p1p22.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p1p2p34.5.1new和delete操作符2.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p1p2p3p44.5.1new和delete操作符2.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p2p3p4p14.5.1new和delete操作符2.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p2p3p4p14.5.1new和delete操作符2.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p2p3p4p14.5.1new和delete操作符2.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p3p4p2p12.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p3p4p2p12.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p4p2p1p32.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p4p2p1p32.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p2p1p3p42.8new和delete操作符int*p1=newint;char*p2=newchar;float*p3=newfloat;int*p4=newint[4];……deletep1;deletep2;deletep3;delete[]p4;p2p1p3p42.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL;p=newint(89); //初始化存儲(chǔ)區(qū)
if(p==NULL) {cout<<"allocationfaiure\n";return;}
cout<<*p;deletep;}例用new算符建立簡(jiǎn)單對(duì)象2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL;p=newint(89); //初始化存儲(chǔ)區(qū)
if(p==NULL) {cout<<"allocationfaiure\n";return;}
cout<<*p;deletep;}p例用new算符建立簡(jiǎn)單對(duì)象2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL;
p=newint(89);
//初始化存儲(chǔ)區(qū)
if(p==NULL) {cout<<"allocationfaiure\n";return;}
cout<<*p;deletep;}p89例用new算符建立簡(jiǎn)單對(duì)象2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL;p=newint(89); //初始化存儲(chǔ)區(qū)
if(p==NULL)
{cout<<"allocationfaiure\n";return;}
cout<<*p;deletep;}p89例用new算符建立簡(jiǎn)單對(duì)象4.5.1new和delete操作符#include<iostream.h>voidmain(){int*p=NULL;p=newint(89); //初始化存儲(chǔ)區(qū)
if(p==NULL) {cout<<"allocationfaiure\n";return;}
cout<<*p;deletep;}89對(duì)象無(wú)名只能通過(guò)指針訪(fǎng)問(wèn)例用new算符建立簡(jiǎn)單對(duì)象p892.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL;p=newint(89); //初始化存儲(chǔ)區(qū)
if(p==NULL) {cout<<"allocationfaiure\n";return;}
cout<<*p;
deletep;}p8989例用new算符建立簡(jiǎn)單對(duì)象2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL;p=newint(89); //初始化存儲(chǔ)區(qū)
if(p==NULL) {cout<<"allocationfaiure\n";return;}
cout<<*p;
deletep;}8989例用new算符建立簡(jiǎn)單對(duì)象p2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pti2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;
p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pti2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];
if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pti2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}
for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pti1001011021031041051061071081092.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pt10i100101102103104105106107108109|2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;
for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pt100101102103104105106107108109|10i#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pt100101102103104105106107108109|10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pt100101102103104105106107108109100|10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;
for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pt100101102103104105106107108109100|10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pt100101102103104105106107108109100|10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組pt100101102103104105106107108109100101|10i#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;
for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;
for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;
for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;
for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102103|t10i#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102103104105106107108109|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102103104105106107108109|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102103104105106107108109|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;
delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102103104105106107108109|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;
delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109100101102103104105106107108109|t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109t10i2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)
p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109t10i以下標(biāo)方式訪(fǎng)問(wèn)動(dòng)態(tài)數(shù)組2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)
p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109t10ii是偏移量p
指針本身值不變2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109t10i跟蹤指針賦初值2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109t10i跟蹤指針移動(dòng)2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p100101102103104105106107108109t10i間址訪(fǎng)問(wèn)元素2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p10010110210310410510610710810910ifor(i=0;i<10;i++){cout<<*p<<"";
p++;}注意結(jié)束循環(huán)后2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=newint[10];if(p==NULL){cout<<"allocationfaiure\n";return;}for(i=0;i<10;i++)p[i]=100+i;
cout<<endl;for(t=p;t<p+10;t++)
cout<<*t<<"";
cout<<endl;
delete[]p;}例用new算符建立動(dòng)態(tài)數(shù)組p10010110210310410510610710810910ifor(i=0;i<10;p++)
cout<<*p<<"";注意結(jié)束循環(huán)后for(i=0;i<10;i++){cout<<*p<<"";
p++;}釋放什么空間?2.8new和delete操作符#include<iostream.h>voidmain(){int*p=NULL,*t;inti;p=ne
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 殘疾人拓展活動(dòng)方案
- 母親生日蛋糕活動(dòng)方案
- 水上冷飲活動(dòng)方案
- 植物教學(xué)活動(dòng)方案
- 武陟活動(dòng)策劃方案
- 殘疾宣傳活動(dòng)方案
- 民國(guó)舞蹈藝術(shù)節(jié)活動(dòng)方案
- 民歌大賽征稿活動(dòng)方案
- 正式車(chē)輛維修活動(dòng)方案
- 疫情防控期間實(shí)驗(yàn)室人員外出審批流程
- 高二【數(shù)學(xué)(人教A版)】用空間向量研究距離、夾角問(wèn)題(2)-教學(xué)設(shè)計(jì)
- 中頻藥物透入治療
- 四好農(nóng)村路培訓(xùn)
- 《瀝青及瀝青混合料》課件
- 醫(yī)療物聯(lián)網(wǎng)標(biāo)準(zhǔn)與規(guī)范-洞察分析
- 綜合機(jī)電供應(yīng)及安裝專(zhuān)業(yè)分包工程機(jī)電系統(tǒng)調(diào)試方案
- 城市軌道交通車(chē)輛智慧運(yùn)維系統(tǒng)技術(shù)規(guī)范
- 信息安全保密三員培訓(xùn)
- 《金屬材料檢測(cè)技術(shù)》課件
- GB/T 25085.1-2024道路車(chē)輛汽車(chē)電纜第1部分:術(shù)語(yǔ)和設(shè)計(jì)指南
- 更換閥門(mén)施工方案
評(píng)論
0/150
提交評(píng)論