C++_編程題庫_第1頁
C++_編程題庫_第2頁
C++_編程題庫_第3頁
C++_編程題庫_第4頁
C++_編程題庫_第5頁
已閱讀5頁,還剩56頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、.31定義盒子Box類,要求具有以下成員:長、寬、高分別為x,y,z,可設(shè)置盒子形狀;可計算盒子體積;可計算盒子的表面積。#include #include using namespace std; class Boxpublic:int weight;int length;int hight; void box_shape(int w, int l, int h);int box_volume(int w, int l, int h);int box_area(int w, int l, int h);int main()Box mybox;cout mybox.weight mybox.l

2、ength mybox.hight;int box_v, box_a; mybox.box_shape(mybox.weight, mybox.length, mybox.hight);box_v = mybox.box_volume(mybox.weight, mybox.length, mybox.hight);cout This boxs volume = box_v endl;box_a = mybox.box_area(mybox.weight, mybox.length, mybox.hight);cout This boxs area = box_a endl;void Box:

3、box_shape(int w, int l, int h)if(w = l & l = h)cout This is a Cube! endl;elsecout This is a cuboid! endl; int Box:box_volume(int w, int l, int h)return w * l * h;int Box:box_area(int w, int l, int h)return 2 * w * l + 2 * l * h + 2 * w * h;32 有兩個長方柱,其長、寬、高分別為:(1)30,20,10;(2)12,10,20。分別求他們的體積。編一個基于對象

4、的程序,在類中用帶參數(shù)的構(gòu)造函數(shù)。#include class Boxprivate:int length;int weight;int hight;public:Box(int, int, int);int volume();int main()using namespace std;Box mybox1(30, 20, 10);cout The first Boxs volume = mybox1.volume() endl;Box mybox2(12, 10, 30);cout The second Boxs volume = mybox2.volume() endl;return 0;

5、Box:Box(int l, int w, int h)length = l;weight = w;hight = h;int Box:volume()return (hight * weight * length);33 有兩個長方柱,其長、寬、高分別為:(1)12,20,25;(2)10,30,20。分別求他們的體積。編一個基于對象的程序,且定義兩個構(gòu)造函數(shù),其中一個有參數(shù),一個無參數(shù)。#include class Boxprivate:int length;int wight;int height;public:Box();Box(int, int, int);int volume();

6、int main()using namespace std;Box mybox1;cout The first boxs volume = mybox1.volume() endl;Box mybox2(10, 30, 20);cout The second boxs volume = mybox2.volume() endl;return 0;Box:Box()length = 12;wight = 20;height = 25;Box:Box(int l, int w, int h)length = l;wight = w;height = h;int Box:volume()return

7、 length * wight * height;34 聲明一個類模板,利用它分別實現(xiàn)兩個整數(shù)、浮點數(shù)和字符的比較,求出大數(shù)和小數(shù)。 #include using namespace std;templateclass Comparepublic:Compare(numtype a,numtype b)x=a;y=b;numtype max()return (xy)?x:y;numtype min()return (xy)?x:y;private:numtype x,y;int main()Comparecmp1(3,4);coutcmp1.max() is the Maximum of tw

8、o inteder numbers.endl; coutcmp1.min() is the Minimum of two inteder numbers.endlendl;Compare cmp2(45.78,93.6); coutcmp2.max() is the Maximum of two float numbers.endl; coutcmp2.min() is the Minimum of two float numbers.endlendl; Compare cmp3(a,A); coutcmp3.max() is the Maximum of two characters.end

9、l; coutcmp3.min() is the Minimum of two characters.endl; return 0; 35 建立一個對象數(shù)組,內(nèi)放5個學(xué)生的數(shù)據(jù)(學(xué)號、成績),用指針指向數(shù)組首元素,輸出第1,3,5個學(xué)生的數(shù)據(jù)。初值自擬。#includeusing namespace std;class Studentpublic:Student(int n,int s):num(n),score(s)void display()coutnum scoreendl;private:int num;int score;int main()Student stud5=Student

10、(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74);Student *p=stud;for(int i=0;idisplay();return 0;36 建立一個對象數(shù)組,內(nèi)放5個學(xué)生的數(shù)據(jù)(學(xué)號、成績),設(shè)立一個函數(shù)max,用指向?qū)ο蟮闹羔樧骱瘮?shù)參數(shù),在max函數(shù)中找出5個學(xué)生中成績最高者,并輸出其學(xué)號。初值自擬。#includeusing namespace std;class Studentpublic:Student(int n,int s):num(n),score(s)int num;int sco

