課程設(shè)計(jì)(小題)_第1頁(yè)
課程設(shè)計(jì)(小題)_第2頁(yè)
課程設(shè)計(jì)(小題)_第3頁(yè)
課程設(shè)計(jì)(小題)_第4頁(yè)
課程設(shè)計(jì)(小題)_第5頁(yè)
已閱讀5頁(yè),還剩8頁(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、1 函數(shù)重載定義重載函數(shù)max3用于計(jì)算三個(gè)數(shù)的最大值(參數(shù)類型分別為int和double)。#include<iostream>using namespace std;int max3(int x, int y, int z)return (x > y ? x : y) > (x > z ? x : z) ? (x > y ? x : y) : (x > z ? x : z);double max3(double x, double y, double z)return (x > y ? x : y) > (x > z ? x :

2、z) ? (x > y ? x : y) : (x > z ? x : z);void main()int a, b, c;cout << "Please input three integer:"cin >> a >> b >> c;cout << "The Max of three is " << max3(a, b, c) << endl;double a1, b1, c1;cout << "Please input three r

3、eal number:"cin >> a1 >> b1 >> c1;cout << "The Max of three is " << max3(a1, b1, c1) << endl;2 類的組合定義point類,數(shù)據(jù)成員包括x,y,成員函數(shù)包括構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù),以及setx,getx,sety,gety四個(gè)屬性函數(shù)。定義line類,端點(diǎn)由兩個(gè)point類的對(duì)象組成,包括構(gòu)造函數(shù),析構(gòu)函數(shù)以及計(jì)算線段長(zhǎng)度的函數(shù)getlength。在main函數(shù)中,定義line的對(duì)象,并輸出其

4、長(zhǎng)度。#include<iostream>#include<cmath>using namespace std;class pointprivate:double x, y;public:point()point(double x, double y) :x(x), y(y)void setx(double xx)x = xx;double getx() return x; void sety(double yy)y = yy;double gety() return y; point(point & p)x = p.x;y = p.y;point();clas

5、s lineprivate:point a, b;public:line(point aa,point bb) :a(aa), b(bb)double getlength()double length;length = sqrt(pow(a.getx() - b.getx(), 2) + pow(a.gety() - b.gety(), 2);return length;line();void main()point p1(2, 3);point p2(5, 6);line l1(p1,p2);cout << "The length of the line is &quo

6、t; << l1.getlength()<<endl;3 對(duì)象數(shù)組和函數(shù)定義student類,數(shù)據(jù)成員包括姓名name和成績(jī)score,成員函數(shù)包括構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù)。定義函數(shù)void highestscore(student s),輸出分?jǐn)?shù)最高的學(xué)生姓名和分?jǐn)?shù)。在main函數(shù)中定義student sN,調(diào)用highestscore函數(shù),輸出分?jǐn)?shù)最高的學(xué)生姓名和分?jǐn)?shù)。#include<iostream>#include<iomanip>#include<string>const int N = 2;using names

7、pace std;class studentprivate:string name;double score;public:student()student(string n, double s) :name(n), score(s)student(student &s)name = ;score = s.score;/*void setname(string nn)name = nn;string getname() return name; void setscore(double ss)score = ss;double getscore() return score

8、; */void highestscore(student s)int k = 0;double max;max = s0.score;for (int i = 1; i < N; i+)if (max < si.score)max = si.score;k = i;cout << "The name is " << << endl;cout << "Highestscore is " << sk.score << endl;student()friend

9、 ostream & operator <<(ostream & os, student s)os << << " " << s.score << endl;return os;friend istream & operator >>(istream & is, student &s)is >> >> s.score;return is;void main()student sN;for (int i = 0;

10、i < N; i+)cin >> si;sN.highestscore(s);4 靜態(tài)數(shù)據(jù)成員設(shè)計(jì)一個(gè)書類,能夠保存書名、定價(jià),所有書的本數(shù)和總價(jià)。(將書名和定價(jià)設(shè)計(jì)為普通數(shù)據(jù)成員;將書的本數(shù)和總價(jià)設(shè)計(jì)為靜態(tài)數(shù)據(jù)成員)class bookprivate:double priece;string name;static int num;static double total;int book:num;double book:total;5 動(dòng)態(tài)內(nèi)存分配定義point類,數(shù)據(jù)成員包括x,y,

