![C高級(jí)編程練習(xí)題(共44頁)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/65402371-f14c-4619-999a-aa5cc63feb99/65402371-f14c-4619-999a-aa5cc63feb991.gif)
![C高級(jí)編程練習(xí)題(共44頁)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/65402371-f14c-4619-999a-aa5cc63feb99/65402371-f14c-4619-999a-aa5cc63feb992.gif)
![C高級(jí)編程練習(xí)題(共44頁)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/65402371-f14c-4619-999a-aa5cc63feb99/65402371-f14c-4619-999a-aa5cc63feb993.gif)
![C高級(jí)編程練習(xí)題(共44頁)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/65402371-f14c-4619-999a-aa5cc63feb99/65402371-f14c-4619-999a-aa5cc63feb994.gif)
![C高級(jí)編程練習(xí)題(共44頁)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/22/65402371-f14c-4619-999a-aa5cc63feb99/65402371-f14c-4619-999a-aa5cc63feb995.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上1 C+語言基礎(chǔ)及過程化程序設(shè)計(jì)1.1 基礎(chǔ)概念1. 函數(shù)聲明和函數(shù)定義有什么區(qū)別?答:1) 函數(shù)聲明是函數(shù)的原型,強(qiáng)調(diào)函數(shù)如何被使用,不包含函數(shù)的實(shí)現(xiàn)代碼;2) 函數(shù)定義給出函數(shù)的實(shí)現(xiàn)代碼。2. const char *p1; char * const p2;的區(qū)別答:1) const位于星號(hào)的左側(cè), const用來修飾指針?biāo)赶虻淖兞?,即指針指向?yàn)槌A浚?)const位于星號(hào)的右側(cè),const用來修飾指針本身,即指針本身是常量。3. delete與 delete 區(qū)別答:delete只會(huì)調(diào)用一次析構(gòu)函數(shù),而delete會(huì)調(diào)用動(dòng)態(tài)分配的多個(gè)對象的析構(gòu)函數(shù)4. 解釋堆
2、和棧的區(qū)別答:1) 棧:由編譯器自動(dòng)分配釋放,存放函數(shù)的參數(shù)、局部變量等。通常在超出作用域后由系統(tǒng)自動(dòng)釋放。2) 堆:一般由程序員負(fù)責(zé)分配與釋放,若程序員不釋放,占用的內(nèi)存直到程序結(jié)束才由OS回收。5. 在什么時(shí)候需要使用“常引用”?答:如果既要利用引用提高程序的效率,又要保護(hù)傳遞給函數(shù)的數(shù)據(jù)不在函數(shù)中被改變,就應(yīng)使用常引用。6. 全局變量和局部變量在內(nèi)存中的區(qū)別。答:1) 全局變量儲(chǔ)存在靜態(tài)數(shù)據(jù)區(qū),程序加載時(shí)分配并初始化,程序結(jié)束時(shí)釋放;2) 局部變量在棧中,進(jìn)入變量作用域后分配,超出其作用域后釋放;3) 全局變量不初始化會(huì)執(zhí)行缺省初始化,如整型變量缺省初始化為0,局部變量不初始化不會(huì)執(zhí)行缺
3、省初始化,往往為垃圾值。7. 簡述內(nèi)存的分配方式。答:1) 靜態(tài)存儲(chǔ)區(qū),是在程序編譯時(shí)就已經(jīng)分配好的,在整個(gè)運(yùn)行期間都存在,如全局變量、常量。2) 棧上分配,函數(shù)內(nèi)的局部變量和形參在棧上分配。3) 堆上分配,動(dòng)態(tài)分配,用new分配內(nèi)存,用delete來釋放內(nèi)存。8. 指針的幾種典型應(yīng)用情況。int *pn;-指針數(shù)組,每個(gè)元素均為指向整型數(shù)據(jù)的指針。int (*p) n;-指向一維數(shù)組的指針,這個(gè)一維數(shù)組含有n個(gè)整型數(shù)據(jù)。int *p();-返回指向整型指針的函數(shù)。int (*p) ();-指向函數(shù)的指針,要求函數(shù)無參并返回整型。 9. 說明0、0、0、“0”的區(qū)別答:0表示整數(shù)常量,值為0;
4、0表示ASCII碼值為0的字符常量;0表示ASCII碼值為48的字符常量;“0”為字符串常量,其中包含0和0兩個(gè)字符。10. 說明下面程序中存在的問題#include<iostream>int main()int arr10, *p=arr;int i;for( ; p<arr+10;+p)cin>>*p;for(; a<p;+arr)cout<<*arr<<0;return 0;答:arr為數(shù)組名,對應(yīng)地址不可修改,不能應(yīng)用+arr運(yùn)算。11. 有如下定義,請寫出訪問a23元素的不同方法int a45;int (*p)5 = a;答
5、:a23、p23、*(a2+3)、*(p2+3)、*(*(a+2)+3)、*(*(p+2)+3)1.2 閱讀程序1. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int main()double numOne = 2.5;int numTwo = 3;double quotient = numOne/2;cout<<"Quotient: "<<quotient<<endl;quotient = numTwo/2;cout<<"Quotient: &
6、quot;<<quotient<<endl;return 0;2. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int main()int number = 103;int digit, tens, hundreds;digit = number %10;tens = (number/10)%10;hundreds = (number/100)%10;cout<<"Hundreds: "<<hundreds<<", Tens: &quo
7、t;<<tens<<", Digit: "<<digit<<endl;return 0;3. 運(yùn)行下面的程序3次,分別輸入90、78、60,寫出每次程序執(zhí)行的輸出結(jié)果。#include <iostream>using namespace std;int main()int grade;cout<<"Enter a grade(1-100): "cin>>grade;if(grade>=85)cout<<"Excellentn"else
8、 if(70<=grade<85)cout<<"Passn"elsecout<<"Failn"return 0;4. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;bool check( int score, int baseLine)if( score >= baseLine )return true;return false;bool check(int score, int baseLine = 60);int main()int scor
9、e=65;if( check(score) = true)cout<<"Passed!n"elsecout<<"Failed!n"if( check(score, 70) = true)cout<<"Passed!n"elsecout<<"Failed!n"return 0;5. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int fun(int a);double fun(double a);ch
10、ar fun(char a);int main()cout<<fun(3)<<endl;cout<<fun(3.6)<<endl;cout<<fun('A')<<endl;cout<<fun('g')<<endl;return 0;int fun(int a)return a/2; double fun(double a)return a/2; char fun(char a)char result=a;if(a>='a'&& a
11、<='z')result=a-32;if(a>='A'&& a<='Z')result=a+32;return result;6. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int gcd(int m, int n)if(n=0)return m;return gcd(n, m%n);int main()cout<<"1:"<<gcd(20,8)<<endl;cout<<&q
12、uot;2:"<<gcd(36,64)<<endl;return 0;7. 寫出下面程序的運(yùn)行結(jié)果,假定輸入"Hello_123"。#include <iostream>using namespace std;int main()char word50;cout<<"Enter a word:"cin>>word;for(int i=0; wordi!='0' +i)if(wordi>='a' && wordi<= '
13、z' )wordi-= 32;cout<<"Upper case: "<<word<<endl;return 0;8. 寫出下面程序的運(yùn)行結(jié)果,假定輸入"Hello123_World"。#include <iostream>using namespace std;int main()char word50;cout<<"Enter a string:"cin>>word;int pos=0;for(int i=0; wordi!='0' +
14、i)if(wordi<'0' | wordi>'9' )wordpos=wordi;+pos;wordpos='0'cout<<"result: "<<word<<endl;return 0;9. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int main() int i,j; for(i=0;i<5;i+) for(j=i;j<5;j+) cout<<" *" c
15、out<<endl; return 0;10. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int sum( int a, int b=1, int c=3 )return a+b+c;int main()int sum(int a, int b=3, int c=4);cout<<sum (2)<<endl;cout<<sum (2,5)<<endl;cout<<sum (2,3,6)<<endl; return 0;11. 寫出下面程序的
16、運(yùn)行結(jié)果。#include <iostream>using namespace std;char & elem(char *s, int n)return sn;int main()char str="HelloWorld"elem(str,1)= 'A'cout<<str<<endl; return 0;12. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int x=10; int main()int x=15;cout<<x<
17、;<endl; cout<<:x<<endl; return 0;13. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;void xhg(int *a,int *b)int *tmp;tmp=b; b=a; a=tmp;cout<<*a<<' '<<*b<<endl;int main()int x(5),y(4);xhg(&x,&y);cout<<x<<' '<<y&
18、lt;<endl;return 0;14. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;void xhg(int &a,int &b)int tmp;tmp=b; b=a; a=tmp;cout<<a<<' '<<b<<endl;int main()int x(5),y(4);xhg(x,y);cout<<x<<' '<<y<<endl;return 0;15. 寫出下面程序的
19、運(yùn)行結(jié)果。#include <iostream>using namespace std;int ff(int *a,int size)if(size=1)return a0;return asize-1+ff(a,size-1);int main()int a5=1,2,3,4,5;cout<<“result:”<<ff(a,5)<<endl;return 0;16. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;void f(const string& s,int n)
20、cout<<sn-1;if(n>1)f(s,n-1);int main()f("animal",6);cout<<endl;f("hello",3);return 0;17. 寫出下面程序的運(yùn)行結(jié)果。#include <iostream>using namespace std;int func(int data,int size)int a=data0;int b=data0;for(int i=1;i<size;+i)if(datai>a) a=datai;if(datai<b) b=data
21、i;return a-b;int main()int a=9,3,2,-1,8,0,4;cout<<func(a,7)<<endl;cout<<func(a+2,4)<<endl;return 0;18. 寫出下面程序的執(zhí)行結(jié)果。#include <iostream>using namespace std;int fun(int interval) int sum=0, i=0; for(i=0; i<100; i+=interval) sum+=i; return sum;int main()cout<<"
22、;Result: "<<fun(2)<<endl;return 0;19. 寫出下面程序的執(zhí)行結(jié)果。#include <iostream>using namespace std;double func( double pData , int size);int main()double array=2.2, 3.8, 6, 5.4;cout<<"Result: "<<func(array, 4)<<endl;cout<<"Result: "<<fun
23、c(array, 3)<<endl;return 0;double func( double pData , int size)double result=0;int i;for(i=0; i<size; +i)result+=pDatai;result /= size;return result;2 面向?qū)ο蟪绦蛟O(shè)計(jì)2.1 基礎(chǔ)概念1、定義形式如下的類,C+編譯器自動(dòng)為該類產(chǎn)生的四個(gè)缺省函數(shù)是什么?寫出其原型。class MyClass public:void SetX(int);private:int x;答:無參構(gòu)造函數(shù),拷貝構(gòu)造函數(shù),析構(gòu)函數(shù),賦值運(yùn)算符函數(shù)。MyCl
24、ass();MyClass(const MyClass& );MyClass();MyClass& operator=(const MyClass& );2、定義形式如下的類,寫出C+編譯器自動(dòng)生成的拷貝構(gòu)造函數(shù)和賦值運(yùn)算符函數(shù)的定義。class MyClass public:void SetX(int);private:int x;答:MyClass(const MyClass& a) x=a.x; MyClass& MyClass:operator=(const MyClass& a) x=a.x; return *this; 3、拷貝構(gòu)造函
25、數(shù)在哪幾種情況下會(huì)被調(diào)用?答:1)當(dāng)類的一個(gè)對象去初始化該類的另一個(gè)對象時(shí);2)如果函數(shù)的形參是類的對象,調(diào)用函數(shù)進(jìn)行形參和實(shí)參結(jié)合時(shí);3)如果函數(shù)的返回值是類對象,函數(shù)調(diào)用完成返回時(shí)。4、構(gòu)造函數(shù)與普通成員函數(shù)相比有什么不同? 答:1)構(gòu)造函數(shù)是類的一種特殊成員函數(shù),一般情況下,它是專門用來初始化數(shù)據(jù)成員的。2)構(gòu)造函數(shù)的名字必須與類名相同,它不具有任何返回類型。構(gòu)造函數(shù)在創(chuàng)建對象時(shí)由系統(tǒng)自動(dòng)調(diào)用。5、創(chuàng)建派生類對象時(shí),構(gòu)造函數(shù)的調(diào)用順序是什么?答:1)先調(diào)用基類構(gòu)造函數(shù);2)按定義順序初始化對象數(shù)據(jù)成員;3)最后調(diào)用本類的構(gòu)造函數(shù)。6、哪幾種情況必須用到初始化成員列表?答:1)類的常量數(shù)據(jù)
26、成員初始化;2)類的引用成員初始化;3)類的對象成員初始化,而該對象沒有無參構(gòu)造函數(shù);4)基類成員初始化,而基類沒有無參構(gòu)造函數(shù)。7、C+頭文件中通常會(huì)包含哪些內(nèi)容?答:類的定義、常量定義、函數(shù)聲明、全局變量聲明8、什么是常對象?答:常對象是指在任何場合都不能對其成員的值進(jìn)行修改的對象。9、什么叫抽象類?答:包含純虛函數(shù)的類,不能定義抽象類對象,可以定義抽象類的指針或引用,指向或引用派生類對象。10、同類對象間是怎樣實(shí)現(xiàn)數(shù)據(jù)共享的?答:通過類的靜態(tài)數(shù)據(jù)成員來實(shí)現(xiàn)。靜態(tài)數(shù)據(jù)成員屬于類,而不為某個(gè)對象所私有,所有實(shí)例對象共享類的靜態(tài)數(shù)據(jù)成員。11、函數(shù)重載是什么意思?它與虛函數(shù)的概念有什么區(qū)別?答
27、:1) 函數(shù)重載是相同作用域內(nèi)存在多個(gè)同名的函數(shù),編譯系統(tǒng)在編譯階段通過函數(shù)參數(shù)個(gè)數(shù)、參數(shù)類型不同來區(qū)分該調(diào)用哪一個(gè)函數(shù),即實(shí)現(xiàn)的是靜態(tài)的多態(tài)性,但不能僅僅通過函數(shù)返回值不同來實(shí)現(xiàn)函數(shù)重載。2) 虛函數(shù)在基類中通過使用關(guān)鍵字virtual來聲明一個(gè)函數(shù)為虛函數(shù),該函數(shù)的功能可能在將來的派生類中重新定義或者在基類的基礎(chǔ)之上進(jìn)行擴(kuò)展,系統(tǒng)只能在運(yùn)行階段才能動(dòng)態(tài)決定該調(diào)用哪一個(gè)函數(shù),所以實(shí)現(xiàn)的是動(dòng)態(tài)的多態(tài)性。12、函數(shù)重載與函數(shù)覆蓋的區(qū)別?答:1)函數(shù)重載是在相同作用域內(nèi),存在多個(gè)同名的函數(shù),但函數(shù)參數(shù)或參數(shù)類型不同,調(diào)用函數(shù)時(shí)編譯器通過實(shí)參類型匹配某個(gè)函數(shù)版本,屬于靜態(tài)多態(tài)性;2)函數(shù)覆蓋指基類和
28、派生類之間存在同名函數(shù),派生類中的函數(shù)隱藏了基類的同名函數(shù)的現(xiàn)象。13、構(gòu)造函數(shù)和析構(gòu)函數(shù)是否可以被重載,為什么?答:構(gòu)造函數(shù)可以被重載,析構(gòu)函數(shù)不可以被重載。因?yàn)闃?gòu)造函數(shù)可以帶多個(gè)參數(shù),而析構(gòu)函數(shù)不能帶參數(shù)。14、分析正誤:抽象類不能產(chǎn)生實(shí)例,所以不需要有構(gòu)造函數(shù)。答:錯(cuò)。抽象類中可以包含數(shù)據(jù)成員,派生類對象初始化時(shí)需要通過抽象基類的構(gòu)造函數(shù)完成對其數(shù)據(jù)成員的初始化。15、一個(gè)類的構(gòu)造函數(shù)和析構(gòu)函數(shù)什么時(shí)候被調(diào)用,是否需要手工調(diào)用?答:構(gòu)造函數(shù)在創(chuàng)建類對象的時(shí)候被自動(dòng)調(diào)用,析構(gòu)函數(shù)在類對象生命期結(jié)束時(shí)。構(gòu)造函數(shù)和析構(gòu)函不需要手工調(diào)用,由系統(tǒng)自動(dòng)調(diào)用。16、構(gòu)造函數(shù)和析構(gòu)函數(shù)的調(diào)用順序?析構(gòu)函
29、數(shù)為什么要定義為虛函數(shù)?答案:構(gòu)造函數(shù)的調(diào)用順序:基類構(gòu)造函數(shù)對象成員構(gòu)造函數(shù)派生類構(gòu)造函數(shù);析構(gòu)函數(shù)的調(diào)用順序與構(gòu)造函數(shù)相反:派生類析構(gòu)函數(shù)對象成員析構(gòu)函數(shù)基類析構(gòu)函數(shù)。析構(gòu)函數(shù)定義為虛函數(shù)是為了防止析構(gòu)不徹底,造成內(nèi)存泄漏。17、請說出類中private,protect,public三種訪問限制類型的區(qū)別答案:private是私有類型,只有本類中的成員函數(shù)才能訪問;protect是保護(hù)型的,本類和子類成員函數(shù)可以訪問;public是公有類型,本類和子類成員函數(shù)可以訪問,類外部通過對象可以間接訪問。18、Test是一種類類型,現(xiàn)要為其重載前置和后置+運(yùn)算符,寫出它們的原型。答:1)前置+:T
30、est& operator+(); 2)后置+:Test& operator+(int)19、說明組合和繼承在復(fù)用代碼方面的區(qū)別答:組合關(guān)系描述的是“有一種”關(guān)系,一個(gè)對象是另一個(gè)對象的一部分;繼承關(guān)系描述的“是一種”關(guān)系,實(shí)現(xiàn)對象的層次關(guān)系。20、指出Dog類定義中的錯(cuò)誤。#include <iostream>using namespace std;class Dogpublic: Dog() age=1; weight=10; Dog(int a,int w)age=a; weight=w; void play()const cout<<age<
31、;<endl; cout<<weight+<<endl;private: const int age; int weight;答:1) age為常數(shù)據(jù)成員,不能在構(gòu)造函數(shù)體內(nèi)賦值,只能通過初始化列表完成初始化;2) play為常成員函數(shù),不能修改數(shù)據(jù)成員的值。2.2 閱讀程序1、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Base public:void display() cout<<“Base display”<<endl; ;class Derived : pu
32、blic Base public:void display() cout<<“Derived display”<<endl; ;void display(Base & rr)rr.display();int main()Base b;Derived d;display(b);display(d);return 0;2、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Personpublic:Person() cout<<“Person構(gòu)造函數(shù)!”<<endl;Perso
33、n() cout<<“Person被析構(gòu)!”<<endl;class Student : public Personpublic:Student() cout<<“Student構(gòu)造函數(shù)!”<<endl;Student() cout<<“Student被析構(gòu)!”<<endl;class Teacher : public Personpublic:Teacher() cout<<“Teacher構(gòu)造函數(shù)!”<<endl;Teacher() cout<<“Teacher被析構(gòu)!”<&l
34、t;endl;int main()Student s;Teacher t;return 0;3、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Exampleprivate:int i;public:Example(int n) i=n;cout<<“Constructing.”<<endl;Example() cout<<“Destructing.”<<endl; int get_i() return i; ;int sqrt_it(Example o) return o.g
35、et_i()*o.get_i();int main()Example x(10);cout<<x.get_i()<<endl;cout<<sqrt_it(x)<<endl;return 0;4、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Testprivate:int x;public:Test(int xx=0):x(xx)Test& operator+()x+;return *this;Test operator+(int)Test temp(*this); x
36、+; return temp;int getValue()constreturn x;int main()Test t;cout<<t.getValue()<<endl;cout<<(t+).getValue()<<endl;cout<<(+t).getValue()<<endl;return 0;5、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Testpublic:Test() cout<<“Default constructor.”&
37、lt;<endl; Test(const Test& t) cout<<“Copy constructor!”<<endl; ;void fun(Test p) int main()Test a;fun(a);return 0;6、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Dogpublic:static int number;Dog() number+;cout<<"New Dog"<<endl;Dog() number-;cout&l
38、t;<"A Dog Die"<<endl;int Dog:number=0;int main()Dog dog;Dog *pDog=new Dog();delete pDog;cout<<Dog:number<<endl;return 0;7、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Animalpublic:virtual void Report() cout<<“Report from Animal!”<<endl; ;class
39、 Tiger : public Animalpublic:virtual void Report() cout<<“Report from Tiger!”<<endl; ;class Monkey : public Animalpublic:virtual void Report() cout<<“Report from Monkey!”<<endl; ;void show(Animal *p)p->Report(); int main()Tiger tiger;Monkey monkey;Animal animal=tiger;show(
40、&tiger);show(&monkey);show(&animal);return 0;8、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Testpublic:Test(int xx=1):x(xx)void output()constcout<<"x:"<<x<<endl;private:int x;int main()Test t;t.output();t=4;t.output();return 0;9、寫出程序輸出結(jié)果#include
41、<iostream>using namespace std;class Testpublic:Test()cout<<"Default Constructorn"Test(int xx):x(xx)cout<<"Int Constructorn"Test(const Test& t):x(t.x)cout<<"Copy Constructorn"private:int x;Test t;int main()cout<<"-n"Test tt(t);
42、return 0;10、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Baseprivate:int base;public:Base(int b) base=b;cout<<“base=”<<b<<endl;Base() ;class Derived : public Baseprivate:Base bb;int derived;public:Derived(int d,int b,int c) : bb(c) , Base(b) derived=d;cout<<“der
43、ived=”<<derived<<endl;Derived() ;int main()Derived d(3,4,5);return 0;11、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Matrixdouble * elem;int row,col;public:Matrix(int r,int c) row=r;col=c;elem=new doublerow *col;double &operator() (int x,int y)return elem col *(x-1)+y-1
44、;double &operator()(int x,int y) constreturn elem col*(x-1)+y-1;Matrix()delete elem; ;int main()Matrix m(5,8);int i;for(i=1;i<6;i+)m(i,1)=i+5;for(i=1; i<6; i+)cout<<m(i, 1)<<”,”;cout<<endl;return 0;12、寫出程序輸出結(jié)果#include <iostream.h>class Point int x,y;public:Point(int
45、 x1=0, int y1=0) :x(x1), y(y1) cout<<"Point:"<<x<<' '<<y<<'n'Point() cout<<"Point destructor!n"class Circle Point center;/圓心位置int radius;/半徑public:Circle(int cx,int cy, int r):center(cx,cy),radius(r) cout<<"Circle rad
46、ius:"<<radius<<'n'Circle() cout<<"Circle destructor!n"int main()Circle c(3,4,5);return 0;13、寫出程序輸出結(jié)果#include <iostream.h>class Basepublic:Base (int i,int j) x0=i; y0=j;void Move(int x,int y) x0+=x; y0+=y;void Show() cout<<"Base("<<
47、x0<<","<<y0<<")"<<endl;private:int x0,y0;class Derived: private Basepublic:Derived(int i,int j,int m,int n):Base(i,j) x=m; y=n;void Show ()cout<<"Next("<<x<<","<<y<<")"<<endl;void Move1()Mov
48、e(2,3);void Show1()Base:Show();private:int x,y;int main( )Base b(1,2);b.Show();Derived d(3,4,10,15);d.Move1();d.Show();d.Show1();return 0;14、寫出程序輸出結(jié)果#include <iostream>using namespace std;class Testpublic:Test() cout<<“Hello: ”<<+i<<endl; static int i;int Test:i=0;int main()T
49、est t2;Test *p;p=new Test2;return 0;15、寫出程序輸出結(jié)果#include <iostream>using namespace std;class TestClasspublic:TestClass(int a) aa=a;cout<<aa<<" Constructed!n"TestClass()cout<<aa<<" Destructed!n"private:int aa;TestClass AA(3);int main()cout<<"
50、;In MainFuction."<<endl;TestClass BB(5);return 0;16、寫出程序輸出結(jié)果#include <iostream>using namespace std;class TestClasspublic:TestClass() cout<<"Constructed!n" TestClass() cout<<"Destructed!n" ;int main()TestClass t1;TestClass *p;p=new TestClass;delete p;r
51、eturn 0;17、寫出程序輸出結(jié)果#include <iostream>using namespace std;class TestClasspublic:TestClass() cout<<"Constructed!n"value = 10;TestClass() cout<<"Destructed!n" void setValue( int newValue) value = newValue; int getValue()const return value; private:int value;int ma
52、in()TestClass t1;cout<<t1.getValue()<<endl;TestClass &rt1 = t1;rt1.setValue(20);cout<<t1.getValue()<<endl;TestClass *pt=&t1;pt->setValue(30);cout<<t1.getValue()<<endl;return 0;18、寫出程序輸出結(jié)果#include <iostream>#include <string>using namespace st
53、d;class Personprivate:string name;int age;public:Person(string name,int age);Person()cout<<"Bye! My name is "<<name<<",I'm "<<age<<" years old."<<endl;void growup() age+; ;Person:Person(string name, int age)this->name=name;this
54、->age=age;cout<<"Hello,"<<name<<" is comming!"<<endl;int main()Person p("zhang",1);for(int i=0;i<90;+i)p.growup();return 0;19、寫出程序輸出結(jié)果#include <iostream>using namespace std;class TestClasspublic:TestClass( int newValue=0) value = newV
55、alue;cout<<"Value: "<<value<<", Constructed!n"TestClass( const TestClass & rhs)value = rhs.value;cout<<"Value: "<<value<<", Copy Constructed!n"TestClass() cout<<"Value: "<<value<<", Destr
56、ucted!n" void setValue( int newValue) value = newValue; int getValue()const return value; private:int value;TestClass fooFun( TestClass t)t.setValue(20);return t;int main()TestClass t1(10),t2(t1),t3;t3=fooFun(t1);return 0;20、寫出程序輸出結(jié)果#include <iostream>#include <string>using namespac
57、e std;class Mousepublic:Mouse( string newName );Mouse();string getName() return name; static int mouseNum;private:string name;int Mouse:mouseNum = 0;Mouse:Mouse( string newName ) : name(newName)cout<<name<<" is born!n"mouseNum+;Mouse:Mouse()cout<<name<<" is gon
58、e.n"mouseNum-;class Catpublic:Cat( const string& newName): name(newName)cout<<name<<" is coming!n"void catchMouse( Mouse *pMouse);private:string name;void Cat:catchMouse( Mouse *pMouse)cout<<"I catch you! I never want to see you again. "<<pMouse->getName()<<"!"<<endl;delete pMouse;int main()Cat cat("Black Cat Detective");Mouse *pMouse1 = new Mouse("Micky");cout<<Mouse:mouseNum<<&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2學(xué)會(huì)溝通交流(說課稿)-2023-2024學(xué)年道德與法治五年級(jí)上冊統(tǒng)編版
- 2025暫估價(jià)材料公開招標(biāo)合同范本變頻水泵排污泵
- 6~9的認(rèn)識(shí)(說課稿)-2024-2025學(xué)年一年級(jí)上冊數(shù)學(xué)人教版
- 2025以買賣合同擔(dān)保
- 2024年秋九年級(jí)化學(xué)上冊 第四單元 自然界的水說課稿 (新版)新人教版
- 2023三年級(jí)英語上冊 Assessment 3說課稿1 湘少版
- 路基邊坡防滑平臺(tái)施工方案
- Unit 4 My tidy bag Lesson 1 I have a big bag (說課稿)-2024-2025學(xué)年粵人版(2024)英語三年級(jí)上冊
- 2023八年級(jí)地理上冊 第一章 中國的疆域與人口第一節(jié) 中國的疆域說課稿 (新版)湘教版
- 出租代工合同范例
- 第五單元任務(wù)二《準(zhǔn)備與排練》教學(xué)設(shè)計(jì) 統(tǒng)編版語文九年級(jí)下冊
- 2024北京海淀高三一模英語試卷(含參考答案)
- 三高疾病之中醫(yī)辨證施治
- 全科醫(yī)學(xué)的基本原則和人文精神(人衛(wèi)第五版全科醫(yī)學(xué)概論)
- 船員健康知識(shí)課件
- 《揚(yáng)州東關(guān)街掠影》課件
- 《3-6歲兒童學(xué)習(xí)與發(fā)展指南》健康領(lǐng)域內(nèi)容目標(biāo)與指導(dǎo)
- GB/T 10739-2023紙、紙板和紙漿試樣處理和試驗(yàn)的標(biāo)準(zhǔn)大氣條件
- 環(huán)保行業(yè)研究報(bào)告
- 孩子撫養(yǎng)費(fèi)起訴狀范本:免修版模板范本
- 物流服務(wù)項(xiàng)目的投標(biāo)書
評論
0/150
提交評論