課后習題整理_第1頁
課后習題整理_第2頁
課后習題整理_第3頁
課后習題整理_第4頁
課后習題整理_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

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

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

4、lude <iostream>#include <string>using namespace std;class Student /學號、姓名、電話號碼、所屬院系,成員函數(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<

6、;<endl; ;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)設計一個學生類,包含學生呢個姓名、成績(char *name;double score),設計一個友元函數(shù),比較學生成績的高低,并求出下一組學生:Stu("zhang"),78,Stu("wang",80),Stu("li",65),Stu("che

7、n",50)中的最高分和最低分#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

8、;i+) if(si.score<si+1.score) tmp=si; si=si+1; si+1=tmp; cout<<"分數(shù)由高到低排列:" for(i=0;i<4;i+) cout<<<<" "<<si.score<<endl; cout<<"最高分:"<<<<" "<<s0.score<<endl; cout<<"最低分:&q

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

10、ck類增加載重噸數(shù).#include<iostream>#include<string>using namespace std;class Vehicleprivate: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<<&quo

12、t;,"<<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(); co

13、ut<<"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;V

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

15、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.

16、p)+1; 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

17、,i);strcat(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()

18、;s2.Show();(8-4)根據(jù)程序定義一個求3個數(shù)之間最大值的函數(shù)模板max。#include<iostream.h>#include<string.h>template<typename T>T max(T a,T b,T c)return (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',&#

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

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

21、ame<<" "<<s.deg<<" "<<"中"<<endl; else if(s.deg>=60) cout<<<<" "<<s.deg<<" "<<"及格"<<endl; else cout<<<<" "<<s.deg<<" &quo

22、t;<<"不及格"<<endl; ; void main() student st=student("王華",78),student("李明",92),student("張偉",62),student("孫強",88); cout<<"輸出結果:"<<endl; cout<<"姓名"<<" "<<"成績"<<"

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

24、;p.no; cout<<"姓名:" cin>>; cout<<"成績:" cin>>p.deg; cout<<"輸出結果"<<endl; cout<<"學生:"<<<<endl; cout<<"學號:"<<p.no<<endl; cout<<"成績?yōu)?"<<p.deg<<e

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

26、d display() cout<<"姓名:"<<name<<",性別:"<<sex<<",年齡:"<<age<<endl; ;class Student:public Person char sno10; int score;public: void input() Person:input(); cout<<"請輸入學號:" cin>>sno; cout<<"請輸入成績:"

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

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

29、lt;<name<<" 薪水:"<<money<<endl<<endl;class Lecturer:public Teacherprivate:char name20;int lessons;public:Lecturer()cout<<"請輸入姓名:"cin>>name;cout<<"請輸入課時:"cin>>lessons;int Salary()return (1000+lessons*50);void Print(int mo

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

溫馨提示

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

評論

0/150

提交評論