11、成員函數(shù)包括構(gòu)造函數(shù),拷貝構(gòu)造函數(shù)和析構(gòu)函數(shù),以及setx,getx,sety,gety四個(gè)屬性函數(shù)。在main函數(shù)中,用new和delete分配和釋放N個(gè)point的數(shù)組。(N是const常量,N=10)#include<iostream>const int N = 10;using namespace std;class pointprivate:int x, y;public:point()point(int x, int y) :x(x), y(y)point(point & p)x = p.x;y = p.y;void setx(int xx)x = xx;int

12、 getx() return x; void sety(int yy)y = yy;int gety() return y; point();void main()point *p = new pointN;delete p;6 類的繼承定義一個(gè)point類,包含私有數(shù)據(jù)成員x,y,成員函數(shù)包括無(wú)參構(gòu)造函數(shù),帶參構(gòu)造函數(shù),set和get屬性函數(shù)。定義circle類,從point類公有派生,增加數(shù)據(jù)成員半徑r,成員函數(shù)包括無(wú)參構(gòu)造函數(shù),帶參構(gòu)造函數(shù),計(jì)算面積函數(shù)getarea。在main函數(shù)中定義一個(gè)circle的對(duì)象,并計(jì)算其面積。#include<iostream>using n

13、amespace std;class pointprivate:double x, y;public:point()point(double x, double y) :x(x), y(y)void setx(double xx)x = xx;double getx() return x; void sety(double yy)y = yy;double gety() return y; point();class circle :public pointprivate:double r;public:circle()circle(double x, double y, double r)

14、:point(x, y), r(r)double getarea()return 3.14*r*r;void main()circle c1(2, 3, 4.5);cout << "The area of circle is " << c1.getarea() << endl;7 虛基類定義vehicle類,數(shù)據(jù)成員包括私有的weight,公有的構(gòu)造函數(shù),析構(gòu)函數(shù)和輸出函數(shù)dispaly;從vehicle類公有派生car類,增加數(shù)據(jù)成員載人數(shù)personnum,公有的構(gòu)造函數(shù),析構(gòu)函數(shù)和輸出display;從vehicle類公有派生tru

15、ck類,增加數(shù)據(jù)成員載貨量laod,公有的構(gòu)造函數(shù),析構(gòu)函數(shù)和輸出函數(shù)display;從car類和truck類共同公有派生出pickup類,包括公有的構(gòu)造函數(shù)和輸出函數(shù)。在main函數(shù)中,定義pickup類對(duì)象,并輸出其基本信息。#include<iostream>using namespace std;class vehicleprivate:double weight;public:vehicle(double w) :weight(w)void display()cout << "the weight of vehicle is " <&

16、lt; weight << endl;vehicle();class car :virtual public vehicleprivate:int personnum;public:car(double ww, int p) :vehicle(ww), personnum(p)void display()/vehicle:display();cout << "The number of person is " << personnum << endl;car();class truck :virtual public vehi

17、cleprivate:double load;public:truck(double ww, double l) :vehicle(ww), load(l)void display()/vehicle:display();cout << "The load of the truck is " << load << endl;truck();class pickup :public car, public truckpublic:pickup(double w1, int p, double w2, double l, double w)

18、:car(w1, p), truck(w2, l), vehicle(w)void display()vehicle:display();car:display();truck:display();pickup();void main()pickup p1(211, 2, 200, 170,100);p1.display();8 運(yùn)算符重載,友元函數(shù)和this指針定義一個(gè)計(jì)數(shù)器類counter,具備自增,自減功能(前后綴);輸入輸出>>,<<功能。在main函數(shù)里測(cè)試該類。#include<iostream>using namespace std;class

