《面向?qū)ο蟪绦蛟O(shè)計(jì)》答案_第1頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》答案_第2頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》答案_第3頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》答案_第4頁(yè)
《面向?qū)ο蟪绦蛟O(shè)計(jì)》答案_第5頁(yè)
已閱讀5頁(yè),還剩17頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!實(shí)驗(yàn)一 熟悉VC+IDE開發(fā)環(huán)境一、實(shí)驗(yàn)?zāi)康?、熟悉VC+6.0集成開發(fā)環(huán)境,熟練掌握VC+6.0項(xiàng)目工作區(qū)、各種編輯器、菜單欄和工具欄的使用。2、掌握如何編輯、編譯、連接和運(yùn)行一個(gè)C+程序。3、通過運(yùn)行簡(jiǎn)單的C+程序,初步了解C+源程序的結(jié)構(gòu)和特點(diǎn)。二、實(shí)驗(yàn)要求1、分析下列程序運(yùn)行的結(jié)果。程序一:#include int add(int x,int y=8);void main() int x=4; coutadd(x),; coutadd(x,add(add(x,add(x)endl;int add(int x,int y) return x+y;

2、/12,28程序二:#include void main()int *p,i; i=5;p=&i;i=*p+10;couti=iendl;/i=15程序三:#include void main(void)int i=10;int &r=i; r+;couti=i, r=rn;i=88; couti=i, r=rn;/i=11,r=111 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!i=88,r=88程序四:#include int f(int i) static int k=1; for(;i0;i-) k +=i; return k; void main() int i; for(i=

3、0;i5;i+) coutf(i) ; / 1 2 5 11 21程序五:#include void func();int n=1; void main() static int a; int b= -9; cout a:a b:b n: nendl;b+=4; func();cout a:a b:b n:nendl;n+=10; func();void func() static int a=2; int b=5; a+=2;n+=12;b+=5; cout a: a b: b n: n endl; / a:0 b:-9 n:1a:4 b:10 n:13a:0 b:-5 n:13a:6 b:

4、10 n:35實(shí)驗(yàn)二 C+對(duì)C的擴(kuò)充一、實(shí)驗(yàn)?zāi)康? / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!1、了解在面向?qū)ο蟪绦蛟O(shè)計(jì)過程中C+對(duì)C功能的擴(kuò)充與增強(qiáng),并善于在編寫程序的過程中應(yīng)用這些新功能。2、進(jìn)一步熟悉編輯、編譯、連接和運(yùn)行C+程序的方法。3、進(jìn)一步熟悉C+程序的結(jié)構(gòu)和編程方法。二、實(shí)驗(yàn)要求1、分析下列程序運(yùn)行的結(jié)果。#include int amount=123; void main()int amount=456; cout:amount,; coutamount,; :amount=789; cout:amount,; coutamountn; / 123,456,789,4

5、562、編寫一個(gè)程序,用來求2個(gè)或3個(gè)正整數(shù)中的最大數(shù)。用不帶默認(rèn)參數(shù)的函數(shù)實(shí)現(xiàn)。include using namespace std;int max(int a,int b,int c) /求3個(gè)整數(shù)中的最大者if (ba) a=b; if (ca) a=c; return a; int max(int a, int b) /求兩個(gè)整數(shù)中的最大者if (ab) return a; else return b;int main( )int a=7,b=-4,c=9; coutmax(a,b,c)endl; /輸出3個(gè)整數(shù)中的最大者 coutmax(a,b)endl; /輸出兩個(gè)整數(shù)中的最大者

6、 return 0;用帶默認(rèn)參數(shù)的函數(shù)實(shí)現(xiàn)。#include using namespace std;int main()int max(int a,int b,int c=0); int a,b,c; cinabc;3 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載! coutmax(a,b,c)=max(a,b,c)endl; coutmax(a,b)=max(a,b)a) a=b; if(ca) a=c; return a;3、有5個(gè)字符串,要求對(duì)它們按由小到大順序排列,用string方法。#include #include using namespace std;int main()

