c++經(jīng)典課后習(xí)題整理(1)_第1頁
c++經(jīng)典課后習(xí)題整理(1)_第2頁
c++經(jīng)典課后習(xí)題整理(1)_第3頁
c++經(jīng)典課后習(xí)題整理(1)_第4頁
c++經(jīng)典課后習(xí)題整理(1)_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、(1) 設(shè)計(jì)一個(gè) Car 類,他的數(shù)據(jù)成員要能描述一輛汽車的品牌、型號(hào)、出廠年份和價(jià)格, 成員函數(shù)包括提供合適的途徑來訪問數(shù)據(jù)成員,在main ()函數(shù)中定義類的對(duì)象并調(diào)用相應(yīng)的成員函數(shù)#include<iostream> #include<string> using namespace std;class Carprivate:string Pingpai;string Xinghao;i nt Year;i nt Price;public:void set()cout<<"shu ru xiang guan xin xi"<&l

2、t;endl; cout<<"pingpai:"cin>>Pingpai; cout<<"xinghao:"cin>>Xinghao; cout<<"chuchangnianfen:"cin>>Year; cout<<"jiage:"cin>>Price;void show() cout<<"pingpai:"<<Pingpai<<endl; cout<<

3、;"xinghao:"<<Xinghao<<endl; cout<<"chuchangnianfen:"<<Year<<endl; cout<<"jiage:"<<Price<<endl;int main()Car car;car.set ();car.show ();return 0;(2) 設(shè)計(jì)一個(gè)學(xué)生類 Student ,擁有的數(shù)據(jù)成員是學(xué)號(hào)、姓名、電話號(hào)碼、所屬院系,成員函數(shù)包括訪問和修改這些屬性,在main ()中定義對(duì)象,并輸出

4、相關(guān)信息#include <iostream>#include <string>using namespace std;class Student / 學(xué)號(hào)、姓名、電話號(hào)碼、所屬院系,成員函數(shù)包括訪問和修改這些屬 性private:string xuehao;string name;string tel;string department;public:Student(string x,string n,string t,string d)xuehao=x;name=n;tel=t;department=d;void change(string x,string n,s

5、tring t,string d)/修改屬性xuehao=x;name=n;tel=t;department=d;void display()cout<<"xuehao="<<xuehao<<endl; cout<<"name="<<name<<endl;cout<<"tel="<<tel<<endl; cout<<"department="<<department<<en

6、dl;int main()string a,b,c,d;cin>>a;cin>>b;cin>>c;cin>>d;Student s(a,b,c,d);s.display();return 0;(3) 設(shè)計(jì)一個(gè)學(xué)生類,包含學(xué)生呢個(gè)姓名、成績( char *name;double score ) , 設(shè)計(jì) 個(gè)友元函數(shù),比較學(xué)生成績的高低,并求出下一組學(xué)生Stu("zha ng"),78,Stu("wa ng",80),Stu("li",65),Stu("che n",50

7、)中的最高分和最低分#include<iostream>#include<string>using namespace std;class studentprivate:char *name;double score;public:student()student(char *na,double sc)name=na;score=sc;friend void order(student s);void order(student s)student tmp;int i,j;for(j=0;j<3;j+)for(i=0;i<3-j;i+)if(si.score

8、<si+1.score)tmp=si;si=si+1;si+1=tmp;cout<<" 分?jǐn)?shù)由高到低排列: "for(i=0;i<4;i+)cout<<<<" "<<si.score<<endl;cout<<" 最高分: "<<<<" "<<s0.score<<endl;cout<<" 最低分: "<<s3.nam

9、e<<" "<<s3.score<<endl;int main()student s4=student("zhang",78),student("wang",80),student("li",92),student("chen",50 );order(s);(4) 編寫程序,定義機(jī)動(dòng)車類 Vehicle ,包括的數(shù)據(jù)成員有出廠日期和售價(jià)并定義成員 函數(shù)可以設(shè)置這些數(shù)據(jù)成員,再定義 print ()然后定義 car 類,增加乘客數(shù)量, truck 類 增加載重噸

