歡迎來到裝配圖網(wǎng)! | 幫助中心 裝配圖網(wǎng)zhuangpeitu.com!
裝配圖網(wǎng)
ImageVerifierCode 換一換
首頁 裝配圖網(wǎng) > 資源分類 > DOC文檔下載  

面向對象程序設計(C++)實驗報告

  • 資源ID:28058064       資源大?。?span id="esg55sa" class="font-tahoma">435KB        全文頁數(shù):25頁
  • 資源格式: DOC        下載積分:15積分
快捷下載 游客一鍵下載
會員登錄下載
微信登錄下載
三方登錄下載: 微信開放平臺登錄 支付寶登錄   QQ登錄   微博登錄  
二維碼
微信掃一掃登錄
下載資源需要15積分
郵箱/手機:
溫馨提示:
用戶名和密碼都是您填寫的郵箱或者手機號,方便查詢和重復下載(系統(tǒng)自動生成)
支付方式: 支付寶    微信支付   
驗證碼:   換一換

 
賬號:
密碼:
驗證碼:   換一換
  忘記密碼?
    
友情提示
2、PDF文件下載后,可能會被瀏覽器默認打開,此種情況可以點擊瀏覽器菜單,保存網(wǎng)頁到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無水印,預覽文檔經(jīng)過壓縮,下載后原文更清晰。
5、試題試卷類文檔,如果標題沒有明確說明有答案則都視為沒有答案,請知曉。

面向對象程序設計(C++)實驗報告

