面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)_第1頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)_第2頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)_第3頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)_第4頁(yè)
面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)_第5頁(yè)
已閱讀5頁(yè),還剩20頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、 實(shí)驗(yàn)一 C+基礎(chǔ)1.1 實(shí)驗(yàn)?zāi)康?了解并熟悉開(kāi)發(fā)環(huán)境,學(xué)會(huì)調(diào)試程序;2熟悉C+中簡(jiǎn)單的標(biāo)準(zhǔn)輸入輸出函數(shù)的使用方法;3理解const修飾符的作用并學(xué)會(huì)應(yīng)用;4理解內(nèi)聯(lián)函數(shù)的優(yōu)缺點(diǎn)并學(xué)會(huì)其使用場(chǎng)合;5理解并學(xué)會(huì)函數(shù)重載;6理解并熟練掌握使用new和delete來(lái)分配內(nèi)存;7理解并熟練掌握引用的使用方法。1.2 實(shí)驗(yàn)內(nèi)容1.2.1 程序閱讀1理解下面的程序并運(yùn)行,然后回答問(wèn)題。#include <iostream.h>int max_def(int x, int y)return (x>y?x:y);int max_def(int x, int y, int z)int temp

2、 = 0;return (temp=(x>y?x:y)>z?temp:z;double max_def(double x, double y)return (x>y?x:y);int main()int x1 = 0;int x2 = 0;double d1 = 0.0;double d2 = 0.0;x1 = max_def(5,6);x2 = max_def(2,3,4);d1 = max_def(2.1,5.6);d2 = max_def(12.3,3.4,7.8);-cout<<"x1="<<x1<<endl;c

3、out<<"x2="<<x2<<endl;cout<<"d1="<<d1<<endl;cout<<"d2="<<d2<<endl;-return 1;問(wèn)題一:上述程序的輸出結(jié)果是什么?問(wèn)題二:用的是哪個(gè)函數(shù)?答:調(diào)用的函數(shù)是int max_def(int x, int y, int z)int temp = 0;return (temp=(x>y?x:y)>z?temp:z;問(wèn)題三:處的輸出結(jié)果為什么是d2=12,而

4、不是d2=12.3?答:因?yàn)樘幷{(diào)用的是整型函數(shù),d2在此函數(shù)中被轉(zhuǎn)換為整型,小數(shù)點(diǎn)后面被刪除。2理解下面的程序并運(yùn)行,然后回答問(wèn)題。#include <iostream.h>int main()int *p1 = new int; -int *p2 = new int(0); -char *p3 = new char10; -return 1;問(wèn)題一:、處動(dòng)態(tài)申請(qǐng)內(nèi)存分別代表什么意思?答:new動(dòng)態(tài)分配存放一個(gè)整數(shù)的內(nèi)存空間,并將其首地址賦給指針變量p1;new動(dòng)態(tài)分配存放一個(gè)整數(shù)的內(nèi)存空間,并對(duì)其初始化賦值為0,并將其首地址賦給指針變量p2;new動(dòng)態(tài)分配存放10個(gè)字符型數(shù)組元素

5、的內(nèi)存空間,并將其首地址賦給指針變量p3。問(wèn)題二:該程序存在什么不合理的地方?。答:程序結(jié)束時(shí)沒(méi)有將分配的空間釋放,應(yīng)該使用delete函數(shù)釋放內(nèi)存。3理解下面的程序并運(yùn)行,然后回答問(wèn)題。#include <iostream.h>void swap(int a, int b)int temp = a;a = b;b = temp;void swap(int *a, int *b)int temp = *a;*a = *b;*b = temp;int main()int i = 5;int j = 10;cout<<"Before swap: i="&

6、lt;<i<<",j="<<j<<endl;swap(i,j); -cout<<"After the first swap: i="<<i<<",j="<<j<<endl;swap(&i,&j); -cout<<"After the second swap: i="<<i<<",j="<<j<<endl;return 1

7、;問(wèn)題一:輸出結(jié)果是什么?問(wèn)題二:處函數(shù)調(diào)用不能實(shí)現(xiàn)兩個(gè)數(shù)的交換,而可以,原因是什么?答:處調(diào)用的函數(shù)只是交換了局部變量a和b,并沒(méi)有改變i和j的值;處調(diào)用的函數(shù)使用了引用形參,i和j的值隨著此處調(diào)用的函數(shù)中a和b的對(duì)換而對(duì)換。問(wèn)題三:處調(diào)用的是哪個(gè)函數(shù)?答:調(diào)用的函數(shù)是void swap(int a, int b)int temp = a;a = b;b = temp;1.2.2 程序設(shè)計(jì)1定義兩個(gè)重名函數(shù),分別求出兩點(diǎn)間平面距離和空間距離。#include<iostream>#include<cmath>using namespace std;int distanc

8、e(int x1,int y1 ,int x2,int y2)double dis;dis=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);cout<<dis<<endl;return dis;int distance(int x1,int y1,int x2,int y2,int z1,int z2)double dis;dis=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);cout<<dis<<endl;return dis;void main()int a;i

