C++_編程題庫_第1頁
C++_編程題庫_第2頁
C++_編程題庫_第3頁
C++_編程題庫_第4頁
已閱讀5頁,還剩117頁未讀, 繼續(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

2、.length 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;v

3、oid Box: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

4、, 20 。分別求他們的體積。編一個基于對象的程序,在類中用帶參數(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 = m

5、ybox2.volume() endl;return 0;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;p

6、ublic:Box();Box(int, int, int);int volume();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)len

7、gth = l;wight = w;height = h;int Box:volume()return 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 m

8、ain()Comparecmp1(3,4);coutcmp1.max() is the Maximum of two 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)

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

10、eendl;private:int num;int score;int main()Studentstud5=Student(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 個學生的數(shù)據(jù)(學號、成績),設(shè)立一個函數(shù)max ,用指向?qū)ο蟮闹羔樧骱瘮?shù)參數(shù),在 max 函數(shù)中找出 5 個學生中成績最高者, 并輸出其學號。初值自擬。#includeusing namespace std;cl

11、ass Studentpublic:Student(int n,int s):num(n),score(s)int num;int score;int main()感謝下載載精品Studentstud5=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

12、(max_scorearri.score)max_score=arri.score;coutmax_score arri.numendl;感謝下載載精品38.定義一個復數(shù)類Complex ,重載運算符“ + ”,使之能用于復數(shù)的加法運算。將運算符函數(shù)重載為非成員、非友元的普通函數(shù)。編寫程序,求兩個復數(shù)之和。初值自擬。#include class Complexprivate:double real;double image;public:Complex();Complex(double, double);double get_real();double get_image();Complex

13、operator + (Complex &, Complex &);int main()Complex s1(3, 4);Complex s2(5, -10);感謝下載載精品Complex 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

14、;Complex:Complex()real = 0;image = 0;感謝下載載精品double Complex:get_real()return real;double Complex:get_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 定義一個復數(shù)類Complex ,重載運算符“”,“”,使之能用于復數(shù)的加,減運算,運算符重載函數(shù)

15、作為Complex類的成員函數(shù)。 編程序,分別求出兩個復數(shù)之和,差。初值自擬。#include 感謝下載載精品class Complexprivate:double real;double image;public:Complex();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.dis

16、play();感謝下載載精品return 0;Complex:Complex()real = 0;image = 0;Complex:Complex(double r, double i)real = r;image = i;Complex 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

17、.real;c.image = image - s1.image;return c;void Complex:display()using namespace std;cout real image endl;40. 定義一個復數(shù)類 Complex ,重載運算符“”,使之能用于復數(shù)的加法運算。參加運算的兩個運算量可以都是類對象,也可以其中有一個是整數(shù),順序任意。例如: c1+c2 ,i+c1 ,c1+i 均合法(設(shè) i 為整數(shù), c1,c2 為復數(shù))。編程序,分別求兩個復數(shù)之和、整數(shù)和復數(shù)之和。初值自擬。#include class Complex感謝下載載精品private:double r

18、eal;double imag;public:Complex()real=0;imag=0;Complex(double r,double i)real=r;imag=i;Complex operator+(Complex &c2);Complex operator+(int &i);friend Complex operator+(int&,Complex &);void display();Complex Complex:operator+(Complex &c)return Complex(real+c.real,imag+c.imag);Complex Complex:operator

19、+(int &i)return Complex(real+i,imag);感謝下載載精品void Complex:display()using namespace std;cout(real,imagi)endl;Complex operator+(int &i,Complex &c)return 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.disp

20、lay();c3=c1+i;感謝下載載精品coutc1+i=;c3.display();return 0;41. 有兩個矩陣 a 和 b ,均為 2 行 3 列。求兩個矩陣之和。重載運算符“+ ”,使之能用于矩陣相加。如c=a+b 。初值自擬。#include using namespace std;class Matrix/ 定義 Matrix類public:Matrix();/ 默認構(gòu)造函數(shù)friend Matrix operator+(Matrix &,Matrix &);/ 重載運算符“ + ”void input();/ 輸入數(shù)據(jù)函數(shù)void display();/ 輸出數(shù)據(jù)函數(shù)pr

21、ivate: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;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+)f

22、or(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();coutendlMatrix a:endl;a.display();coutendlMatrix b:endl;b.display();c=a+b;/ 用重載運算符“ + ”實現(xiàn)兩個矩陣相加coutendlMatrix c = Matrix a + Matrix b :endl;c.di

23、splay();return 0;42. 將運算符“”重載為適用于復數(shù)加法,重載函數(shù)不作為成員函數(shù),而放在類外,作為 Complex 類的友元函數(shù)。初值自擬。#include 感謝下載載精品class Compareprivate:int real;int imag;public:Compare();Compare(int, int);friend Compare operator+(Compare & c1, Compare & c2);void display();int main()Compare c1(3, 4), c2(2, 5), c3;c3 = c1 + c2;c3.displa

24、y();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;Compare operator+(Compare & c1, Compare & c2)Compare c(c1.real + c2.real, c1.imag + c2.imag);return c;感謝下載載精品43. 定義一個字符串類 String ,用來存放

25、不定長的字符串,重載運算符“”,用于兩個字符串的等于比較運算。初值自擬。44. 定義一個字符串類 String ,用來存放不定長的字符串,重載運算符 ,用于兩個字符串的大于的比較運算。初值自擬。#include #include using namespace std;class Stringpublic:String() p=NULL;String(char *str);friend bool operator(String &string1,String &string2);friend bool operator(String &string1,String &string2);frie

26、nd 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(strcmp(string1.p,string2.p)0)return true;elsereturn false;bool operator(String &string1,String &string2)if(strcmp(str

27、ing1.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=(string1,string2)=1)string1.display();cout=;string2.display();感謝下載載精品coutendl;int main()String string1(Hello),string2(Book),stri

28、ng3(Computer),string4(Hello);compare(string1,string2);compare(string2,string3);compare(string1,string4);return 0;46. 定義一個描述學生基本情況的類,數(shù)據(jù)成員包括姓名、學號、C+ 成績、英語和數(shù)學成績,成員函數(shù)包括輸出數(shù)據(jù),求出總成績和平均成績。數(shù)據(jù)自擬。#include class Studentprivate:char * name;感謝下載載精品int id;int cpp_source;int eng_source;int math_source;public:Studen

29、t(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, int cs, int es, int ms)name = n;感謝下載載精品id = i;cpp_source = cs;eng_source = es;math_source = ms;void Student:sum_source()s

30、td: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: avg std:endl;47. 先建立一個 Point (點)類,包含數(shù)據(jù)成員 x, y (坐標點)。以它為基類,派生出一個 Circle (圓)類,增加數(shù)據(jù)成員 r (半徑),再以 Circle 類為直接基類,派生出一

31、個 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 b)y = b;int getX()return x;int getY()return y;class Circle:public Pointprotected:int r;感謝

32、下載載精品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 h):Circle(x, y, r)this-h = h;void setH(int a)h = a;int getH()return h;friend std:

33、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);return input;std:ostream & operator(std:ostream & os, Cylinder & cc)os cc.

34、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;class Circle:public Shapeprivate:double r;public:Circle(double a):r(a)virtual double area() const感謝下載載精品return 3.14 * r * r;class Rect

溫馨提示

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

評論

0/150

提交評論