11、re;int main()Student stud5=Student(01,70),Student(02,71),Student(03,72),Student(04,73),Student(05,74);void max(Student *);Student *p=&stud0;max(p);return 0;void max(Student *arr)float max_score=arr0.score;for(int i=0;i5;i+)if(max_scorearri.score)max_score=arri.score;coutmax_score arri.numendl;38. 定義

12、一個復(fù)數(shù)類Complex,重載運算符“+”,使之能用于復(fù)數(shù)的加法運算。將運算符函數(shù)重載為非成員、非友元的普通函數(shù)。編寫程序,求兩個復(fù)數(shù)之和。初值自擬。#include class Complexprivate:double real;double image;public:Complex();Complex(double, double);double get_real();double get_image();Complex operator + (Complex &, Complex &);int main()Complex s1(3, 4);Complex s2(5, -10);Comp

13、lex c3;c3 = s1 + s2;std:cout s1 + s2 = ;std:cout ( c3.get_real() , c3.get_image() i) std:endl;/Complex s1(3, 4);/std:cout s1.get_real() std:endl;return 0;Complex:Complex(double r, double i)real = r;image = i;Complex:Complex()real = 0;image = 0;double Complex:get_real()return real;double Complex:get_

14、image()return image;Complex operator + (Complex & s1, Complex & s2)Complex c(s1.get_real() + s2.get_real(), s1.get_image() + s2.get_image();return c;39 定義一個復(fù)數(shù)類Complex,重載運算符“”,“”,使之能用于復(fù)數(shù)的加,減運算,運算符重載函數(shù)作為Complex類的成員函數(shù)。編程序,分別求出兩個復(fù)數(shù)之和,差。初值自擬。#include class Complexprivate:double real;double image;public:C

15、omplex();Complex(double, double);Complex operator + (Complex &);Complex operator - (Complex &);void display();int main()Complex s1(3, 4), s2(5, 6), c;c = s1 + s2;c.display();c = s1 - s2;c.display();return 0;Complex:Complex()real = 0;image = 0;Complex:Complex(double r, double i)real = r;image = i;Com

16、plex Complex:operator + (Complex & s1)Complex c;c.real = s1.real + real;c.image = s1.image + image;return c;Complex Complex:operator - (Complex & s1)Complex c;c.real = real - s1.real;c.image = image - s1.image;return c;void Complex:display()using namespace std;cout real image endl;40. 定義一個復(fù)數(shù)類Complex

17、,重載運算符“”,使之能用于復(fù)數(shù)的加法運算。參加運算的兩個運算量可以都是類對象,也可以其中有一個是整數(shù),順序任意。例如:c1+c2,i+c1,c1+i均合法(設(shè)i為整數(shù),c1,c2為復(fù)數(shù))。編程序,分別求兩個復(fù)數(shù)之和、整數(shù)和復(fù)數(shù)之和。初值自擬。#include class Complexprivate:double real;double imag;public:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex &c2);Complex operator+(int &

18、i);friend Complex operator+(int&,Complex &);void display();Complex Complex:operator+(Complex &c)return Complex(real+c.real,imag+c.imag);Complex Complex:operator+(int &i)return Complex(real+i,imag);void Complex:display()using namespace std;cout(real,imagi)endl;Complex operator+(int &i,Complex &c)retu

19、rn Complex(i+c.real,c.imag);int main()using namespace std; Complex c1(3,4),c2(5,-10),c3;int i=5;c3=c1+c2;coutc1+c2=;c3.display();c3=i+c1;couti+c1=;c3.display();c3=c1+i;coutc1+i=;c3.display();return 0;41. 有兩個矩陣a和b,均為2行3列。求兩個矩陣之和。重載運算符“+”,使之能用于矩陣相加。如c=a+b。初值自擬。#include using namespace std;class Matrix