9、nt i,j,k,l,q,w,e,r,t,y;cout<<"請(qǐng)輸入平面兩點(diǎn)坐標(biāo):"<<endl;cin>>i>>j>>k>>l;a=distance(i,j,k,l);cout<<"請(qǐng)輸入空間兩點(diǎn)坐標(biāo)"<<endl;cin>>q>>w>>e>>r>>t>>y;a=distance(q,w,e,r,t,y);2 設(shè)計(jì)一個(gè)函數(shù):exch(),當(dāng)調(diào)用exch (a,b,c)時(shí),將a賦值給b,b賦

10、值給c,c賦值給a,要求采用引用的方式來(lái)實(shí)現(xiàn)。#include<iostream>#include<cmath>using namespace std;void exch(int &m,int &n,int &p)int temp=p;p=n;n=m;m=temp;int main()int a=1,b=2,c=3;cout<<"a="<<a<<"b="<<b<<"c="<<c<<endl;exch(a,

11、b,c);cout<<"a="<<a<<"b="<<b<<"c="<<c<<endl;return 0;1.3 思考題1 自己設(shè)計(jì)一個(gè)程序,測(cè)試指向常量的指針,常指針,指向常量的常指針之間的區(qū)別。#include <iostream>using namespace std;void main()int a = 10;int const *p = &a;cout<<a<<endl;cout<<*p&

12、lt;<endl;int b = 20;我們可以改變指針變量p所指向的內(nèi)容,而不能改變p的地址空間,如 添加上p = &b;我們就會(huì)發(fā)現(xiàn)編譯錯(cuò)誤!指向常量的指針const int*p,特點(diǎn)是指針?biāo)4娴牡刂房梢愿淖?,然而指針?biāo)赶虻闹祬s不可以改變。同理,當(dāng)添加*p = b時(shí),會(huì)發(fā)生編譯錯(cuò)誤!指向常量的常指針const int const*p特點(diǎn)是指針?biāo)4娴牡刂凡豢勺?,指針?biāo)赶虻臄?shù)值也不可變。2 編寫一個(gè)函數(shù),實(shí)現(xiàn)兩個(gè)字符串變量的交換。#include <iostream>using namespace std;void Exchg2(char *m,ch

13、ar *n) char tmp = *m; *m = *n; *n = tmp; void main() char a = 'q' char b = 'p'cout<<"a="<<a<<" b="<<b<<endl; Exchg2(&a, &b); cout<<"a="<<a<<" b="<<b<<endl; 實(shí)驗(yàn)三 類和對(duì)象構(gòu)造函數(shù)與析構(gòu)函數(shù)3.1

14、 實(shí)驗(yàn)?zāi)康?理解this指針的作用和用法;2掌握構(gòu)造函數(shù)的定義和作用;3掌握構(gòu)造函數(shù)的使用;4掌握拷貝構(gòu)造函數(shù)的定義和使用;5掌握構(gòu)造函數(shù)的重載;6掌握析構(gòu)函數(shù)的定義和使用。3.2 實(shí)驗(yàn)內(nèi)容3.2.1程序閱讀1理解下面的程序并運(yùn)行,然后回答后面的問(wèn)題。#include <iostream.h>class CPointpublic:void Set(int x,int y);void Print();private:int x;int y;void CPoint:Set(int x,int y)x = x;y = y;void CPoint:Print()cout<<&q

