




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上C+第四次上機答案1. 1)建立一個教師類Teacher,其包含3個私有數(shù)據(jù)成員:num(工號)、name(姓名)、sex(性別),包含2個公有成員函數(shù):帶參構造函數(shù)(用于對三個數(shù)據(jù)成員進行初始化),display函數(shù)(輸出數(shù)據(jù)成員)。2)建立一個生日類BirthDate,其包含3個私有數(shù)據(jù)成員:year(年)、month(月)、day(日),包含3個公有成員函數(shù):帶參構造函數(shù)(用于對三個數(shù)據(jù)成員進行初始化),display函數(shù)(輸出數(shù)據(jù)成員),change函數(shù)(修改生日數(shù)據(jù),即將3個數(shù)據(jù)成員的值改為change函數(shù)參數(shù)指定的值)。3)建立一個教授類Professor
2、,它是Teacher類的公有派生類,在Teacher類的基礎上增加了私有數(shù)據(jù)生日信息birthday(birthday是BirthDate類的對象),其新增成員函數(shù)包含:帶參構造函數(shù)(對新增的和繼承的數(shù)據(jù)成員初始化),display函數(shù)(輸出所有數(shù)據(jù)成員),changebirth函數(shù)(將修改生日信息修改為函數(shù)參數(shù)指定日期)。4)main函數(shù)中的操作步驟如下:l 定義Professor類的對象prof1l 輸出對象prof1的所有數(shù)據(jù)信息l 修改prof1的生日信息l 輸出對象prof1的所有數(shù)據(jù)信息#include <iostream>#include <string>
3、using namespace std;class Teacherpublic:Teacher(int, string, char);void display(); /輸出數(shù)據(jù)private:int num; /工號string name; /姓名char sex; /性別;Teacher:Teacher(int n, string na, char s): num(n), name(na), sex(s) void Teacher:display() /輸出數(shù)據(jù)cout<<"num:"<<num<<endl;cout<<&q
4、uot;name:"<<name<<endl;cout<<"sex:"<<sex<<endl;class BirthDatepublic:BirthDate(int, int, int);void display(); /輸出年、月、日數(shù)據(jù)void change(int, int, int); /修改年、月、日數(shù)據(jù)private:int year; /年int month; /月int day; /日;BirthDate:BirthDate(int y, int m, int d): year(y), m
5、onth(m), day(d) void BirthDate:display() /輸出年、月、日數(shù)據(jù)cout<<"birthday:"<<year<<'/'<<month<<'/'<<day<<endl;void BirthDate:change(int y, int m, int d) /修改年、月、日數(shù)據(jù)year = y;month = m;day = d;class Professor:public Teacherpublic:Professor(in
6、t, string, char, int, int, int);void display(); /輸出數(shù)據(jù)void changebirth(int, int, int); /修改生日信息private:BirthDate birthday; /子對象;Professor:Professor(int n,string na,char s,int y,int m,int d): Teacher(n,na,s), birthday(y,m,d)void Professor:display() /輸出數(shù)據(jù)Teacher:display(); /父子同名函數(shù)birthday.display(); /輸出
7、生日信息void Professor:changebirth(int y,int m,int d) /修改生日信息birthday.change(y,m,d);void main()Professor prof1(1001,"Wangli",'f',1973,5,10);prof1.display(); /調用派生類自己的displaycout<<endl<<endl;prof1.changebirth(1975,10,12); /修改生日信息prof1.display(); /調用派生類自己的display2. 寫一個程序,定義抽象
8、基類Shape,由它派生出3個派生類:Circle(圓形類,含一個數(shù)據(jù)成員:半徑)、Rectangle(矩形類,含兩個數(shù)據(jù)成員:長、寬)、Triangle(三角形類,含三個數(shù)據(jù)成員:三條邊),用一個函數(shù)printArea分別輸出以上三者的面積,3個圖形的數(shù)據(jù)在定義對象時給定。法一:#include <iostream>#include <math.h>using namespace std;/定義抽象基類Shapeclass Shapepublic:virtual double area() =0; /純虛函數(shù);/定義Circle類class Circle: publi
9、c Shapepublic:Circle(double r): radius(r) /構造函數(shù)virtual double area() /virtual可以省略 return 3.14159*radius*radius; ; /定義虛函數(shù)private:double radius; /半徑;/定義Rectangle類class Rectangle: public Shapepublic:Rectangle(double len, double w): length(len), width(w) /構造函數(shù)virtual double area() return length*width; /
10、定義虛函數(shù)private:double length, width; /長與寬;/定義Triangle類class Triangle:public Shapepublic:Triangle(double x, double y, double z): a(x), b(y), c(z) /構造函數(shù)virtual double area() /定義虛函數(shù)double s=(a+b+c)*0.5;return sqrt( s*(s-a)*(s-b)*(s-c) ); private:double a,b,c; /三條邊;/輸出面積的函數(shù)void printArea(Shape &s) /基類
11、的引用對象作為形參,實參可以是子類對象。引用對象比指針變量節(jié)約空間cout<<s.area()<<endl; /area是虛函數(shù),調用的是實參對象所屬類的areaint main()Circle circle(12.6); /建立Circle類對象circlecout<<"area of circle =" printArea(circle); /輸出circle的面積Rectangle rectangle(4.5,8.4); /建立Rectangle類對象rectanglecout<<"area of rectan
12、gle =" printArea(rectangle); /輸出rectangle的面積Triangle triangle(4,8,5); /建立Triangle類對象 cout<<"area of triangle ="printArea(triangle); /輸出triangle的面積return 0;法二:#include <iostream>#include <math.h>using namespace std;/定義抽象基類Shapeclass Shapepublic:virtual void printArea(
13、) =0; /純虛函數(shù);/定義Circle類class Circle: public Shapepublic:Circle(double r): radius(r) /構造函數(shù)virtual void printArea() /virtual可以省略 cout<<3.14159*radius*radius<<endl; ; /定義虛函數(shù)private:double radius; /半徑;/定義Rectangle類class Rectangle: public Shapepublic:Rectangle(double len, double w): length(len
14、), width(w) /構造函數(shù)virtual void printArea() cout<<length*width<<endl; /定義虛函數(shù)private:double length, width; /長與寬;/定義Triangle類class Triangle:public Shapepublic:Triangle(double x, double y, double z): a(x), b(y), c(z) /構造函數(shù)virtual void printArea() /定義虛函數(shù)double s=(a+b+c)*0.5;cout<<sqrt( s
15、*(s-a)*(s-b)*(s-c) )<<endl; private:double a,b,c; /三條邊;int main()Shape *pc; /基類指針變量Circle circle(12.6); /建立Circle類對象circlepc=&circle;cout<<"area of circle =" pc->printArea(); /輸出circle的面積Rectangle rectangle(4.5,8.4); /建立Rectangle類對象rectanglepc=&rectangle;cout<<
16、"area of rectangle =" pc->printArea(); /輸出rectangle的面積Triangle triangle(4,8,5); /建立Triangle類對象 pc=▵cout<<"area of triangle ="pc->printArea(); /輸出triangle的面積return 0;自主上機題目:2015年C+A卷的第五題(編程題)。定義具有繼承關系的點類Point和圓類Circle:(1)Point類具有(x, y)坐標對;(2) Circle類為Point
17、類的子類,它本身又增加了半徑radius、求周長getPeri ()和求面積getArea()的成員函數(shù);(3)為Point類和Circle類添加構造函數(shù);(4)在主函數(shù)中創(chuàng)建圓A,其圓心坐標為(5,7),半徑為2,計算并輸出圓A的周長和面積。#include <iostream.h>#define PI 3.14class Pointfloat x,y;public:Point(float xp=0,float yp=0):x(xp),y(yp);class Circle: public Pointfloat radius; /半徑public:Circle(float x, float y, float r): Point(x,y), radius(r)float getPeri(); /求周長float g
溫馨提示
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 城軌火災專項應急預案(3篇)
- 計算機軟件技術員試題及答案分析指導
- 火災觸電應急預案范文(3篇)
- 《機電一體化設備安裝與調試》課件-學習情景九 組態(tài)軟件在機電一體化設備上和自動生產(chǎn)線上的應用
- 高考作文與文化自信的表達探討試題及答案
- VB編程的藝術與試題及答案的提升
- 2025年VB考試經(jīng)驗分享與試題答案
- VB編程思維試題及答案
- 2025年行業(yè)整合與公司戰(zhàn)略試題及答案
- 高教與經(jīng)濟發(fā)展的聯(lián)系探討試題及答案
- 國際貿(mào)易學課件:關稅
- 校園食品安全智慧化建設與管理規(guī)范
- 檢驗科事故報告制度
- 精細化學品化學智慧樹知到期末考試答案章節(jié)答案2024年青島科技大學
- 分包合同模板
- 多元主體協(xié)同治理
- 舞蹈基本功訓練與舞蹈鑒賞智慧樹知到期末考試答案章節(jié)答案2024年蘭州文理學院
- 《化妝品原料》課件-油脂的基本特性
- 中西文化鑒賞智慧樹知到期末考試答案章節(jié)答案2024年鄭州大學
- 關節(jié)黏連松解手術
- 英語定位紙模板
評論
0/150
提交評論