20、 /定義Matrix類 public: Matrix(); /默認構(gòu)造函數(shù) friend Matrix operator+(Matrix &,Matrix &); /重載運算符“+” void input(); /輸入數(shù)據(jù)函數(shù) void display(); /輸出數(shù)據(jù)函數(shù) private: int mat23; ;Matrix:Matrix() /定義構(gòu)造函數(shù)for(int i=0;i2;i+) for(int j=0;j3;j+) matij=0;Matrix operator+(Matrix &a,Matrix &b) /定義重載運算符“+”函數(shù)Matrix c; for(int i=0

21、;i2;i+) for(int j=0;j3;j+) c.matij=a.matij+b.matij; return c; void Matrix:input() /定義輸入數(shù)據(jù)函數(shù)coutinput value of matrix:endl; for(int i=0;i2;i+) for(int j=0;jmatij;void Matrix:display() /定義輸出數(shù)據(jù)函數(shù)for (int i=0;i2;i+) for(int j=0;j3;j+) coutmatij ; coutendl;int main()Matrix a,b,c; a.input(); b.input(); co

22、utendlMatrix a:endl; a.display(); coutendlMatrix b:endl; b.display(); c=a+b; /用重載運算符“+”實現(xiàn)兩個矩陣相加 coutendlMatrix c = Matrix a + Matrix b :endl; c.display(); return 0;42. 將運算符“”重載為適用于復(fù)數(shù)加法,重載函數(shù)不作為成員函數(shù),而放在類外,作為Complex類的友元函數(shù)。初值自擬。#include class Compareprivate:int real;int imag;public:Compare();Compare(int

23、, int);friend Compare operator+(Compare & c1, Compare & c2);void display();int main()Compare c1(3, 4), c2(2, 5), c3;c3 = c1 + c2;c3.display();return 0;Compare:Compare()real = 0;imag = 0;Compare:Compare(int r, int i)real = r;imag = i;void Compare:display()using namespace std;cout real imag i endl;Com

24、pare operator+(Compare & c1, Compare & c2)Compare c(c1.real + c2.real, c1.imag + c2.imag);return c;43. 定義一個字符串類String,用來存放不定長的字符串,重載運算符“”,用于兩個字符串的等于比較運算。初值自擬。44. 定義一個字符串類String,用來存放不定長的字符串,重載運算符,用于兩個字符串的大于的比較運算。初值自擬。#include #include using namespace std;class String public:String() p=NULL;String(cha

25、r *str);friend bool operator(String &string1,String &string2);friend bool operator(String &string1,String &string2);friend bool operator=(String &string1,String &string2);void display();private:char *p; ;String:String(char *str) p=str; void String:display() cout(String &string1,String &string2) if(s

26、trcmp(string1.p,string2.p)0) return true;else return false; bool operator(String &string1,String &string2) if(strcmp(string1.p,string2.p)(string1,string2)=1) string1.display();cout;string2.display(); else if(operator(string1,string2)=1) string1.display();cout;string2.display(); else if(operator=(str

27、ing1,string2)=1) string1.display();cout=;string2.display(); coutendl; int main() String string1(Hello),string2(Book), string3(Computer),string4(Hello); compare(string1,string2); compare(string2,string3); compare(string1,string4); return 0; 46. 定義一個描述學(xué)生基本情況的類,數(shù)據(jù)成員包括姓名、學(xué)號、C+成績、英語和數(shù)學(xué)成績,成員函數(shù)包括輸出數(shù)據(jù),求出總成績

28、和平均成績。數(shù)據(jù)自擬。#include class Studentprivate:char * name;int id;int cpp_source;int eng_source;int math_source;public:Student(char *, int, int, int, int);void sum_source();void avg_source();int main()Student s(John, 1, 90, 80, 97);s.sum_source();s.avg_source();return 0;Student:Student(char * n, int i, in

29、t cs, int es, int ms)name = n;id = i;cpp_source = cs;eng_source = es;math_source = ms;void Student:sum_source()std:cout name The sum of source: cpp_source+eng_source+math_source std:endl; void Student:avg_source()double avg;avg = (cpp_source + eng_source + math_source) / 3;std:cout The avg of source

30、: avg std:endl;47. 先建立一個Point(點)類,包含數(shù)據(jù)成員x,y(坐標(biāo)點)。以它為基類,派生出一個Circle(圓)類,增加數(shù)據(jù)成員r(半徑),再以Circle類為直接基類,派生出一個Cylinder(圓柱體)類,在增加數(shù)據(jù)成員h(高)。編寫程序,重載運算符“”,使之能夠用于輸出以上類對象。#include class Pointprotected:int x, y;public:Point()x = 0, y = 0;Point(int a, int b)this-x = a;this-y = b;void setX(int a)x = a;void setY(int

31、b)y = b;int getX()return x;int getY()return y;class Circle:public Pointprotected:int r;public:Circle(int x, int y, int r):Point(x, y)this-r = r;void setR(int a)r = a;int getR()return r;class Cylinder:public Circleprotected:int h;public:Cylinder():Circle(0, 0, 0),h(0)Cylinder(int x, int y, int r, int

32、 h):Circle(x, y, r)this-h = h;void setH(int a)h = a;int getH()return h;friend std:istream & operator(std:istream &, Cylinder &);friend std:ostream & operator(std:istream & input, Cylinder & cc)int _x, _y, _r, _h;std:cout Enter the Cylinder: _x _y _r _h;cc.setX(_x);cc.setY(_y);cc.setR(_r);cc.setH(_h)