15、uot;x="<<x<<",y="<<y<<endl;void main()CPoint pt;pt.Set(10,20);pt.Print();問(wèn)題一:以上程序編譯能通過(guò)嗎?如果不能,原因是什么?能通過(guò)編譯。問(wèn)題二:以上程序的運(yùn)行結(jié)構(gòu)是否正確,如果不正確,分析為什么,如何改正?結(jié)果不正確,因?yàn)镾et函數(shù)中的形參與類中的相同產(chǎn)生錯(cuò)誤,改為void CPoint:Set(int m,int n)。2理解下面的程序并運(yùn)行,然后回答后面的問(wèn)題。#include <iostream.h>class CPerson

16、public:void Print();private:CPerson();private:int age;char *name;CPerson:CPerson()void CPerson:Print()cout<<"name="<<name<<",age="<<age<<endl;void main()CPerson ps(23,"張三");ps.Print();問(wèn)題一:以上程序存在三個(gè)錯(cuò)誤,在不改變主函數(shù)內(nèi)容的前提下,試改正該程序。#include<iostream

17、>#include<string>using namespace std;class CPersonpublic:void Print();CPerson(int m,string n)age=m;name=n;private:int age;string name;void CPerson:Print()cout<<"name="<<name<<",age="<<age<<endl;void main()CPerson ps(23,"張三");ps.Pri

18、nt();3.2.2 程序設(shè)計(jì)1設(shè)計(jì)實(shí)現(xiàn)一個(gè)CPoint類,滿足以下要求:a 該類包含兩個(gè)整型成員變量x(橫坐標(biāo))和y(縱坐標(biāo)),以及一個(gè)輸出函數(shù)Print()用來(lái)輸出橫坐標(biāo)和縱坐標(biāo),要求不可以在類的外部直接訪問(wèn)成員變量;b可以采用沒(méi)有參數(shù)的構(gòu)造函數(shù)初始化對(duì)象,此時(shí)的成員變量采用默認(rèn)值0;c可以采用直接輸入?yún)?shù)的方式來(lái)初始化該類的成員變量;#include<iostream>#include<ioamanip>using namespace std;class CPointpublic:void print();CPoint()x=0;y=0;point(int x1,

19、int y1);int GetX() return x;int GetY() return y;private:int x;int y;void CPoint:print()cout<<x<<setw(6)<<y<<endl;CPoint:point(int x1,int y1)x=x1;y=y1;void main() CPoint p;CPoint(); p.print();p.point(1,2);p.print();p.GetX();p.GetX();3.3思考題1設(shè)計(jì)一個(gè)CStudent(學(xué)生)類,并使CStudent類具有以下特點(diǎn):a

20、有學(xué)生姓名、學(xué)號(hào)、程序設(shè)計(jì)、信號(hào)處理、數(shù)據(jù)結(jié)構(gòu)三門課程的成績(jī);b全部信息由鍵盤輸入;c通過(guò)成員函數(shù)統(tǒng)計(jì)學(xué)生平均成績(jī),當(dāng)課程數(shù)量增加時(shí),成員函數(shù)無(wú)須修改仍可以求取平均成績(jī);d輸出學(xué)生的基本信息、各科成績(jī)與平均成績(jī);e學(xué)生對(duì)象用鏈表存儲(chǔ); f統(tǒng)計(jì)不及格學(xué)生人數(shù)。#include<iostream.h>#include<iomanip.h>#include<string.h>#define N 3#define M 3class CStudentpublic:void setstudent(char *name,char *sn,float scoreN);voi