7、 int i; string str5=BASIC,C,FORTRAN,C+,PASCAL; void sort(string ); sort(str); coutthe sorted strings :endl; for(i=0;i5;i+) coutstri ; coutendl; return 0;void sort(string s)int i,j; string t; for (j=0;j5;j+) for(i=0;isi+1) t=si;si=si+1;si+1=t; 4、定義一個(gè)求兩個(gè)數(shù)中較小值的函數(shù)模板min( ),要求在main( )函數(shù)中進(jìn)行調(diào)用求兩個(gè)浮點(diǎn)型數(shù)據(jù)和兩個(gè)整型數(shù)

8、據(jù)中較小的數(shù)。#include iostream#include stringusing namespace std;templateT min(T a,T b) return ab?a:b;int main()4 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載! int a=1, b=9; float c=1.23471,d=32.431564; coutThe min of a and b is min(a,b)endl The min of c and d is min(c,d)endl; return 0;實(shí)驗(yàn)三 類和對(duì)象(一)一、實(shí)驗(yàn)?zāi)康?、掌握聲明類的方法,類和類的成員的概念以及

9、定義對(duì)象的方法。2、掌握類的構(gòu)造函數(shù)與析構(gòu)函數(shù)的概念和使用方法。3、初步掌握用類和對(duì)象編制基于對(duì)象的程序。二、實(shí)驗(yàn)要求1、分析下面的程序,寫出其運(yùn)行時(shí)的輸出結(jié)果。#include using namespace std;class Datepublic:Date(int,int,int);Date(int,int);Date(int);Date( );void display( );private:int month;int day;int year;DateDate(int m,int d,int y):month(m),day(d),year(y) DateDate(int m,int d

10、):month(m),day(d) year=2005; DateDate(int m):month(m) day=1;year=2005;DateDate( ) month=1;day=1;year=2005;void Datedisplay( )coutmonth/day/yearendl;int main( )5 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載! Date d1(10,13,2005);Date d2(12,30);Date d3(10);Date d4;d1.display( );d2.display( );d3.display( );d4.display( );re

11、turn 0;/ 10/13/2005 12/30/2005 10/1/2005 1/1/20052、建立一個(gè)名為Student的類,該類有以下幾個(gè)私有成員變量:學(xué)生姓名、學(xué)號(hào)、性別、年齡。還有以下兩個(gè)成員變量:一個(gè)用于初始化學(xué)生姓名、學(xué)號(hào)、性別和年齡的構(gòu)造函數(shù),一個(gè)用于輸出學(xué)生信息的函數(shù)。編寫一個(gè)主函數(shù),聲明一個(gè)學(xué)生對(duì)象,然后調(diào)用成員函數(shù)在屏幕輸出學(xué)生信息。#include iostream#include stringusing namespace std;class student public: student(); void display(); private: string sN

12、ame,sNum; char chSex; int iAge;student:student(string na,string num,char s,int a):sName(na),sNum(num), chSex(s),iAge(a)void student:display() cout -THE INFORMATION OF STUDENT-n; cout name: sName endl number: sNumendl sex: chSex endl age: iAge endl;int main() student s(WangFang,0811045263,w,20);6 / 2

13、1如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載! s.display(); return 0;3、類Person的定義如下,請(qǐng)實(shí)現(xiàn)該類,并在主函數(shù)中創(chuàng)建對(duì)象obj,然后使用構(gòu)造函數(shù)為obj賦予初始值(內(nèi)容自定)。class Person private: char name10; int age; int salary; char tel8;public: Person(char *xname,int xage,int xsalary,char *xtel); void disp();解:#include #include Person:person(char *Xname,int Xage,in

14、t Xsalary,char *Xtel) strcpy ( name, xname);age=xage;salary=xsalary;strcpy (tel,xtel);void Person:disp() cout“ 姓名:”nameendl;cout“ 年齡”:ageendl;cout“ 工資”:salaryendl:cout“ 電話”:telendl;void main() Person obj (“張三”,25,850,“45678912”); obj.disp()實(shí)驗(yàn)四 類和對(duì)象(二)一、實(shí)驗(yàn)?zāi)康?、進(jìn)一步加深對(duì)類和對(duì)象的理解。2、掌握對(duì)類的對(duì)象數(shù)組、對(duì)象的指針及其使用方法。3、掌