C+課程設計面向對象程序設計(C+)實驗報告姓名:劉九州 學院:數(shù)學與計算機學院 班級:10級計算機大類三班 學號:100511314實驗5 單繼承一、實驗目的1掌握派生的類別與方式; 2了解派生類中如何使用基類的成員、基類成員在派生類中的訪問控制; 3. 掌握繼承中構造函數(shù)和析構函數(shù)的調(diào)用過程。二、實驗內(nèi)容及步驟1. 給出一個Document類,從Document派生出Book類,增加PageCount變量。在主函數(shù)中進行測試,創(chuàng)建Book類對象并進行初始化,輸出書名和頁數(shù)。 2. 設計一個單基繼承的類層次程序,利用Person類派生出Student類,增加屬性xh(學號),Person類中至少有姓名、年齡等數(shù)據(jù)成員,成員函數(shù)中構造函數(shù)對其初始化,析構函數(shù)釋放相應存儲單元,輸出函數(shù)輸出其數(shù)據(jù)成員的值,其它成員函數(shù)根據(jù)需要添加,在主函數(shù)中進行測試。3. 設計一個人員類person和一個日期類date,由人員類派生出學生類student和教師類professor,學生類和教師類的數(shù)據(jù)成員birthday為日期類。在主函數(shù)中進行測試。三、實驗源程序和運行結果實驗(一)源程序:#include<iostream>#include<string>using namespace std;class Documentpublic:Document();Document();Document(char*name);char *Name;void PrintNameOf();Document:Document(char*name)Name=new charstrlen(name+1);strcpy(Name,name);Document:Document()delete Name;void Document:PrintNameOf()cout<<Name<<endl;class Book:public Documentpublic:int PageCount;Book(char *a,int b):Document(a)PageCount=b;void main()char BookName20;int n;cout<<"請輸入書名:"<<endl;cin>>BookName;cout<<"請輸入書的頁數(shù):"<<endl;cin>>n;Book b(BookName,n);cout<<"書名為:"<<b.Name<<endl;cout<<"頁數(shù)為:"<<b.PageCount<<endl;運行結果:實驗(二)源程序:#include<iostream>#include<string>using namespace std;class personpublic:person()name="張三"age=0;person(string c,int a)name=c;age=a;person()void setname(string c)name=c;string getname()return name;void setage(int a)age=a;int getage()return age;private:string name;int age;class student:public personpublic:student()xh=0;student(int d)xh=d;student(string c,int a,int d):person(c,a)xh=d;student()void setxh(int d)xh=d;int getxh()return xh;private:int xh;void main()string c;cout<<"請輸入學生的姓名:n"cin>>c;cout<<"請輸入學生的年齡:n"int a;cin>>a;cout<<"請輸入學生的學號:n"int d;cin>>d;student n(c,a,d);cout<<"請輸入學生的姓名為: "<<n.getname()<<endl;cout<<"請輸入學生的年齡為: "<<n.getage()<<endl; cout<<"請輸入學生的學號為: "<<n.getxh()<<endl;運行結果:實驗(三)源程序:#include<iostream>using namespace std;class personpublic:person()name="張三"age=0;person(string c,int a)name=c;age=a;person()void setname(string c)name=c;string getname()return name;void setage(int a)age=a;int getage()return age;private:string name;int age;class datepublic:date()year=2011;month=12;day=17;date()date(int y,int m,int d)year=y;month=m;day=d;int getyear()return year;int getmonth()return month;int getday()return day;private:int year;int month;int day;class student:public personpublic:student()/birthday.date();student(int y,int m,int d):birthday(y,m,d)student()void getbirthday()cout<<"學生的生日為:n"cout<<birthday.getyear()<<"年"<<birthday.getmonth()<<"月"<<birthday.getday()<<"日"<<endl;private:date birthday;class teacher:public personpublic:teacher()/birthday.date();teacher(int y,int m,int d):birthday(y,m,d)/birthday.date(y,m,d);teacher()void getbirthday()cout<<"老師的生日為:n"cout<<birthday.getyear()<<"年"<<birthday.getmonth()<<"月"<<birthday.getday()<<"日"<<endl;private:date birthday;void main()cout<<"請輸入學生的生日:"<<endl;int y,m,d;cin>>y>>m>>d;student s(y,m,d);cout<<"請輸入老師的生日:"<<endl;cin>>y>>m>>d;teacher t(y,m,d);s.getbirthday(); t.getbirthday();運行結果:實驗6 多繼承一、實驗目的1掌握多基繼承的使用,訪問方法;2理解類層次中訪問規(guī)則;3掌握虛基類的定義及使用。二、實驗內(nèi)容及步驟1. 定義一個學生類Student和教師類Teacher,學生類有姓名、學號、私有數(shù)據(jù)成員,教師類有姓名、工作證號、職稱、課程、周學時數(shù)。再定義一個助教類TA,繼承學生類和教師類,該類可以使用學生類的全部數(shù)據(jù)成員,以及教師類的課程和周學時數(shù)的數(shù)據(jù)成員。要求:每個類提供自定義的構造函數(shù)和析構函數(shù),并通過同名函數(shù)ShowInfo來顯示全部數(shù)據(jù)成員的值。2. 設計一個虛基類Person,包含姓名和年齡私有數(shù)據(jù)成員以及相關的成員函數(shù);由它派生出領導類Leader,包含職務和部門私有數(shù)據(jù)成員以及相關的成員函數(shù);再由Person派生出工程師類Engineer,包含職務和專業(yè)私有數(shù)據(jù)成員以及相關的成員函數(shù);再由Leader和Engineer類派生出主任工程師類Chairman。并采用相關數(shù)據(jù)進行測試。三、實驗源程序和運行結果實驗(一)源程序:#include<iostream.h>#include<string.h>class Studentprotected:char s_name20;int id_s;public:Student(char *name,int id);void ShowInfo();class Teacherprotected:char t_name20;int id_t;char position30;char lesson30;int hour;public:Teacher(char *pos,int h);Teacher(char *name,int id,char *less,char *pos,int h);void ShowInfo();class TA:public Student,public Teacherpublic:TA(char *name,char id,char *less,int h);void ShowInfo();Student:Student(char *name,int id)strcpy(s_name,name);id_s=id;void Student:ShowInfo()cout<<"姓名:"<<s_name<<t<<"學號:"<<id_s<<endl;Teacher:Teacher(char *less,int h)strcpy(lesson,less);hour=h;Teacher:Teacher(char *name,int id,char *less,char *pos,int h)strcpy(t_name,name);strcpy(lesson,less);strcpy(position,pos);id_t=id;hour=h;void Teacher:ShowInfo()cout<<"姓名:"<<t_name<<t<<"職工號:"<<id_t<<t<<"職稱:"<<position<<t<<"課程:"<<lesson<<t<<"學時數(shù):"<<hour<<endl;TA:TA(char *name,char id,char *less,int h):Student(name,id),Teacher(less,h)void TA:ShowInfo()Student:ShowInfo();cout<<"課程:"<<lesson<<t<<"學時數(shù):"<<hour<<endl;void main()TA ta("劉九州",14,"c+",64);ta.ShowInfo();運行結果:實驗(二)源程序:#include<iostream.h>#include<string.h>class Person /虛基類person類char name30;int age;public:Person(char *n,int a);void setname(char *n);void setage(int a);char *getname();int getage();class Leader:virtual public Person/領導類char job30;/職務char dep30;/部門public:Leader(char *jb,char *dp);void setjob(char *jb);void setdep(char *dp);char *getjob();char *getdep();class Engineer:virtual public Person/工程師類char major30; /專業(yè)char prof30; /職稱public:Engineer(char *maj,char *pf);void setmajor(char *maj);void setprof(char *pf);char *getmajor();char *getprof();class Chairman:public Leader,public Engineer /主任工程師類public:Chairman(char *n,int a,char *jb,char *dp,char *maj,char *pf);void disp();Person:Person(char *n,int a)strcpy(name,n);age=a;void Person:setname(char *n)strcpy(name,n);void Person:setage(int a)age=a;char *Person:getname()return name;int Person:getage()return age;Leader:Leader(char *jb,char *dp):Person("",30)strcpy(job,jb);strcpy(dep,dp);void Leader:setjob(char *jb)strcpy(job,jb);void Leader:setdep(char *dp)strcpy(dep,dp);char *Leader:getjob()return job;char *Leader:getdep()return dep;Engineer:Engineer(char *maj,char *pf):Person("",30)strcpy(major,maj);strcpy(prof,pf);void Engineer:setmajor(char *maj)strcpy(major,maj);void Engineer:setprof(char *pf)strcpy(prof,pf);char *Engineer:getmajor()return major;char *Engineer:getprof()return prof;Chairman:Chairman(char *n,int a,char *jb,char *dp,char *maj,char *pf):Person(n,a),Leader(jb,dp),Engineer(maj,pf)void Chairman:disp()cout<<"姓名:"<<getname()<<t<<"年齡:"<<getage()<<t<<"職務:"<<getjob()<<t<<"部門:"<<getdep()<<t<<"專業(yè):"<<getmajor()<<t<<"職稱:"<<getprof()<<endl;void main()Chairman c("劉九州",21,"廳長","財政廳","經(jīng)濟學","高級經(jīng)濟師");c.disp();運行結果: 實驗7 多態(tài)與虛函數(shù)一、實驗目的1.理解多態(tài)的概念2.掌握如何用虛函數(shù)實現(xiàn)運行時多態(tài)3.掌握如何利用抽象類二、實驗內(nèi)容及步驟1. 設計一個圖形類(Shape),由它派生出三角形類(Triangle)、正方形類(Square)、圓形類(Circle),利用虛函數(shù)計算圖形面積,并在主函數(shù)中進行測試。2. 定義一個教師類,由教師類派生出講師、副教授、教授類。教師的工資分別由基本工資、課時費和津貼構成。假設講師、副教授、教授的基本工資分別為800、900、1000元,課時費分別為每小時40、45、50元,津貼分別為1300、1800、2300。定義虛函數(shù)來計算教師的工資,并通過主函數(shù)來進行驗證。三、實驗源程序和運行結果實驗(一)源程序:#include<iostream>using namespace std;class Shapepublic:virtual float area()return 0.0;class Triangle:public Shapepublic:Triangle()bc=1.0;h=1.0;Triangle(float bc,float h)this->bc=bc;this->h=h;bool setbc(float a)if(a>0)bc=a;float getbc()return bc;bool setg(float b)if(b>0)h=b;float getg()return h;float area()return bc*h/2;protected:float bc,h;class Square:public Shapepublic:Square()l=1.0;Square(float m)this->l=m;bool setbc(float c)if(c>0)l=c;float getbc()return l;float area()return l*l;protected:float l;class Circle:public Shapepublic:Circle()radius=1.0;Circle(float R) this->radius=R;bool setRadius(float r)if(r>0)radius=r;float getRadius()return radius;float area()return 3.14159*radius*radius;protected:float radius;void displayShapeArea(Shape *p)cout<<"圖形面積為:"<<p->area()<<endl;void main()Shape *p1,*p2,*p3;Triangle T(15.0,10.0);Square S(10.0);Circle C(10.0);p1=&T;p2=&S;p3=&C;displayShapeArea(p1);displayShapeArea(p2);displayShapeArea(p3);運行結果:實驗(二)源程序:#include<iostream>using namespace std;class teacherpublic:virtual float wage()return 0.0;class lecturer:public teacherpublic:lecturer()WorkHours=1.0;lecturer(float WorkHours)this->WorkHours=WorkHours;bool setWorkHours(float h)if(h>0)WorkHours=h;float getWorkHours()return WorkHours;float wage()return (800+40*WorkHours+1300);protected:float WorkHours;class AssociateProfessor:public teacherpublic:AssociateProfessor()WorkHours=1.0;AssociateProfessor(float WorkHours)this->WorkHours=WorkHours;bool setWorkHours(float h)if(h>0)WorkHours=h;float getWorkHours()return WorkHours;float wage()return (900+45*WorkHours+1800);protected:float WorkHours;class Professor:public teacherpublic:Professor()WorkHours=1.0;Professor(float WorkHours)this->WorkHours=WorkHours;bool setWorkHours(float h)if(h>0)WorkHours=h;float getWorkHours()return WorkHours;float wage()return (1000+50*WorkHours+2300);protected:float WorkHours;void displayWage(teacher *s)cout<<"工資為:"<<s->wage()<<endl;void main()teacher *s1,*s2,*s3;lecturer L(30.5);AssociateProfessor A(20.6);Professor P(10.5);s1=&L;s2=&A;s3=&P;displayWage(s1);displayWage(s2);displayWage(s3);運行結果:實驗8 運算符重載一、實驗目的掌握C+中運算符重載的機制和運算符重載的方式;二、實驗內(nèi)容及步驟1. 編寫一個簡單復數(shù)類Scomplex,要求用友元函數(shù)重載“+”、“-”運算符,用成員函數(shù)重載“=”運算符,使之能夠實現(xiàn)整數(shù)或浮點數(shù)和復數(shù)的加法和減法,并且進行測試。 2. 空間一點p的坐標為(x,y,z),其中x,y,z為整數(shù)。編寫點類Point3D,定義空間兩點之間的加”+”,減”-”運算為相應三個坐標值分別進行加、減運算,要求實現(xiàn)空間兩點之間的加”+”減”-”賦值”=”運算,空間兩點間的比較”= =”運算。要求編寫Point3D類的聲明定義和測試程序。3. 設計一個時間類Time,包括時、分、秒等私有數(shù)據(jù)成員。重載“+”和“-”運算符以實現(xiàn)時間的加法和減法運算,并進行測試。三、實驗源程序和運行結果 實驗(一)源程序:#include<iostream.h>class Scomplex private:double real,imag;public:Scomplex()real=0; /實部imag=0; /虛部Scomplex(double x,double y)real=x;imag=y;Scomplex& operator =(Scomplex s);double getreal()return real;double getimag()return imag;friend Scomplex operator+(int i,Scomplex s); friend Scomplex operator+(double d,Scomplex s); friend Scomplex operator-(int i,Scomplex s); friend Scomplex operator-(double d,Scomplex s);Scomplex& Scomplex:operator =(Scomplex s)if(this=&s) return *this;real=s.real;imag=s.imag;return *this;Scomplex operator+(int i,Scomplex s)Scomplex t;t.real=i+s.real;t.imag=s.imag;return t;Scomplex operator+(double d,Scomplex s)Scomplex t;t.real=d+s.real;t.imag=s.imag;return t;Scomplex operator-(int i,Scomplex s)Scomplex t;t.real=i-s.real;t.imag=s.imag;return t;Scomplex operator-(double d,Scomplex s)Scomplex t;t.real=d-s.real;t.imag=s.imag;return t;void main()Scomplex s1(3.4,5.2),s2;s2=1+s1; cout<<"復數(shù)s2是:("<<s2.getreal()<<,<<s2.getimag()<<)<<endl;s2=6.2+s1; cout<<"復數(shù)s2是:("<<s2.getreal()<<,<<s2.getimag()<<)<<endl;s2=5-s1;cout<<"復數(shù)s2是:("<<s2.getreal()<<,<<s2.getimag()<<)<<endl;s2=3.2-s1;cout<<"復數(shù)s2是:("<<s2.getreal()<<,<<s2.getimag()<<)<<endl;運行結果:實驗(二)源程序:#include<iostream.h>class Point3Dpublic:Point3D()x=1;y=1;z=1;Point3D(int a,int b,int c) x=a;y=b;z=c;int getx()return x;int gety()return y;int getz()return z;Point3D& operator =(Point3D p);Point3D operator +(Point3D p);Point3D operator -(Point3D p);bool operator =(Point3D p);private:int x,y,z;Point3D& Point3D:operator =(Point3D p)if(this=&p) return *this;x=p.x;y=p.y;z=p.z;return *this;Point3D Point3D:operator +(Point3D p) Point3D t;t.x=x+p.x;t.y=y+p.y;t.z=z+p.z;return t;Point3D Point3D:operator -(Point3D p)Point3D t;t.x=x-p.x;t.y=y-p.y;t.z=z-p.z;return t;bool Point3D:operator =(Point3D p)if(x=p.x&&y=p.y&&z=p.z)return true;elsereturn false;void main()Point3D p1(1,2,3),p2(1,2,3),p3,p4;p3=p1+p2;cout<<"兩點相加后為:("<<p3.getx()<<","<<p3.gety()<<","<<p3.getz()<<")"<<endl;p4=p2-p1;cout<<"兩點相減后為:("<<p4.getx()<<","<<p4.gety()<<","<<p4.getz()<<")"<<endl;if(p1=p2)cout<<"p1=p2"<<endl;elsecout<<"p1!=p2"<<endl;運行結果:實驗(三)源程序:#include<iostream.h>class Timepublic:Time()hour=0;minute=0;second=0;Time(int h,int m,int s)hour=h;minute=m;second=s;void setHour(int h)hour=h;void setMinute(int m)minute=m;void setSecond(int s)second=s;int getHour()return hour;int getMinute()return minute;int getSecond()return second;void displayTime()cout<<hour<<":"<<minute<<":"<<second<<endl;Time operator + (Time);Time operator - (Time);private:int hour;int minute;int second;Time Time:operator+(Time t)int carry,hh,mm,ss;ss=second+t.getSecond();if(ss>60)ss-=60;carry=1;else carry=0;mm=minute+t.getMinute()+carry;if(mm>60)mm-=60;carry=1;else carry=0;hh=hour+t.getHour()+carry;if(hh>24)hh-=24;Time tt(hh,mm,ss);return(tt);Time Time:operator-(Time t)int borrow,hh,mm,ss;ss=second-t.getSecond();if(ss<0)ss+=60;borrow=1;else borrow=0;mm=minute-t.getMinute()-borrow;if(mm<0)mm+=60;borrow=1;else borrow=0;hh=hour-t.getHour()-borrow;if(hh<0) hh+=24;Time tt(hh,mm,ss);return(tt);void main()Time t1(13,2,40),t2(15,37,30),t3;t3=t1+t2;t3.displayTime();t3=t1-t2;t3.displayTime();運行結果:

注意事項

本文(面向對象程序設計(C++)實驗報告)為本站會員(仙***)主動上傳,裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對上載內(nèi)容本身不做任何修改或編輯。 若此文所含內(nèi)容侵犯了您的版權或隱私,請立即通知裝配圖網(wǎng)(點擊聯(lián)系客服),我們立即給予刪除!

溫馨提示:如果因為網(wǎng)速或其他原因下載失敗請重新下載,重復下載不扣分。




關于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權所有   聯(lián)系電話:18123376007

備案號:ICP2024067431-1 川公網(wǎng)安備51140202000466號


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權或隱私,請立即通知裝配圖網(wǎng),我們立即給予刪除!