21、d showstudent();private:char Sname10;char Sno8;float Score3;float Avg;float Sum;int count;void CStudent : setstudent(char *name,char *sn,float scoreN)int i;float Sum=0.0;int count=0;strcpy(Sname,name);strcpy(Sno,sn);for(i=0;i<N;i+) Scorei=scorei; count+;for(i=0;i<3;i+)Sum=Sum+Scorei;Avg=Sum/co

22、unt;void CStudent :showstudent()int i;cout<<Sname<<setw(16)<<Sno<<setw(10);for(i=0;i<3;i+)cout<<Scorei<<setw(10);cout<<setw(12)<<Avg<<endl;void main() int i,j,k=0;char name10,no8;float scoreN;for(j=1;j<=M;j+) cout<<"please input

23、student"<<j<<" name "<<setw(6);cin>>name;cout<<"please input student"<<j<<" no "<<setw(6);cin>>no;cout<<"please input student"<<j<<" score "for(i=0;i<N;i+)cin>>scor

24、ei;CStudent S1;cout<<"student"<<j<<" name"<<setw(6)<<"no"cout<<setw(15)<<"程序設(shè)計(jì)"<<setw(10)<<"信號(hào)處理"<<setw(10)<<"數(shù)據(jù)結(jié)構(gòu)"<<setw(10)<<"avg"<<endl; S1.set

25、student(name,no,score);S1.showstudent(); if(scorei<60)k+; cout<<"不及格人數(shù):"<<k<<endl;實(shí)驗(yàn)五 派生與繼承單基派生5.1 實(shí)驗(yàn)?zāi)康?理解繼承的概念;2理解公有派生、私有派生和保護(hù)派生;3理解單基派生類中構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行順序。5.2 實(shí)驗(yàn)內(nèi)容5.2.1程序閱讀1理解下面的程序并運(yùn)行,然后回答后面的問(wèn)題。#include "iostream.h"class CBasepublic:CBase(int a):a(a)protected:

26、void print()cout<<"a="<<a<<endl;private:int a;class CDerive : public CBasepublic:void print()CBase:print();cout<<"b="<<b<<endl;private:int b;void main()CDerive d;d.print();CBase b;b.print();問(wèn)題一:以上程序有兩個(gè)錯(cuò)誤,試指出來(lái),并改正之。答:派生類CDerive中沒(méi)有定義CDerive(),主函數(shù)

27、中沒(méi)有給d,b對(duì)象賦值。#include "iostream.h"class CBasepublic:CBase(int a):a(a)void print()cout<<"a="<<a<<endl;private:int a;class CDerive : public CBasepublic:CDerive(int a,int c):CBase(a)b=c;void print()CBase:print();cout<<"b="<<b<<endl;privat

28、e:int b;void main()CDerive d(1,3);d.print();CBase b(2);b.print();2理解下面的程序并運(yùn)行,然后回答后面的問(wèn)題。#include "iostream.h"class CBasepublic:CBase(int a):a(a)cout<<"base structure"<<endl;CBase()cout<<"base destructure"<<endl;void print()cout<<"a=&quo

29、t;<<a<<endl;protected:int a;class CDerive : public CBasepublic:CDerive(int a, int b,int c):CBase(a),b(b),c(c)cout<<"derive structure"<<endl;CDerive()cout<<"derive destructure"<<endl;void print()CBase:print();cout<<"b.a="<<

30、b.a<<endl;cout<<"c="<<c<<endl;private:CBase b;int c;void main()CDerive d(1,2,3); -d.print();問(wèn)題一:以上程序的輸出結(jié)果是什么,為什么?答:程序錯(cuò)誤,不能輸出結(jié)果。“cout<<"b.a="<<b.a<<endl;”這個(gè)語(yǔ)句中調(diào)用了基類中的保護(hù)參數(shù)a。問(wèn)題二:處語(yǔ)句執(zhí)行完后,d.b.a的值為多少?答:b.a=2。實(shí)驗(yàn)七 多態(tài)性函數(shù)與運(yùn)算符重載7.1 實(shí)驗(yàn)?zāi)康?理解靜態(tài)聯(lián)編和動(dòng)態(tài)聯(lián)編的

