版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、第八章 #include <iostream> using namespace std; class Time public: /成員改為公用的 int hour; int minute; int sec; ; Time t; void set_time(void) /在 main 函數(shù)之前定義 cin>>t.hour; cin>>t.minute; cin>>t.sec; void show_time(void) /在 main 函數(shù)之前定義 cout<<t.hour<<":"<<t.mi
2、nute<<":"<<t.sec<<endl; int main() set_time(); show_time(); return 0; 8.2 題 #include <iostream> using namespace std; class Time public: void set_time(void) cin>>hour; cin>>minute; cin>>sec; void show_time(void) cout<<hour<<":"
3、<<minute<<":"<<sec<<endl; private: int hour; int minute; int sec; ; Time t; int main() t.set_time(); t.show_time(); return 0; 8.3 題 #include <iostream> using namespace std; class Time public: void set_time(void); void show_time(void); private: int hour; int mi
4、nute; int sec; ; void Time:set_time(void) cin>>hour; cin>>minute; cin>>sec; void Time:show_time(void) cout<<hour<<":"<<minute<<":"<<sec<<endl; Time t; int main() t.set_time(); t.show_time(); return 0; 8.4 題 /xt8-4.h(student.h)
5、 class Student public: void display( ); void set_value(); private: int num; char name20; char sex ; ; 8.4 題 /xt8-4-1.cpp(main.cpp) #include <iostream> using namespace std; #include "xt8-4.h" int main() Student stud; stud.set_value(); stud.display(); return 0; 8.4 題另一解 /xt8-4-2.cpp(即
6、student.cpp) #include "xt8-4.h" /在此文件中進(jìn)行函數(shù)的定義 #include <iostream> using namespace std; /不要漏寫此行 void Student:display( ) cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; void
7、 Student:set_value() cin>>num; cin>>name; cin>>sex; 8.5 題 /xt8-5.h(arraymax.h) class Array_max public: void set_value(); void max_value(); void show_value(); private: int array10; int max; ; 8.5 題 /xt8-5-1.cpp(file1.cpp) #include <iostream> #include "xt8-5.h" int ma
8、in() Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return 0; 8.5 題 /xt8-5-2.cpp(arraymax.cpp) #include <iostream> using namespace std; #include "xt8-5.h" void Array_max:set_value() int i; for (i=0;i<10;i+) cin>>arrayi; void Array_max:max_valu
9、e() int i; max=array0; for (i=1;i<10;i+) if(arrayi>max) max=arrayi; void Array_max:show_value() cout<<"max="<<max<<endl; 8.6 題 #include <iostream> using namespace std; class Box public: void get_value(); float volume(); void display(); public: float lengh; flo
10、at width; float height; ; void Box:get_value() cout<<"please input lengh, width,height:" cin>>lengh; cin>>width; cin>>height; float Box:volume() return(lengh*width*height); void Box:display() cout<<volume()<<endl; int main() Box box1,box2,box3; box1.get_
11、value(); cout<<"volmue of bax1 is " box1.display(); box2.get_value(); cout<<"volmue of bax2 is " box2.display(); box3.get_value(); cout<<"volmue of bax3 is " box3.display(); return 0; 8.6 題另一解 #include <iostream> using namespace std; class Box pu
12、blic: void get_value(); void volume(); void display(); public: float lengh; float width; float height; float vol; ; void Box:get_value() cout<<"please input lengh, width,height:" cin>>lengh; cin>>width; cin>>height; void Box:volume() vol=lengh*width*height; void Box
13、:display() cout<<vol<<endl; int main() Box box1,box2,box3; box1.get_value(); box1.volume(); cout<<"volmue of bax1 is " box1.display(); box2.get_value(); box2.volume(); cout<<"volmue of bax2 is " box2.display(); box3.get_value(); box3.volume(); cout<<
14、"volmue of bax3 is " box3.display(); return 0; 9.2 題 #include <iostream> using namespace std; class Date public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; ; Date:Date(int m,int d,int y):month(m),day(d),year(y) Date:Dat
15、e(int m,int d):month(m),day(d) year=2005; Date:Date(int m):month(m) day=1; year=2005; Date:Date() month=1; day=1; year=2005; void Date:display() cout<<month<<"/"<<day<<"/"<<year<<endl; int main() Date d1(10,13,2005); Date d2(12,30); Date d3(10)
16、; Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; 9.3 題 #include <iostream> using namespace std; class Date public: Date(int=1,int=1,int=2005); void display(); private: int month; int day; int year; ; Date:Date(int m,int d,int y):month(m),day(d),year(y) void Date:dis
17、play() cout<<month<<"/"<<day<<"/"<<year<<endl; int main() Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; 9.4 題 #include <iostream> using namespace std; class St
18、udent public: Student(int n,float s):num(n),score(s) void display(); private: int num; float score; ; void Student:display() cout<<num<<" "<<score<<endl; int main() Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,
19、95.5); Student *p=stud; for(int i=0;i<=2;p=p+2,i+) p->display(); return 0; 9.5 題 #include <iostream> using namespace std; class Student public: Student(int n,float s):num(n),score(s) int num; float score; ; void main() Student stud5= Student(101,78.5),Student(102,85.5),Student(103,98.5),
20、 Student(104,100.0),Student(105,95.5); void max(Student* ); Student *p=&stud0; max(p); void max(Student *arr) float max_score=arr0.score; int k=0; for(int i=1;i<5;i+) if(arri.score>max_score) max_score=arri.score;k=i; cout<<arrk.num<<" "<<max_score<<endl;
21、9.6 題 #include <iostream> using namespace std; class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display()cout<<num<<" "<<score<<endl; private: int num; float score; ; int main() Student stud(101,78.5);
22、 stud.display(); stud.change(101,80.5); stud.display(); return 0; 9.7 題 #include <iostream> using namespace std; class Student public: Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<<num<<" "<<score<<endl;
23、/ 可改為 :void display() const cout<<num<<" "<<score<<endl; private: int num; float score; ; int main() const Student stud(101,78.5); stud.display(); /stud.change(101,80.5); stud.display(); return 0; 9.7 題另一解 #include <iostream> using namespace std; class Student
24、 public: Student(int n,float s):num(n),score(s) void change(int n,float s) const num=n;score=s; void display() const cout<<num<<" "<<score<<endl; private: mutable int num; mutable float score; ; int main() const Student stud(101,78.5); stud.display(); stud.change(10
25、1,80.5); stud.display(); return 0; 9.7 題另一解 Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<<num<<" "<<score<<endl; private: int num; float score; ; int main() Student stud(101,78.5); Student *p=&stud; p->d
26、isplay(); p->change(101,80.5); p->display(); return 0; A Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<<num<<" "<<score<<endl; private: mutable int num; mutable float score; ; int main() Student stud(101,7
27、8.5); const Student *p=&stud; p->display(); p->change(101,80.5); p->display(); return 0; B Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() constcout<<num<<" "<<score<<endl; private: int num; float score; ;
28、 int main() Student stud(101,78.5); const Student *p=&stud; p->display(); stud.change(101,80.5); p->display(); return 0; Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<<num<<" "<<score<<endl; private: i
29、nt num; float score; ; int main() Student stud(101,78.5); Student * const p=&stud; p->display(); p->change(101,80.5); p->display(); return 0; 9.8 題 Student(int n,float s):num(n),score(s) void change(int n,float s) num=n;score=s; void display() cout<<num<<" "<<
30、;score<<endl; private: int num; float score; ; int main() Student stud(101,78.5); void fun(Student&); fun(stud); return 0; void fun(Student &stu) stu.display(); stu.change(101,80.5); stu.display(); 9.9 題 #include <iostream> using namespace std; class Product public: Product(int n
31、,int q,float p):num(n),quantity(q),price(p); void total(); static float average(); static void display(); private: int num; int quantity; float price; static float discount; static float sum; static int n; ; void Product:total() float rate=1.0; if(quantity>10) rate=0.98*rate; sum=sum+quantity*pri
32、ce*rate*(1-discount); n=n+quantity; void Product:display() cout<<sum<<endl; cout<<average()<<endl; float Product:average() return(sum/n); float Product:discount=0.05; float Product:sum=0; int Product:n=0; int main() Product Prod3= Product(101,5,23.5),Product(102,12,24.56),Pro
33、duct(103,100,21.5) ; for(int i=0;i<3;i+) Prodi.total(); Product:display(); return 0; 9.10 題 #include <iostream> using namespace std; class Date; class Time public: Time(int,int,int); friend void display(const Date &,const Time &); private: int hour; int minute; int sec; ; Time:Time(
34、int h,int m,int s) hour=h; minute=m; sec=s; class Date public: Date(int,int,int); friend void display(const Date &,const Time &); private: int month; int day; int year; ; Date:Date(int m,int d,int y) month=m; day=d; year=y; void display(const Date &d,const Time &t) cout<<d.mont
35、h<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; int main() Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; 9.11 題 #include <iostream> usi
36、ng namespace std; class Time; class Date public: Date(int,int,int); friend Time; private: int month; int day; int year; ; Date:Date(int m,int d,int y):month(m),day(d),year(y) class Time public: Time(int,int,int); void display(const Date &); private: int hour; int minute; int sec; ; Time:Time(int
37、 h,int m,int s):hour(h),minute(m),sec(s) void Time:display(const Date &d) cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl; int main() Time t1(10,1
38、3,56); Date d1(12,25,2004); t1.display(d1); return 0; 9.12 題 #include <iostream> using namespace std; template<class numtype> class Compare public: Compare(numtype a,numtype b); numtype max(); numtype min(); private: numtype x,y; ; template <class numtype> Compare<numtype>:Co
39、mpare(numtype a,numtype b) x=a;y=b; template <class numtype> numtype Compare<numtype>:max() return (x>y)?x:y; template <class numtype> numtype Compare<numtype>:min() return (x<y)?x:y; int main() Compare<int> cmp1(3,7); cout<<cmp1.max()<<" is the M
40、aximum of two integer numbers."<<endl; cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl; Compare<float> cmp2(45.78,93.6); cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl; cout<<
41、;cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl; Compare<char> cmp3('a','A'); cout<<cmp3.max()<<" characters."<<endl; is the Maximum of two cout<<cmp3.min()<<" is the Minimum of two charac
42、ters."<<endl; return 0; 10.1 題 #include <iostream> using namespace std; class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; double get_real(); double get_imag(); void display(); private: double real; double imag; ; double Complex:get_real() return
43、real; double Complex:get_imag() return imag; void Complex:display() cout<<"("<<real<<","<<imag<<"i)"<<endl; Complex operator + (Complex &c1,Complex &c2) return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.ge t_imag();
44、int main() Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c3=" c3.display(); return 0; 10.2 題 #include <iostream> using namespace std; class Complex public: Complex()real=0;imag=0; Complex(double r,double i)real=r;imag=i; Complex operator+(Complex &c2); Complex operator-(Co
45、mplex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display(); private: double real; double imag; ; Complex Complex:operator+(Complex &c2) Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; Complex Complex:operator-(Complex &c2) Complex c;
46、 c.real=real-c2.real; c.imag=imag-c2.imag; return c; Complex Complex:operator*(Complex &c2) Complex c; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; return c; Complex Complex:operator/(Complex &c2) Complex c; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.i mag*
47、c2.imag); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.i mag*c2.imag); return c; void Complex:display() cout<<"("<<real<<","<<imag<<"i)"<<endl; int main() Complex c1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1+c2="
48、; c3.display(); c3=c1-c2; cout<<"c1-c2=" c3.display(); c3=c1*c2; cout<<"c1*c2=" c3.display(); c3=c1/c2; cout<<"c1/c2=" c3.display(); return 0; 10.3 題 #include <iostream> /用 VC+時(shí)改為 #include <iostream.h> using namespace std; /用 VC+時(shí)為取消此行 clas
49、s Complex 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(); private: double real; double imag; ; Complex Complex:operator+(Complex &c) retur
50、n Complex(real+c.real,imag+c.imag); Complex Complex:operator+(int &i) return Complex(real+i,imag); void Complex:display() cout<<"("<<real<<","<<imag<<"i)"<<endl; Complex operator+(int &i,Complex &c) return Complex(i+c.real
51、,c.imag); int main() Complex c1(3,4),c2(5,-10),c3; int i=5; c3=c1+c2; cout<<"c1+c2=" c3.display(); c3=i+c1; cout<<"i+c1=" c3.display(); c3=c1+i; cout<<"c1+i=" c3.display(); return 0; 10.4 題 #include <iostream> using namespace std; class Matrix /定
52、義 Matrix 類 public: Matrix(); /默認(rèn)構(gòu)造函數(shù) friend Matrix operator+(Matrix &,Matrix &); /重載運(yùn)算符“+” void input(); /輸入數(shù)據(jù)函數(shù) void display(); / 輸出數(shù)據(jù)函數(shù) private: int mat23; ; Matrix:Matrix() /定義構(gòu)造函數(shù) for(int i=0;i<2;i+) for(int j=0;j<3;j+) matij=0; Matrix operator+(Matrix &a,Matrix &b) /定義重載運(yùn)算
53、符“+” 函數(shù) Matrix c; for(int i=0;i<2;i+) for(int j=0;j<3;j+) c.matij=a.matij+b.matij; return c; void Matrix:input() / 定義輸入數(shù)據(jù)函數(shù) cout<<"input value of matrix:"<<endl; for(int i=0;i<2;i+) for(int j=0;j<3;j+) cin>>matij; void Matrix:display() /定義輸出數(shù)據(jù)函數(shù) for (int i=0;i
54、<2;i+) for(int j=0;j<3;j+) cout<<matij<<" " cout<<endl; int main() Matrix a,b,c; a.input(); b.input(); cout<<endl<<"Matrix a:"<<endl; a.display(); cout<<endl<<"Matrix b:"<<endl; b.display(); c=a+b; /用重載運(yùn)算符“+”實(shí)現(xiàn)
55、兩個(gè)矩陣相加 cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl; c.display(); return 0; 10.5 題 #include <iostream.h> /using namespace std; class Matrix public: Matrix(); friend Matrix operator+(Matrix &,Matrix &); friend ostream& operator<<(ostream&
56、;,Matrix&); friend istream& operator>>(istream&,Matrix&); private: int mat23; ; Matrix:Matrix() for(int i=0;i<2;i+) for(int j=0;j<3;j+) matij=0; Matrix operator+(Matrix &a,Matrix &b) Matrix c; for(int i=0;i<2;i+) for(int j=0;j<3;j+) c.matij=a.matij+b.matij;
57、return c; istream& operator>>(istream &in,Matrix &m) cout<<"input value of matrix:"<<endl; for(int i=0;i<2;i+) for(int j=0;j<3;j+) in>>m.matij; return in; ostream& operator<<(ostream &out,Matrix &m) for (int i=0;i<2;i+) for(int
58、 j=0;j<3;j+) out<<m.matij<<" " out<<endl; return out; int main() Matrix a,b,c; cin>>a; cin>>b; cout<<endl<<"Matrix a:"<<endl<<a<<endl; cout<<endl<<"Matrix b:"<<endl<<b<<endl; c=a+b; cout<<endl<<"Matrix c = Matrix a + Ma
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年房地產(chǎn)行業(yè)崗位能力測試題投資顧問崗位
- 2026年應(yīng)屆生國際貿(mào)易實(shí)務(wù)基礎(chǔ)知識(shí)題
- 2026年管理科學(xué)基于ISO標(biāo)準(zhǔn)的內(nèi)審技術(shù)模擬試題
- 2026年交通規(guī)則與駕駛安全知識(shí)題庫
- 2026年機(jī)械制造行業(yè)認(rèn)證題庫與正確答案詳解
- 2026年廣西藍(lán)天航空職業(yè)學(xué)院單招綜合素質(zhì)考試參考題庫含詳細(xì)答案解析
- 2025貴州從江瑤浴產(chǎn)業(yè)發(fā)展有限公司招聘參考考試試題及答案解析
- 2026季華實(shí)驗(yàn)室管理部門招聘1人(廣東)考試重點(diǎn)試題及答案解析
- 2026年山西衛(wèi)生健康職業(yè)學(xué)院單招綜合素質(zhì)筆試備考題庫含詳細(xì)答案解析
- 2026年麗江師范高等??茖W(xué)校單招綜合素質(zhì)筆試參考題庫含詳細(xì)答案解析
- 2026廣東惠州市博羅縣城鄉(xiāng)管理和綜合執(zhí)法局招聘編外人員55人考試參考試題及答案解析
- 2026臺(tái)州三門金鱗招商服務(wù)有限公司公開選聘市場化工作人員5人備考考試題庫及答案解析
- 江西省南昌市2025-2026學(xué)年上學(xué)期期末九年級(jí)數(shù)學(xué)試卷(含答案)
- 信息化培訓(xùn)考核管理制度
- 體育培訓(xùn)教練員制度
- 縣醫(yī)院醫(yī)?;鸸芾碇贫?3篇)
- 建筑鋼結(jié)構(gòu)防火技術(shù)規(guī)范
- 護(hù)坡施工方案審查(3篇)
- 低空智能-從感知推理邁向群體具身
- 內(nèi)鏡進(jìn)修匯報(bào)
- 春節(jié)后復(fù)工“收心會(huì)”會(huì)議紀(jì)要
評(píng)論
0/150
提交評(píng)論