10、數(shù) #include<iostream> #include<string> using namespace std; class Vehicle private: int year,month,day; int price; public: Vehicle() cout<<"please input date: "cout<<"year"<<","<<"month"<<","<<"day&q

11、uot;<<","<<endl;cin>>year; cin>>month;cin>>day; cout<<"please input price:"cin>>price;cout<<"Vehicle"<<endl;virtual void print1() cout<<"date:"<<year<<","<<month<<&qu

12、ot;,"<<day<<","<<endl; cout<<"price:"<<price<<endl;class Car:public Vehiclepublic:int amount;Car() cout<<"please input amount of passager: "cin>>amount; cout<<"car"<<endl;void print2()print1(); c

13、out<<"amount:"<<amount<<endl;class Truck:public Vehiclepublic:int ton;Truck() cout<<"please input ton:"cin>>ton; cout<<"truck"<<endl;void print3()print1(); cout<<"ton:"<<ton<<endl;int main()Vehicle V1

14、;V1.print1();Car C1;C1.print2();Truck T1;T1.print3();return 0;(7-4) 完整定義字符串類 string ,使用動(dòng)態(tài)分配內(nèi)存機(jī)制實(shí)現(xiàn)字符串存儲(chǔ),定義構(gòu)造函 數(shù),析構(gòu)函數(shù),重載運(yùn)算符“ =”,"+", “+=”實(shí)現(xiàn)兩個(gè)字符串的賦值,連接等功能。#include<iostream> #include<string> using namespace std;class Stringpublic:String();String(char *t);String(String &t);Stri

15、ng();String operator=(String t);String operator+( String t);String operator +=(String t);void Show()cout<<p<<endl;private:char *p;String:String()p = new char1;*p = '0'String:String(char *t)p = new charstrlen(t)+1; strcpy(p,t);String:String(String &t)p = new charstrlen(t.p)+1;

16、strcpy(p,t.p);String String:operator +(String t)char *pt;pt = new charstrlen(p)+strlen(t.p)+1; strcpy(pt,p);strcat(pt,t.p);String temp(pt);deletept;return temp;String String:operator +=(String t)char *i;i =new charstrlen(p)+1;strcpy(i,p);delete p;p=new charstrlen(t.p)+strlen(p)+1;strcpy(p,i);strcat(

17、p,t.p);return *this;String:String()deletep;String String:operator =(String t)if(this = &t)return *this;deletep;p = new charstrlen(t.p)+1;strcpy(p,t.p);return *this;void main()String s1("hello! ");String s2("when.");String s3;s3=s2+s1;s3+=s2;s2=s1;s3.Show();s2.Show();max。(8-4)

18、 根據(jù)程序定義一個(gè)求 3 個(gè)數(shù)之間最大值的函數(shù)模板#include<iostream.h>#include<string.h>template<typename T>T max(T a,T b,T c)r eturn (a>b?a:b)>c?(a>b?a:b):c;int main() cout<<max(11,29,22)<<endl;cout<<max(3.14f,28.3f,6.7f)<<endl;cout<<max('c','b','

19、a')<<endl;r eturn 0;( 5)有一個(gè)學(xué)生類 student, 包括學(xué)生姓名、成績,設(shè)計(jì)一個(gè)友元函數(shù),輸出成績對(duì)應(yīng) 的等級(jí):大于等于 90:優(yōu); 8090:良; 7079:中; 60!69 :及格;小于 60:不及格。#include<iostream.h>#include<string.h>class studentchar name10;int deg;public:student(char na,int d)strcpy(name,na);deg=d;friend void trans(student &s)if(s.d

20、eg>=90)cout<<<<" "<<s.deg<<""<<"優(yōu)"<<e ndl;else if(s.deg>=80)cout<<<<" "<<s.deg<<""<<"良"<<e ndl;else if(s.deg>=70)cout<<<<" &

21、quot;<<s.deg<<""<<"中"<<e ndl;else if(s.deg>=60)cout<<<<" "<<s.deg<<""<<"及格"<<endl;elsecout<<<<" "<<s.deg<<""<<"不及格 "