33、;return input;std:ostream & operator(std:ostream & os, Cylinder & cc)os cc.getX() cc.getY() cc.getR() cc.getH() cc;cout cc;return 0;48. 寫一個程序,定義抽象類型Shape,由他派生三個類:Circle(圓形),Rectangle(矩形),Trapezoid(梯形),用一個函數(shù)printArea分別輸出三者的面積,3個圖形的數(shù)據(jù)在定義對象是給定。#include class Shapepublic:virtual double area() const = 0;

34、class Circle:public Shapeprivate:double r;public:Circle(double a):r(a)virtual double area() constreturn 3.14 * r * r;class Rectangle:public Shapeprivate:double h, w;public:Rectangle(double a, double b):h(a),w(b)virtual double area() constreturn h * w;class Trapezoid:public Shapeprivate:double h, w;p

35、ublic:Trapezoid(double a, double b):h(a), w(b)virtual double area() constreturn 0.5 * h * w;void printArea(const Shape & c)std:cout c.area() std:endl;int main()Circle c(2);printArea(c);Rectangle r(2, 4);printArea(r);Trapezoid t(3, 5);printArea(t);return 0;49. 定義一個人員類Cperson,包括數(shù)據(jù)成員:姓名、編號、性別和用于輸入輸出的成員

36、函數(shù)。在此基礎(chǔ)上派生出學(xué)生類CStudent(增加成績)和老師類Cteacher(增加教齡),并實現(xiàn)對學(xué)生和教師信息的輸入輸出。#include class Cpersonprotected:char name10;int id;int sex;public:virtual void getInfor() = 0;virtual void printInfor() = 0;class CStudent:public Cpersonprotected:int course;public:virtual void getInfor()using namespace std;cout Enter t

37、he students inforamtion endl;cout name ;cout id;cout sex;cout course;virtual void printInfor()using namespace std;cout id endl;cout name endl;cout sex endl;cout course endl;class Cteacher:public Cpersonprotected:int age;public:virtual void getInfor()using namespace std;cout Enter the teachers infora

38、mtion endl;cout name ;cout id;cout sex;cout age;virtual void printInfor()using namespace std;cout id endl;cout name endl;cout sex endl;cout age endl;int main()CStudent stu;stu.getInfor();stu.printInfor();Cteacher tea;tea.getInfor();tea.printInfor();return 0;50. 定義一個學(xué)生類Student做基類,再派生一個Graduate類,學(xué)生類有學(xué)

39、號、姓名、和分數(shù),研究生增加工資,它們有同名的函數(shù)display(),利用虛函數(shù),編程分別輸出學(xué)生和研究生的數(shù)據(jù),具體數(shù)據(jù)自擬。#include #include using namespace std;class Studentprotected:int id;string name;int score;public:Student(int ,string , int);virtual void display();Student:Student(int i, string n, int cr)id = i;name = n;score = cr;void Student:display()

40、/using namespace std;cout id : name endl;cout score endl;class Graduate:public Studentprotected:int salary;public:Graduate(int i, string n, int cr, int sa):Student(i, n, cr),salary(sa) void display();void Graduate:display()/using namespace std;cout id : name endl;cout score endl;cout salary endl;int

41、 main()Student stu(1, John, 99);stu.display();Graduate gra(2, JOHNLIU, 100, 5000);gra.display();return 0;2、 第二類題目(40道,每題9分,請自行設(shè)計輸出格式)蔣玉金1-5題1. 輸入20個學(xué)生成績(設(shè)為int型),統(tǒng)計各分數(shù)段的人數(shù),分數(shù)段為90及90以上、80到89分、70到79分、60到69分、60分以下。#includeint main()int a20=55,66,77,88,99,54,64,74,84,94,56,66,76,86,96,60,70,80,90,85;int

42、b,c,d,e,f;b=c=d=e=f=0;for(int i=0;i=90)b+;else if(ai=80)c+;else if(ai=70)d+;else if(ai=60)e+;else f+;coutb=bendl;coutc=cendl;coutd=dendl;coute=eendl;coutf=fendl;return 0;2. 把有序的兩個數(shù)組a和b 合并,要求合并后數(shù)組依然有序,數(shù)據(jù)自擬。#includeint main()int i=0;int j=0;int k=0;int c7;int a4=2,3,5,8;int b3=1,4,6;while(i4&j3)if(aibj) ck+=ai+;else ck+=bj+;while(i4)ck+=ai+;while(j3)ck+=bj+;for

溫馨提示

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

評論

0/150

提交評論