19、 counterprivate:double num;public:counter()counter(double n) :num(n)counter operator+()+num;return *this;counter operator-()-num;return *this;counter operator+(int)counter nn;nn.num = this->num;num+;return nn;counter operator-(int)counter nn;nn.num = this->num;num-;return nn;friend ostream &am

20、p; operator << (ostream & os, counter c);friend istream & operator >> (istream & is, counter &c);ostream & operator << (ostream & os, counter c)os << c.num << endl;return os;istream & operator >> (istream & is, counter &c)is >

21、;> c.num;return is;void main()counter c1,c2;cin >> c1;c2 = c1.operator+(0);cout << "c1=" << c1;cout << "c2=" << c2;9 虛函數(shù)和抽象類定義一個(gè)抽象類shape,包括公有的計(jì)算面積area函數(shù),計(jì)算體積volume函數(shù),輸出基本信息函數(shù)printinfo(三個(gè)函數(shù)均為純虛函數(shù))。從shape公有派生point類,增加私有數(shù)據(jù)成員x,y坐標(biāo),以及構(gòu)造函數(shù),析構(gòu)函數(shù)。從point公有

22、派生circle類,增加私有數(shù)據(jù)成員半徑r,以及構(gòu)造函數(shù),析構(gòu)函數(shù)。從circle公有派生cylinder類,增加私有數(shù)據(jù)成員高度h,以及構(gòu)造函數(shù),析構(gòu)函數(shù)。(在定義三個(gè)派生類的過(guò)程中,自己考慮需要重定義哪個(gè)虛函數(shù))。在main函數(shù)中,定義shape類的指針,指向派生類的對(duì)象,輸出三類對(duì)象的基本信息,面積,體積;(將shape指針改為引用再嘗試)。#include<iostream>using namespace std;class shapepublic:virtual double area() = 0;virtual double volume() = 0;virtual v

23、oid printinfo() = 0;class point :public shapeprivate:int x, y;public:point(int x, int y) :x(x), y(y)void printinfo()cout << "x=" << x << ",y=" << y << endl;point();class circle :public pointprivate:double r;public:circle(int x, int y, double r) :poin

24、t(x, y), r(r)double area()return 3.14*r*r;void printinfo()point:printinfo();cout << "r=" << r << endl;/cout << "circle area is " << area() << endl;circle();class cylinder :public circleprivate:double h;public:cylinder(int x, int y, double r, do

25、uble h) :circle(x, y, r), h(h)double area()return circle:area();double volume()return circle:area()*h;void printinfo()circle:printinfo();cout << "h=" << h << endl;/cout << "volume is " << volume() << endl;cylinder();void main()cylinder c1(2, 3,

26、 4, 5);shape *s;s = &c1;(*s).printinfo();cout << "circle area is " << s->area() << endl;cout << "volume is " << (*s).volume() << endl;10 模板設(shè)計(jì)一個(gè)堆棧的類模板Stack,在模板中用類型參數(shù)T表示棧中存放的數(shù)據(jù),用非類型參數(shù)MAXSIZE代表?xiàng)5拇笮 ?include<iostream>/const int MAXSIZE

27、 = 5;using namespace std;template<typename T,int MAXSIZE>class stackprivate:T aMAXSIZE;int top;public:stack() :top(0)void push(int x)if (top = MAXSIZE)cout << "the stack is full" << endl;elseatop = x;top+;T pop()if (top = 0)cout << "the stack is empty" <

28、;< endl;elsetop-;return atop;void main()stack<int,5> s;s.push(1);cout << s.pop() << endl;11 文件讀寫定義學(xué)生類數(shù)組,有N個(gè)人(N=5),包括姓名和語(yǔ)數(shù)外三名課的成績(jī),通過(guò)重載<<和>>運(yùn)算符實(shí)現(xiàn)學(xué)生數(shù)組的文件讀寫。(姓名用string name;)#include<iostream>#include<fstream>#include<string>#include<iomanip>const int N = 3;using namespace std;class studentprivate:char name10;double Chinese, Math, English;public:student()student(char n, double

溫馨提示

  • 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)論