15、握友元的概念和使用。4、了解類模板的使用方法。二、實(shí)驗(yàn)要求1、分析并比較下列程序運(yùn)行的結(jié)果。7 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!程序一:#include#includeclass smallonepublic:smallone(int sma) coutsm constr:sman;void fn(int n) smallone sm(n);coutin function fn with n=nendl;int main() fn(10); fn(20); return 0;/sm constr: 10in function fn with n=10sm constr: 20

16、in function fn with n=20程序二:#include#includeclass smallonepublic:smallone(int sma) coutsm constr:sman;void fn(int n) static smallone sm(n);coutin function fn with n=nendl; int main() fn(10); fn(20); return 0;/sm constr:10in function fn with n=10in function fn with n=202、建立一個(gè)對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生的數(shù)據(jù)(學(xué)號(hào)、成績(jī)),設(shè)立一

17、個(gè)函數(shù)max,用指向?qū)ο蟮闹羔樧骱瘮?shù)參數(shù),在max函數(shù)中找出5個(gè)學(xué)生中成績(jī)最高者,并輸出其學(xué)號(hào)。8 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!#include using namespace std;class Student public: Student(int n,float s):num(n),score(s) int num; float score; ;void main()Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,

18、95.5); void max(Student* ); Student *p=&stud0; max(p); reyurn 0; void max(Student *arr)float max_score=arr0.score; int k=0; for(int i=1;imax_score) max_score=arri.score;k=i; coutarrk.num max_scoreendl; 3、聲明一個(gè)類模板,利用它分別實(shí)現(xiàn)兩個(gè)整數(shù)、浮點(diǎn)數(shù)和字符的比較,求出大數(shù)和小數(shù)。#include using namespace std;templateclass Compare public:

19、 Compare(numtype a,numtype b) x=a;y=b; numtype max() return (xy)?x:y; numtype min() return (xy)?x:y; private: numtype x,y; ;int main()Compare cmp1(3,7); coutcmp1.max() is the Maximum of two inteder numbers.endl; coutcmp1.min() is the Minimum of two inteder numbers.endlendl;9 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載

20、! Compare cmp2(45.78,93.6); coutcmp2.max() is the Maximum of two float numbers.endl; coutcmp2.min() is the Minimum of two float numbers.endlendl; Compare cmp3(a,A); coutcmp3.max() is the Maximum of two characters.endl; coutcmp3.min() is the Minimum of two characters.endl; return 0;實(shí)驗(yàn)五 運(yùn)算符重載一、實(shí)驗(yàn)?zāi)康?、進(jìn)

21、一步了解運(yùn)算符重載的概念和使用方法。2、掌握幾種常用的運(yùn)算符重載的方法。二、實(shí)驗(yàn)要求1、定義一個(gè)復(fù)數(shù)類Complex,重載運(yùn)算法“+”,使之能用于復(fù)數(shù)的加法運(yùn)算。將運(yùn)算符重載為普通函數(shù)(非成員、非友元)、成員函數(shù)、友元函數(shù)。根據(jù)要求修改通過函數(shù)來實(shí)現(xiàn)復(fù)數(shù)相加的示例,分別編寫程序,求兩個(gè)復(fù)數(shù)之和。#include using namespace std;class Complex /定義Complex類public: Complex(float x=0,float y=0)real=x;imag=y; /構(gòu)造函數(shù) Complex complex_add(Complex &c2); /聲明復(fù)數(shù)相加

22、函數(shù) void display() coutreal+imagiendl; ; private: float real; /實(shí)部 float imag; /虛部;Complex Complex:complex_add(Complex &c2) Complex c;c.real = real +c2.real;c.imag=imag+c2.imag;return c;int main() Complex complex1(3.34f, 4.8f), complex2(12.8f, 5.2f);Complex complex; /定義3個(gè)復(fù)數(shù)對(duì)象complex=plex_add(complex2)

23、; / 進(jìn)行兩個(gè)復(fù)數(shù)的加運(yùn)算complex.display( ); return 0;/16.14+10i10 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!/普通函數(shù)(非成員、非友元)#include using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; double get_real(); double get_imag(); void display(); private: double real; double imag;