31、概念;2掌握成員函數(shù)方式運(yùn)算符重載;3掌握友元函數(shù)方式運(yùn)算符重載;4掌握+、-、=運(yùn)算符的重載。7.2 實(shí)驗(yàn)內(nèi)容7.2.1程序閱讀1理解下面的程序并運(yùn)行,然后回答后面的問(wèn)題。#include "iostream.h"class CComplexpublic:CComplex()real = 0; imag = 0;CComplex(int x,int y)real = x;imag = y;int real;int imag;CComplex operator + (CComplex obj1)-CComplex obj2(real + obj1.real, imag +

32、obj1.imag);return obj2;void main()CComplex obj1(100,30);CComplex obj2(20, 30);CComplex obj;obj = obj1+obj2; -cout << obj.real <<endl;cout << obj.imag << endl;問(wèn)題一:處的運(yùn)算符重載,為什么該函數(shù)的返回值要設(shè)計(jì)成CComplex類型?答:運(yùn)算符重載函數(shù)的返回值與其操作類的類型相同。問(wèn)題二:處的運(yùn)算符重載函數(shù)調(diào)用就相當(dāng)于“obj=operator+(obj1,obj2);”,請(qǐng)問(wèn)CComplex

33、類中的運(yùn)算符重載函數(shù)為什么只有一個(gè)參數(shù)?答:因?yàn)榱硪粋€(gè)參數(shù)是隱含調(diào)用,是CComplex類的當(dāng)前對(duì)象。它通過(guò)this指針隱含地進(jìn)行傳遞。2理解下面的程序并運(yùn)行,然后回答后面的問(wèn)題。#include "iostream.h"class CComplexpublic:CComplex()real = 0.0; imag = 0.0;CComplex(float x, float y)real = x;imag = y;CComplex operator + (CComplex &obj1, CComplex &obj2)CComplex obj3(obj1.re

34、al + obj2.real, obj1.imag + obj2.imag);return obj3;CComplex &operator+(CComplex &obj)obj.real += 1;obj.imag +=1;return obj;void print()cout<<real<<"+"<<imag<<"i"<<endl;private:float real;float imag;CComplex &operator-(CComplex &x)x.re

35、al -= 1;x.imag -= 1;return x;void main()CComplex obj1(2.1,3.2);CComplex obj2(3.6,2.5);cout<<"obj1="obj1.print();cout<<"obj2="obj2.print();CComplex obj3 = obj1 + obj2;cout<<"befor+, obj3="obj3.print();+obj3;cout<<"after+, obj3="obj3.prin

36、t();-obj3;cout<<"after-, obj3="obj3.print();CComplex obj4 = +obj3;cout<<"obj4="obj4.print();問(wèn)題一:以上程序中的三個(gè)運(yùn)算符重載都有錯(cuò)誤,試改正并分析輸出結(jié)果。#include "iostream.h"class CComplexpublic:CComplex()real = 0.0;imag = 0.0;CComplex(float x, float y)real = x;imag = y;CComplex operat

37、or + (CComplex &obj1)CComplex obj2(real + obj1.real, imag + obj1.imag);return obj2;friend CComplex operator+(CComplex &obj)obj.real += 1;obj.imag += 1;return obj;CComplex operator-();void print()cout << real << "+" << imag << "i" << endl;priv

38、ate:float real;float imag;CComplex CComplex:operator-()real -= 1;imag -= 1;return (*this);void main()CComplex obj1(2.1, 3.2);CComplex obj2(3.6, 2.5);cout << "obj1="obj1.print();cout << "obj2="obj2.print();CComplex obj3 = obj1 + obj2;cout << "befor+, obj3=&q

39、uot;obj3.print();+obj3;cout << "after+, obj3="obj3.print();-obj3;cout << "after-, obj3="obj3.print();CComplex obj4 = +obj3;cout << "obj4="CComplex obj4.print();結(jié)果:obj1=2.1+3.2;obj2=3.1+2.5;obj3=5.7+5.7;after+obj3=1.7+1.7;after-obj3=5.7+5.7;obj4=1.7+1.7;7.2.2 程序設(shè)計(jì)1

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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)論