22、;<<endl;void main()student st=student(" 王 華 ",78),student(" 李 明 ",92),student(" 張 偉 ",62),student(" 孫強(qiáng) ",88);cout<<" 輸出結(jié)果: "<<endl;cout<<"姓名"<<" "<<" 成績"<<""<<&qu

23、ot; 等級(jí)"<<endl;for(int i=0;i<4;i+) trans(sti);(6) 編寫一個(gè)程序,設(shè)計(jì)一個(gè) Point 類,包括學(xué)號(hào)、姓名和成績等私有數(shù)據(jù)成員,不含 任何成員函數(shù),只將 main() 設(shè)置為該類的友元函數(shù)。#include<iostream.h>class pointint no;char name10;int deg;public:friend void main();void main()point p;cout<<"輸入學(xué)號(hào) :"cin>>p.no;cout<<&

24、quot;姓名: "cin>>;cout<<"成績: "cin>>p.deg;cout<<"輸出結(jié)果 "<<endl;cout<<"學(xué)生 :"<<<<endl;cout<<"學(xué)號(hào) :"<<p.no<<endl;cout<<"成績?yōu)?:"<<p.deg<<endl;(7) 創(chuàng)建一個(gè)學(xué)生類 , 包括學(xué)

25、號(hào)和成績 , 編程輸入和顯示學(xué)生信息 , 建立一個(gè)人類 ,包括姓 名, 性別, 年齡, 并作為學(xué)生的基類 .#include<iostream.h>class Personchar name10;char sex;int age;public:void input()cout<<" 請(qǐng)輸入姓名 :"cin>>name;cout<<" 請(qǐng)輸入性別 :"cin>>sex;cout<<" 請(qǐng)輸入年齡 :"cin>>age;void display()cout

26、<<" 姓名 :"<<name<<", 性別 :"<<sex<<", 年齡 :"<<age<<endl;class Student:public Personchar sno10; int score;public: void input() Person:input();cout<<" 請(qǐng)輸入學(xué)號(hào) :" cin>>sno;cout<<" 請(qǐng)輸入成績 :" cin>>

27、;score;void display()Person:display();cout<<" 學(xué)號(hào) ;"<<sno<<", 成績 :"<<score<<endl;void main()Student s1;s1.input();s1.display();(8) 按要求編程: 某學(xué)校教授和講師的月工資計(jì)算辦法規(guī)定如下: 教授每月固定工資為 4000 元。講師每月工資與講課學(xué)時(shí)數(shù)有關(guān),計(jì)算方法是每學(xué)時(shí) 50 元,另加補(bǔ)助 1000 元。 編程顯示某個(gè)教員的月工資數(shù)目。#include <iost

28、ream.h> class Teacherpublic:virtual int Salary()=0;virtual void Print(int)=0;class Professor:public Teacher private: char name20;i nt lessons; public: Professor()cout<<" 請(qǐng)輸入姓名: "cin>>name; / 字符串中不能有空格i nt Salary()return 4000;void Print(int money)cout<<" 職稱:教授 姓名:

29、"<<name<<" 薪水: ;class Lecturer:public Teacher private: char name20;i nt lessons; public: Lecturer()cout<<" 請(qǐng)輸入姓名: " cin>>name;cout<<" 請(qǐng)輸入課時(shí): " cin>>lessons;i nt Salary()return (1000+lessons*50);void Print(int money)cout<<"職

30、稱:講師 姓名:"<<name<<"薪水:;void main()"<<money<<endl<<endl;"<<money<<endl<<endl;i nt money=0;Professor t;money = t.Salary();t .Print(money);Lecturer l;money = l.Salary();l .Print(money);(9) 1.給定一個(gè)int型數(shù)n,編程實(shí)現(xiàn)按不同進(jìn)制輸出,包括十進(jìn)制數(shù)、八進(jìn)制數(shù)、十 六進(jìn)制數(shù)。要求使用純虛函數(shù)Print ()o十進(jìn)制數(shù)(dec) 十六進(jìn)制數(shù)(hex)八進(jìn)制數(shù)( oct )#include<iostream.h>#include<math.h>int main()char ch1

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論