實驗4繼承與派生_第1頁
實驗4繼承與派生_第2頁
實驗4繼承與派生_第3頁
實驗4繼承與派生_第4頁
實驗4繼承與派生_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、實驗4 繼承與派生班級 網(wǎng)絡1311 學號 39 姓名 付豪 成績 一、實驗目的1. 熟練掌握類的繼承,能夠定義和使用類的繼承關系2. 掌握派生類的聲明與實現(xiàn)方法3. 掌握類構造函數(shù)的初始化列表與作用域分辨率的使用方法4. 理解虛基類在解決二義性問題中的作用.二、實驗內(nèi)容1定義一個基類有姓名、性別、年齡,再由基類派生出教師類和學生類,教師類增加工號、職稱和工資,學生類增加學號、班級、專業(yè)和入學成績,在main()函數(shù)中定義基類和派生類對象,對類進行測試。2聲明一個哺乳動物Mammal類,再由此派生出狗Dog類,聲明一個Dog類的對象,觀察基類與派生類的構造函數(shù)與析構函數(shù)的調(diào)用順序。3定義一個P

2、oint類,派生出矩形類Rectangle和圓類Circle,計算各派生類對象的面積Area()。4設計一個圓類circle和一個桌子類table,另設計一個圓桌類roundtable,它是從前兩個類派生的,要求輸出一個圓桌的高度、面積和顏色等數(shù)據(jù)。 5定義一個大學生類student,函數(shù)私有數(shù)據(jù)成員:姓名、學號、校名,并為它定義帶參數(shù)的構造函數(shù),參數(shù)帶缺省值的構造函數(shù)和輸出數(shù)據(jù)成員值的print()公有成員函數(shù),另定義研究生類,它以公有繼承方式派生于類student,新增加“研究方向、導師名”兩個私有數(shù)據(jù)成員,并定義帶參數(shù)的構造函數(shù)和輸出研究生數(shù)據(jù)的print()公有成員函數(shù)。在main()

3、函數(shù)中定義基類和派生類對象,對類進行測試。三、實驗源程序、測試與結論1#include<iostream>#include<string>using namespace std;class personstring name; char sex;int age;public:person(string n,char s,int a):name(n),sex(s),age(a) void show()cout<<name<<endl; cout<<sex<<endl;cout<<age<<endl;c

4、lass teacher:public personint num; string job; int money;public:teacher(string n,char s,int a,int nu,string j,int m):person(n,s,a),num(nu),job(j),money(m) void show()person:show();cout<<num<<endl;cout<<job<<endl;cout<<money<<endl;class student:public personint num

5、; string Class; string zhuanye; float sorce;public:student(string n,char s,int a,int nu,string C,string z,float ss):person(n,s,a),num(nu),Class(C),zhuanye(z),sorce(ss) void show()person:show(); cout<<num<<endl;cout<<Class<<endl;cout<<zhuanye<<endl;cout<<sorc

6、e<<endl;void main() teacher t1("Fsda",'M',1234,74,"jiaoshi",8000); student s1("Hfgh",'F',1145,1001,"BX1311","wangluo",511); t1.show();cout<<endl<<endl;s1.show();2#include<iostream>using namespace std;class Mamm

7、alpublic:Mammal()cout<<"Mammal構造"<<endl;Mammal()cout<<"Mammal析構"<<endl;class Dog:public Mammalpublic:Dog()cout<<"Dog構造"<<endl;Dog()cout<<"Dog析構"<<endl;void main() Dog a;3.#include<iostream>using namespace s

8、td;class Pointdouble x;double y;public:Point(double x,double y):x(x),y(y)void show()cout<<"("<<x<<","<<y<<")"<<endl;class Rectangle:public Point double x, y;public:Rectangle(double x,double y,double x1,double y1):Point(x,y),x(x1),y(y1

9、)void Area()Point:show();cout<<"Ãæ»ý:"<<x*y<<endl;class Circle:public Pointdouble r;public:Circle(double x,double y,double c):Point(x,y),r(c)void Area()Point:show();cout<<"Ãæ»ý:"<<r*r*3.14<<endl;void main

10、() Rectangle f2(5,6,7,8); Circle f3(1,2,3);f2.Area();f3.Area();4.#include<iostream>#include<string>using namespace std;class Circleint r;public:Circle(int r1)r=r1;class Tableint h;string color;public:Table(int h1,string c1)h=h1;color=c1;class Roundtable:public Circle,public Tablepublic:R

11、oundtable(int x,int y,string z):Circle(x),Table(y,z) cout<<"高度:"<<x<<"面積:"<<y<<"顏色:"<<z<<endl;void main() string c; cout<<"請輸入顏色:"<<endl;cin>>c;Roundtable a(7,11,c);5.#include<iostream>#include&

12、lt;string>using namespace std;class student protected: string name; int num; string school;public:student(string n="hi",int a=0,string b="bey"):name(n),num(a),school(b)void print() cout<<"name:"<<name<<endl; cout<<"num:"<< num&

13、lt;<endl; cout<<"school:"<<school<<endl;class graduate:public student string research_area;string tutor_name;public:graduate(string a,int b,string c,string d,string e): student(a,b,c),research_area(d),tutor_name(e)void print() cout<<"name:"<<name&

14、lt;<endl; cout<<"num:"<< num<<endl; cout<<"school:"<<school<<endl;/*/student:print();cout<<"research_area:"<<research_area<<endl;cout<<"tutor_name:"<<tutor_name<<endl;void main() graduate g("Fsda",17,"DJ","wangluo","Tao"); cout<<"graduate:"<<endl

溫馨提示

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

評論

0/150

提交評論