24、; double Complex:get_real()return real;double Complex:get_imag()return imag;void Complex:display()cout(real,imagi)endl;Complex operator + (Complex &c1,Complex &c2) return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag();int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc3=; c3.dis

25、play(); return 0; /運(yùn)算符重載為成員函數(shù)#include using namespace std;class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator + (Complex &c2);11 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載! void display(); private: double real; double imag; ;Complex Complex:operator + (Complex &c2)C

26、omplex c; c.real=real+c2.real; c.imag=imag+c2.imag;return c;void Complex:display()cout(real,imagi)endl;int main()Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc1=;c1.display(); coutc2=;c2.display(); coutc1+c2=;c3.display(); return 0;/將運(yùn)算符重載為友元函數(shù)#include using namespace std;class Complex public: Comple

27、x()real=0;imag=0; Complex(double r)real=r;imag=0; Complex(double r,double i)real=r;imag=i; friend Complex operator+ (Complex &c1,Complex &c2); void display(); private: double real; double imag; ;Complex operator+ (Complex &c1,Complex &c2) return Complex(c1.real+c2.real, c1.imag+c2.imag);void Complex

28、:display()cout(real,imagi)endl;int main()12 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; coutc1=; c1.display(); coutc2=; c2.display(); coutc1+c2=; c3.display(); return 0;實(shí)驗(yàn)六 繼承和派生一、實(shí)驗(yàn)?zāi)康?、了解繼承在面向?qū)ο蟪绦蛟O(shè)計(jì)中的重要作用。2、進(jìn)一步理解繼承與派生的概念。3、掌握通過繼承派生出一個(gè)新的類的方法。4、了解虛基類的作用和用法。二、實(shí)驗(yàn)要求1、運(yùn)行程序,分析構(gòu)造函數(shù)與析構(gòu)函

29、數(shù)的調(diào)用順序。程序一:#include class A public: A()coutA:Constructorendl;A()coutA:Destructor endl;class B:public A public: B()coutB:Constructor endl; B()coutB:Destructorendl;void main() B b;/A:ConstructorB:ConstructorB:DestructorA:Destructor程序二:#include class A int a;public :A(int aa=0) a=aa; A() cout”Destructo

30、r A!”aendl; 13 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!;class B: public A int b;public: B(int aa=0, int bb=0) : A(aa) b=bb; B() cout”Destructor B!”bendl; ;void main() B x(5),y(6,7);/Destructor B!7Destructor A!6Destructor B!0Destructor A!5調(diào)用順序:構(gòu)造x.A a=5 構(gòu)造x.B a=5 b=0 構(gòu)造y.A / 不匹配,不調(diào)用A() 構(gòu)造y.B a=6 b=7 析構(gòu)y.B B!7 析構(gòu)y.A

31、 A!6 析構(gòu)x.B B!0 析構(gòu)x.A A!52、分別聲明Teacher(教師)類和Cadre(干部)類,采用多重繼承方式由這兩個(gè)類派生出新類Teacher_Cader類。要求:在兩個(gè)基類種豆包含姓名、年齡、性別、地址、電話等數(shù)據(jù)成員。在Teacher類中還包含數(shù)據(jù)成員title(職稱),在Cader 類中還包含數(shù)據(jù)成員post(職務(wù))。在Teacher_Cader類中還包含數(shù)據(jù)成員wages(工資)。在對(duì)兩個(gè)基類中的姓名、年齡、性別、地址、電話等數(shù)據(jù)成員用相同的名字,在引用這些數(shù)據(jù)成員時(shí),指定作用域。在類體中聲明成員函數(shù),在類外定義數(shù)據(jù)成員。在派生類Teacher_Cader的成員函數(shù)sh

32、ow中調(diào)用Teacher類中的display函數(shù),輸出姓名、年齡、性別、職稱、地址、電話,然后再調(diào)用cout語句輸出職務(wù)和工資。#include#include using namespace std;class Teacher public: Teacher(string nam,int a,char s,string tit,string ad,string t); void display(); protected: string name; int age; char sex; string title; string addr; string tel;Teacher:Teacher(

33、string nam,int a,char s,string tit,string ad,string t): name(nam),age(a),sex(s),title(tit),addr(ad),tel(t) void Teacher:display()14 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載! coutname:nameendl; coutageageendl; coutsex:sexendl; couttitle:titleendl; coutaddress:addrendl; couttel:telendl; class Cadre public: Cadre(stri

34、ng nam,int a,char s,string p,string ad,string t); void display(); protected: string name; int age; char sex; string post; string addr; string tel; ; Cadre:Cadre(string nam,int a,char s,string p,string ad,string t): name(nam),age(a),sex(s),post(p),addr(ad),tel(t) void Cadre:display() coutname:nameend

35、l; coutage:ageendl; coutsex:sexendl; coutpost:postendl; coutaddress:addrendl; couttel:telendl; class Teacher_Cadre:public Teacher,public Cadre public: Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w); void show( ); private: float wage; ; Teacher_Cadre:Teacher_Cad

36、re(string nam,int a,char s,string t,string p,string ad,string tel,float w): Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) void Teacher_Cadre:show( ) Teacher:display(); coutpost:Cadre:postendl; coutwages:wageendl;15 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載! int main( ) Teacher_Cadre te_ca(Wang-li,50,f,pr

37、of.,president,135 Beijing Road,Shanghai,(021)61234567,1534.5); te_ca.show( ); return 0;實(shí)驗(yàn)七 多態(tài)性和虛函數(shù)一、實(shí)驗(yàn)?zāi)康?、了解多態(tài)性的概念。2、了解虛函數(shù)的作用及其使用方法。3、了解靜態(tài)關(guān)聯(lián)和動(dòng)態(tài)關(guān)聯(lián)的概念和用法。4、了解純虛函數(shù)和抽象類的概念和用法。二、實(shí)驗(yàn)要求1、分析程序運(yùn)行結(jié)果,掌握虛函數(shù)的使用。程序一:#include class ONE public: virtual void f()coutlendl;class TWO:public ONE public: TWO()cout2endl;cl

38、ass THREE:public TWO public: virtual void f()TWO:f(); coutf();/221316 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!程序二:#includeclass Base public:virtual void fn() cout In Base Classn;class SubClass :public Base public: virtual void fn() cout fn();p=≻ p-fn();/In Base ClassIn Sub Class2、實(shí)現(xiàn)一個(gè)類A,在A中有兩個(gè)私有的整型變量a和b,定義構(gòu)造函數(shù)對(duì)

39、a和b進(jìn)行初始化,并實(shí)現(xiàn)成員函數(shù)geta()取得a的值和getb()取b的值。實(shí)現(xiàn)類B從A繼承,覆蓋geta(),使其返回a的2倍。主函數(shù)中聲明類B對(duì)象,調(diào)用類B中的geta()并將結(jié)果輸出。#include iostreamusing namespace std;class A private: int a; int b; public: A(int m,int n) a=m;b=n; int geta() return a; int getb() return b;class B :public A public: B(int m,int n):A(m,n) int geta() retu

40、rn A:geta()*2;void main() B b(2,2); cout b.geta() endl; return 0;3、聲明抽象基類Shape,由它派生出3個(gè)派生類:Cirle(圓形)、Rectangle(矩形)、Triangle(三角形),用一個(gè)函數(shù)printArea分別輸出以上三者的面積,3個(gè)圖形的數(shù)據(jù)在定義對(duì)象是給定。18 / 21如果您需要使用本文檔,請(qǐng)點(diǎn)擊下載按鈕下載!#include using namespace std;/定義抽象基類Shapeclass Shapepublic: virtual double area() const =0; /純虛函數(shù);/定義C

41、ircle類class Circle:public Shapepublic:Circle(double r):radius(r) /結(jié)構(gòu)函數(shù) virtual double area() const return 3.14159*radius*radius; /定義虛函數(shù) protected: double radius; /半徑;/定義Rectangle類class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) /結(jié)構(gòu)函數(shù) virtual double area() const return width*height; /定義虛函數(shù) protected: double width,height; /寬與高;class T

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論