C面向?qū)ο蟪绦蛟O(shè)計(jì)答案18章譚浩強(qiáng)_第1頁
C面向?qū)ο蟪绦蛟O(shè)計(jì)答案18章譚浩強(qiáng)_第2頁
C面向?qū)ο蟪绦蛟O(shè)計(jì)答案18章譚浩強(qiáng)_第3頁
C面向?qū)ο蟪绦蛟O(shè)計(jì)答案18章譚浩強(qiáng)_第4頁
C面向?qū)ο蟪绦蛟O(shè)計(jì)答案18章譚浩強(qiáng)_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

第一章5:#include<iostream>usingnamespacestd;intmain(){cout<<"This"<<"is";cout<<"a"<<"C++";cout<<"program."<<endl;return0;}6:#include<iostream>usingnamespacestd;intmain(){inta,b,c;a=10;b=23;c=a+b;cout<<"a+b=";cout<<c;cout<<endl;return0;}7:#include<iostream>usingnamespacestd;intmain(){inta,b,c;intf(intx,inty,intz);cin>>a>>b>>c;c=f(a,b,c);cout<<c<<endl;return0;}intf(intx,inty,intz){intm;if(x<y)m=x;elsem=y;if(z<m)m=z;return(m);}8:#include<iostream>usingnamespacestd;intmain(){inta,b,c;cin>>a>>b;c=a+b;cout<<"a+b="<<a+b<<endl;return0;}9:#include<iostream>usingnamespacestd;intmain(){intadd(intx,inty);inta,b,c;cin>>a>>b;c=add(a,b);cout<<"a+b="<<c<<endl;return0;}intadd(intx,inty){intc;c=x+y;return(c);}10:#include<iostream>usingnamespacestd;intmain(){voidsort(intx,inty,intz);intx,y,z;cin>>x>>y>>z;sort(x,y,z);return0;}voidsort(intx,inty,intz){inttemp;if(x>y){temp=x;x=y;y=temp;}//{}內(nèi)3個(gè)語句的作用是將x和y的值互換)if(z<x)cout<<z<<','<<x<<','<<y<<endl;elseif(z<y)cout<<x<<','<<z<<','<<y<<endl;elsecout<<x<<','<<y<<','<<z<<endl;}11:#include<iostream>usingnamespacestd;intmain(){intmax(inta,intb,intc=0);inta,b,c;cin>>a>>b>>c;cout<<"max(a,b,c)="<<max(a,b,c)<<endl;cout<<"max(a,b)="<<max(a,b)<<endl;return0;}intmax(inta,intb,intc){if(b>a)a=b;if(c>a)a=c;returna;}12:#include<iostream>usingnamespacestd;intmain(){voidchange(int,int);inta,b;cin>>a>>b;if(a<b)change(a,b);cout<<"max="<<a<<"min="<<b<<endl;return0;}voidchange(int,int){intr1,r2,temp;temp=r1;r1=r2;r2=temp;}13:#include<iostream>usingnamespacestd;intmain(){voidsort(int&,int&,int&);inta,b,c,a1,b1,c1;cout<<"Pleaseenter3integers:";cin>>a>>b>>c;a1=a;b1=b;c1=c;sort(a1,b1,c1);cout<<a<<""<<b<<""<<c<<"insortedorderis";cout<<a1<<""<<b1<<""<<c1<<endl;return0;}voidsort(int&i,int&j,int&k){voidchange(int&,int&);if(i>j)change(i,j);if(i>k)change(i,k);if(j>k)change(j,k);}voidchange(int&x,int&y){inttemp;temp=x;x=y;y=temp;}14:#include<iostream>#include<string>usingnamespacestd;intmain(){strings1="week",s2="end";cout<<"s1="<<s1<<endl;cout<<"s2="<<s2<<endl;s1=s1+s2;cout<<"Thenewstringis:"<<s1<<endl;return0;}15:#include<iostream>#include<string>usingnamespacestd;intmain(){stringstr;inti,n;chartemp;cout<<"pleaseinputastring:";cin>>str;n=str.size();for(i=0;i<n/2;i++) {temp=str[i];str[i]=str[n-i-1];str[n-i-1]=temp;}cout<<str<<endl;return0;}16:#include<iostream>#include<string>usingnamespacestd;intmain(){inti;stringstr[5]={"BASIC","C","FORTRAN","C++","PASCAL"};voidsort(string[]);sort(str);cout<<"thesortedstrings:"<<endl;for(i=0;i<5;i++)cout<<str[i]<<"";cout<<endl;return0;}voidsort(strings[]){inti,j;stringt;for(j=0;j<5;j++)for(i=0;i<5-j;i++)if(s[i]>s[i+1]) {t=s[i];s[i]=s[i+1];s[i+1]=t;}}17:#include<iostream>#include<string>usingnamespacestd;intmain(){longc[5]={10100,-123567,1198783,-165654,3456};inta[5]={1,9,0,23,-45};floatb[5]={2.4,7.6,5.5,6.6,-2.3};voidsort(int[]);voidsort(float[]);voidsort(long[]);sort(a);sort(b);sort(c);return0;}voidsort(inta[]){inti,j,t;for(j=0;j<5;j++)for(i=0;i<5-j;i++)if(a[i]>a[i+1]) {t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"thesortednumbers:"<<endl;for(i=0;i<5;i++) cout<<a[i]<<"";cout<<endl<<endl;}voidsort(longa[]){inti,j;longt;for(j=0;j<5;j++)for(i=0;i<5-j;i++)if(a[i]>a[i+1]) {t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"thesortednumbers:"<<endl;for(i=0;i<5;i++) cout<<a[i]<<"";cout<<endl<<endl;}voidsort(floata[]){inti,j;floatt;for(j=0;j<5;j++)for(i=0;i<5-j;i++)if(a[i]>a[i+1]) {t=a[i];a[i]=a[i+1];a[i+1]=t;}cout<<"thesortednumbers:"<<endl;for(i=0;i<5;i++) cout<<a[i]<<"";cout<<endl<<endl;}18:#include<iostream>#include<string>usingnamespacestd;template<typenameT>voidsort(Ta[]){inti,j,min;Tt;for(i=0;i<5;i++) {min=i;for(j=i+1;j<5;j++) if(a[min]>a[j])min=j; t=a[i];a[i]=a[min];a[min]=t; }cout<<"thesortednumbers:"<<endl;for(i=0;i<5;i++) cout<<a[i]<<"";cout<<endl<<endl;}intmain(){inta[5]={1,9,0,23,-45};floatb[5]={2.4,7.6,5.5,6.6,-2.3};longc[5]={10100,-123567,1198783,-165654,3456};sort(a);sort(b);sort(c);return0;}第二章1#include<iostream>usingnamespacestd;classTime{public: voidset_time(); voidshow_time();private://成員改為公用的inthour;intminute;intsec;};voidTime::set_time()//在main函數(shù)之前定義{cin>>hour;cin>>minute;cin>>sec;}voidTime::show_time()//在main函數(shù)之前定義{cout<<hour<<":"<<minute<<":"<<sec<<endl;}intmain(){Timet1;t1.set_time();t1.show_time();return0;}2:#include<iostream>usingnamespacestd;classTime{public:voidset_time(void){cin>>hour;cin>>minute;cin>>sec;}voidshow_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}private:inthour;intminute;intsec;};Timet;intmain(){t.set_time();t.show_time();return0;}3:#include<iostream>usingnamespacestd;classTime{public:voidset_time(void);voidshow_time(void);private:inthour;intminute;intsec;};voidTime::set_time(void){cin>>hour;cin>>minute;cin>>sec;}voidTime::show_time(void){cout<<hour<<":"<<minute<<":"<<sec<<endl;}Timet;intmain(){t.set_time();t.show_time();return0;}4://xt2-4-1.cpp(main.cpp)#include<iostream>usingnamespacestd;#include"xt2-4.h"intmain(){Studentstud;stud.set_value();stud.display();return0;}//xt2-4-2.cpp(即student.cpp)#include"xt2-4.h"http://在此文件中進(jìn)行函數(shù)的定義#include<iostream>usingnamespacestd;//不要漏寫此行voidStudent::display(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}voidStudent::set_value(){cin>>num;cin>>name;cin>>sex;}5://xt2-5-1.cpp(file1.cpp)#include<iostream>#include"xt2-5.h"intmain(){Array_maxarrmax;arrmax.set_value();arrmax.max_value();arrmax.show_value();return0;}//xt2-5-2.cpp(arraymax.cpp)#include<iostream>usingnamespacestd;#include"xt2-5.h"voidArray_max::set_value(){inti;for(i=0;i<10;i++)cin>>array[i];}voidArray_max::max_value(){inti;max=array[0];for(i=1;i<10;i++)if(array[i]>max)max=array[i];}voidArray_max::show_value(){cout<<"max="<<max<<endl;}6:解法一#include<iostream>usingnamespacestd;classBox{public:voidget_value();floatvolume();voiddisplay();public:floatlengh;floatwidth;floatheight;};voidBox::get_value(){cout<<"pleaseinputlengh,width,height:";cin>>lengh;cin>>width;cin>>height;}floatBox::volume(){return(lengh*width*height);}voidBox::display(){cout<<volume()<<endl;}intmain(){Boxbox1,box2,box3;box1.get_value();cout<<"volmueofbax1is";box1.display();box2.get_value();cout<<"volmueofbax2is";box2.display();box3.get_value();cout<<"volmueofbax3is";box3.display();return0;}解法二:#include<iostream>usingnamespacestd;classBox{public:voidget_value();voidvolume();voiddisplay();public:floatlengh;floatwidth;floatheight;floatvol;};voidBox::get_value(){cout<<"pleaseinputlengh,width,height:";cin>>lengh;cin>>width;cin>>height;}voidBox::volume(){vol=lengh*width*height;}voidBox::display(){cout<<vol<<endl;}intmain(){Boxbox1,box2,box3;box1.get_value();box1.volume();cout<<"volmueofbax1is";box1.display();box2.get_value();box2.volume();cout<<"volmueofbax2is";box2.display();box3.get_value();box3.volume();cout<<"volmueofbax3is";box3.display();return0;}第三章2:#include<iostream>usingnamespacestd;classDate{public:Date(int,int,int);Date(int,int);Date(int);Date();voiddisplay();private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}Date::Date(intm,intd):month(m),day(d){year=2005;}Date::Date(intm):month(m){day=1;year=2005;}Date::Date(){month=1;day=1;year=2005;}voidDate::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}intmain(){Dated1(10,13,2005);Dated2(12,30);Dated3(10);Dated4;d1.display();d2.display();d3.display();d4.display();return0;}3:#include<iostream>usingnamespacestd;classDate{public:Date(int=1,int=1,int=2005);voiddisplay();private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}voidDate::display(){cout<<month<<"/"<<day<<"/"<<year<<endl;}intmain(){Dated1(10,13,2005);Dated2(12,30);Dated3(10);Dated4;d1.display();d2.display();d3.display();d4.display();return0;}4:#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voiddisplay();private:intnum;floatscore;};voidStudent::display(){cout<<num<<""<<score<<endl;}intmain(){Studentstud[5]={Student(101,78.5),Student(102,85.5),Student(103,98.5),Student(104,100.0),Student(105,95.5)};Student*p=stud;for(inti=0;i<=2;p=p+2,i++)p->display();return0;}5:#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}intnum;floatscore;};voidmain(){Studentstud[5]={Student(101,78.5),Student(102,85.5),Student(103,98.5),Student(104,100.0),Student(105,95.5)};voidmax(Student*);Student*p=&stud[0];max(p);}voidmax(Student*arr){floatmax_score=arr[0].score;intk=0;for(inti=1;i<5;i++)if(arr[i].score>max_score){max_score=arr[i].score;k=i;}cout<<arr[k].num<<""<<max_score<<endl;}6:#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats){num=n;score=s;}voiddisplay(){cout<<num<<""<<score<<endl;}private:intnum;floatscore;};intmain(){Studentstud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return0;}7:解法一#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats){num=n;score=s;}voiddisplay(){cout<<num<<""<<score<<endl;}//可改為:voiddisplay()const{cout<<num<<""<<score<<endl;}private:intnum;floatscore;};intmain(){constStudentstud(101,78.5);stud.display();//stud.change(101,80.5);stud.display();return0;}解法二:#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats)const{num=n;score=s;}voiddisplay()const{cout<<num<<""<<score<<endl;}private:mutableintnum;mutablefloatscore;};intmain(){constStudentstud(101,78.5);stud.display();stud.change(101,80.5);stud.display();return0;}解法三:#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats){num=n;score=s;}voiddisplay(){cout<<num<<""<<score<<endl;}private:intnum;floatscore;};intmain(){Studentstud(101,78.5);Student*p=&stud;p->display();p->change(101,80.5);p->display();return0;}8:#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}voidchange(intn,floats){num=n;score=s;}voiddisplay(){cout<<num<<""<<score<<endl;}private:intnum;floatscore;};intmain(){Studentstud(101,78.5);voidfun(Student&);fun(stud);return0;}voidfun(Student&stu){stu.display();stu.change(101,80.5);stu.display();}9:#include<iostream>usingnamespacestd;classProduct{public:Product(intn,intq,floatp):num(n),quantity(q),price(p){};voidtotal();staticfloataverage();staticvoiddisplay();private:intnum;intquantity;floatprice;staticfloatdiscount;staticfloatsum;staticintn;};voidProduct::total(){floatrate=1.0;if(quantity>10)rate=0.98*rate;sum=sum+quantity*price*rate*(1-discount);n=n+quantity;}voidProduct::display(){cout<<sum<<endl;cout<<average()<<endl;}floatProduct::average(){return(sum/n);}floatProduct::discount=0.05;floatProduct::sum=0;intProduct::n=0;intmain(){ProductProd[3]={Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)};for(inti=0;i<3;i++)Prod[i].total();Product::display();return0;}10:#include<iostream>usingnamespacestd;classDate;classTime{public:Time(int,int,int);friendvoiddisplay(constDate&,constTime&);private:inthour;intminute;intsec;};Time::Time(inth,intm,ints){hour=h;minute=m;sec=s;}classDate{public:Date(int,int,int);friendvoiddisplay(constDate&,constTime&);private:intmonth;intday;intyear;};Date::Date(intm,intd,inty){month=m;day=d;year=y;}voiddisplay(constDate&d,constTime&t){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}intmain(){Timet1(10,13,56);Dated1(12,25,2004);display(d1,t1);return0;}11:#include<iostream>usingnamespacestd;classTime;classDate{public:Date(int,int,int);friendTime;private:intmonth;intday;intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}classTime{public:Time(int,int,int);voiddisplay(constDate&);private:inthour;intminute;intsec;};Time::Time(inth,intm,ints):hour(h),minute(m),sec(s){}voidTime::display(constDate&d){cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;}intmain(){Timet1(10,13,56);Dated1(12,25,2004);t1.display(d1);return0;}12:#include<iostream>usingnamespacestd;template<classnumtype>classCompare{public:Compare(numtypea,numtypeb);numtypemax();numtypemin();private:numtypex,y;};template<classnumtype>Compare<numtype>::Compare(numtypea,numtypeb){x=a;y=b;}template<classnumtype>numtypeCompare<numtype>::max(){return(x>y)?x:y;}template<classnumtype>numtypeCompare<numtype>::min(){return(x<y)?x:y;}intmain(){Compare<int>cmp1(3,7);cout<<cmp1.max()<<"istheMaximumoftwointegernumbers."<<endl;cout<<cmp1.min()<<"istheMinimumoftwointegernumbers."<<endl<<endl;Compare<float>cmp2(45.78,93.6);cout<<cmp2.max()<<"istheMaximumoftwofloatnumbers."<<endl;cout<<cmp2.min()<<"istheMinimumoftwofloatnumbers."<<endl<<endl;Compare<char>cmp3('a','A');cout<<cmp3.max()<<"istheMaximumoftwocharacters."<<endl;cout<<cmp3.min()<<"istheMinimumoftwocharacters."<<endl;return0;}第四章1:#include<iostream>usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}doubleget_real();doubleget_imag();voiddisplay();private:doublereal;doubleimag;};doubleComplex::get_real(){returnreal;}doubleComplex::get_imag(){returnimag;}voidComplex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complexoperator+(Complex&c1,Complex&c2){returnComplex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());}intmain(){Complexc1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c3=";c3.display();return0;}2:#include<iostream>usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator+(Complex&c2);Complexoperator-(Complex&c2);Complexoperator*(Complex&c2);Complexoperator/(Complex&c2);voiddisplay();private:doublereal;doubleimag;};ComplexComplex::operator+(Complex&c2){Complexc;c.real=real+c2.real;c.imag=imag+c2.imag;returnc;}ComplexComplex::operator-(Complex&c2){Complexc;c.real=real-c2.real;c.imag=imag-c2.imag;returnc;}ComplexComplex::operator*(Complex&c2){Complexc;c.real=real*c2.real-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;returnc;}ComplexComplex::operator/(Complex&c2){Complexc;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);returnc;}voidComplex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}intmain(){Complexc1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c1+c2=";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();return0;}3:#include<iostream>//用VC++時(shí)改為∶#include<iostream.h>usingnamespacestd;//用VC++時(shí)為取消此行classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator+(Complex&c2);Complexoperator+(int&i);friendComplexoperator+(int&,Complex&);voiddisplay();private:doublereal;doubleimag;};ComplexComplex::operator+(Complex&c){returnComplex(real+c.real,imag+c.imag);}ComplexComplex::operator+(int&i){returnComplex(real+i,imag);}voidComplex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complexoperator+(int&i,Complex&c){returnComplex(i+c.real,c.imag);}intmain(){Complexc1(3,4),c2(5,-10),c3;inti=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();return0;}4:#include<iostream>usingnamespacestd;classMatrix//定義Matrix類{public:Matrix();//默認(rèn)構(gòu)造函數(shù)friendMatrixoperator+(Matrix&,Matrix&);//重載運(yùn)算符“+”voidinput();//輸入數(shù)據(jù)函數(shù)voiddisplay();//輸出數(shù)據(jù)函數(shù)private:intmat[2][3];};Matrix::Matrix()//定義構(gòu)造函數(shù){for(inti=0;i<2;i++)for(intj=0;j<3;j++)mat[i][j]=0;}Matrixoperator+(Matrix&a,Matrix&b)//定義重載運(yùn)算符“+”函數(shù){Matrixc;for(inti=0;i<2;i++)for(intj=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}returnc;}voidMatrix::input()//定義輸入數(shù)據(jù)函數(shù){cout<<"inputvalueofmatrix:"<<endl;for(inti=0;i<2;i++)for(intj=0;j<3;j++)cin>>mat[i][j];}voidMatrix::display()//定義輸出數(shù)據(jù)函數(shù){for(inti=0;i<2;i++){for(intj=0;j<3;j++){cout<<mat[i][j]<<"";}cout<<endl;}}intmain(){Matrixa,b,c;a.input();b.input();cout<<endl<<"Matrixa:"<<endl;a.display();cout<<endl<<"Matrixb:"<<endl;b.display();c=a+b;//用重載運(yùn)算符“+”實(shí)現(xiàn)兩個(gè)矩陣相加cout<<endl<<"Matrixc=Matrixa+Matrixb:"<<endl;c.display();return0;}5:#include<iostream.h>//usingnamespacestd;classMatrix{public:Matrix();friendMatrixoperator+(Matrix&,Matrix&);friendostream&operator<<(ostream&,Matrix&);friendistream&operator>>(istream&,Matrix&);private:intmat[2][3];};Matrix::Matrix(){for(inti=0;i<2;i++)for(intj=0;j<3;j++)mat[i][j]=0;}Matrixoperator+(Matrix&a,Matrix&b){Matrixc;for(inti=0;i<2;i++)for(intj=0;j<3;j++){c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}returnc;}istream&operator>>(istream&in,Matrix&m){cout<<"inputvalueofmatrix:"<<endl;for(inti=0;i<2;i++)for(intj=0;j<3;j++)in>>m.mat[i][j];returnin;}ostream&operator<<(ostream&out,Matrix&m){for(inti=0;i<2;i++){for(intj=0;j<3;j++){out<<m.mat[i][j]<<"";}out<<endl;}returnout;}intmain(){Matrixa,b,c;cin>>a;cin>>b;cout<<endl<<"Matrixa:"<<endl<<a<<endl;cout<<endl<<"Matrixb:"<<endl<<b<<endl;c=a+b;cout<<endl<<"Matrixc=Matrixa+Matrixb:"<<endl<<c<<endl;return0;}6:#include<iostream>usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler){real=r;imag=0;}Complex(doubler,doublei){real=r;imag=i;}operatordouble(){returnreal;}voiddisplay();private:doublereal;doubleimag;};voidComplex::display(){cout<<"("<<real<<","<<imag<<")"<<endl;}intmain(){Complexc1(3,4),c2;doubled1;d1=2.5+c1;cout<<"d1="<<d1<<endl;c2=Complex(d1);cout<<"c2=";c2.display();return0;}7:#include<iostream>usingnamespacestd;classStudent{public:Student(int,char[],char,float);intget_num(){returnnum;}char*get_name(){returnname;}charget_sex(){returnsex;}voiddisplay(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n";}private:intnum;charname[20];charsex;floatscore;};Student::Student(intn,charnam[],chars,floatso){num=n;strcpy(name,nam);sex=s;score=so;}classTeacher{public:Teacher(){}Teacher(Student&);Teacher(intn,charnam[],charsex,floatpay);voiddisplay();private:intnum;charname[20];charsex;floatpay;};Teacher::Teacher(intn,charnam[],chars,floatp){num=n;strcpy(name,nam);sex=s;pay=p;}Teacher::Teacher(Student&stud){num=stud.get_num();strcpy(name,stud.get_name());sex=stud.get_sex();pay=1500;}voidTeacher::display(){cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}intmain(){Teacherteacher1(10001,"Li",'f',1234.5),teacher2;Studentstudent1(20010,"Wang",'m',89.5);cout<<"student1:"<<endl;student1.display();teacher2=Teacher(student1);cout<<"teacher2:"<<endl;teacher2.display();return0;}第五章1:#include<iostream>usingnamespacestd;classStudent{public:voidget_value(){cin>>num>>name>>sex;}voiddisplay(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}private:intnum;charname[10];charsex;};classStudent1:publicStudent{public:voidget_value_1(){get_value();cin>>age>>addr;}voiddisplay_1(){cout<<"age:"<<age<<endl;//引用派生類的私有成員,正確。cout<<"address:"<<addr<<endl;}//引用派生類的私有成員,正確。private:intage;charaddr[30];};intmain(){Student1stud1;stud1.get_value_1();stud1.display();stud1.display_1();return0;}2:#include<iostream>usingnamespacestd;classStudent{public:voidget_value(){cin>>num>>name>>sex;}voiddisplay(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}private:intnum;charname[10];charsex;};classStudent1:privateStudent{public:voidget_value_1(){get_value();cin>>age>>addr;}voiddisplay_1(){display();cout<<"age:"<<age<<endl;//引用派生類的私有成員,正確。cout<<"address:"<<addr<<endl;}//引用派生類的私有成員,正確。private:intage;charaddr[30];};intmain(){Student1stud1;stud1.get_value_1();stud1.display_1();return0;}3:#include<iostream>usingnamespacestd;classStudent//聲明基類{public://基類公用成員voidget_value();voiddisplay();protected://基類保護(hù)成員intnum;charname[10];charsex;};voidStudent::get_value(){cin>>num>>name>>sex;}voidStudent::display(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}classStudent1:protectedStudent//聲明一個(gè)保護(hù)派生類{public:voidget_value_1();voiddisplay1();private:intage;charaddr[30];};voidStudent1::get_value_1(){get_value();cin>>age>>addr;}voidStudent1::display1(){cout<<"num:"<<num<<endl;//引用基類的保護(hù)成員cout<<"name:"<<name<<endl;//引用基類的保護(hù)成員cout<<"sex:"<<sex<<endl;//引用基類的保護(hù)成員cout<<"age:"<<age<<endl;//引用派生類的私有成員cout<<"address:"<<addr<<endl;//引用派生類的私有成員}intmain(){Student1stud1;//stud1是派生類student1類的對象stud1.get_value_1();//調(diào)用派生類對象stud1的公用成員函數(shù)stud1.display1();//調(diào)用派生類對象stud1的公用成員函數(shù)return0;}4:解法一#include<iostream>usingnamespacestd;classStudent//聲明基類{public://基類公用成員voidget_value();voiddisplay();protected://基類保護(hù)成員intnum;charname[10];charsex;};voidStudent::get_value(){cin>>num>>name>>sex;}voidStudent::display(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}classStudent1:publicStudent//聲明一個(gè)公用派生類{public:voidget_value_1();voiddisplay1();private:intage;charaddr[30];};voidStudent1::get_value_1(){get_value();cin>>age>>addr;}voidStudent1::display1(){cout<<"num:"<<num<<endl;//引用基類的保護(hù)成員,合法cout<<"name:"<<name<<endl;//引用基類的保護(hù)成員,合法cout<<"sex:"<<sex<<endl;//引用基類的保護(hù)成員,合法cout<<"age:"<<age<<endl;//引用派生類的私有成員,合法cout<<"address:"<<addr<<endl;//引用派生類的私有成員,合法}intmain(){Student1stud1;//stud1是派生類student1類的對象stud1.get_value_1();//調(diào)用派生類對象stud1的公用成員函數(shù)get_value_1stud1.display1();//調(diào)用派生類對象stud1的公用成員函數(shù)display1return0;}解法二#include<iostream>usingnamespacestd;classStudent//聲明基類{public://基類公用成員voidget_value();voiddisplay();protected://基類保護(hù)成員intnum;charname[10];charsex;};voidStudent::get_value(){cin>>num>>name>>sex;}voidStudent::display(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}classStudent1:protectedStudent//聲明一個(gè)公用派生類{public:voidget_value_1();voiddisplay1();private:intage;charaddr[30];};voidStudent1::get_value_1(){cin>>age>>addr;}voidStudent1::display1(){cout<<"age:"<<age<<endl;cout<<"address:"<<addr<<endl;}intmain(){Student1stud1;//stud1是派生類student1類的對象stud1.get_value();stud1.get_value_1();stud1.display();stud1.display1();//合法。display1是派生類中的公用成員函數(shù)return0;}5:classA//A為基類{public:voidf1();inti;protected:voidf2();intj;private:intk;};classB:publicA//B為A的公用派生類{public:voidf3();protected:intm;private:intn;};classC:publicB//C為B的公用派生類{public:voidf4();private:intp;};intmain(){Aa1;//a1是基類A的對象Bb1;//b1是派生類B的對象Cc1;//c1是派生類C的對象return0;}6:#include<iostream>usingnamespacestd;classA{public:voidf1();protected:voidf2();private:inti;};classB:publicA{public:voidf3();intk;private:intm;};classC:protectedB{public:voidf4();protected:intn;private:intp;};classD:privateC{public:voidf5();protected:intq;private:intr;};intmain(){Aa1;Bb1;Cc1;Dd1;return0;}7:#include<iostream>usingnamespacestd;classA{public:A(){a=0;b=0;}A(inti){a=i;b=0;}A(inti,intj){a=i;b=j;}voiddisplay(){cout<<"a="<<a<<"b="<<b;}private:inta;intb;};classB:publicA{public:B(){c=0;}B(inti):A(i){c=0;}B(inti,intj):A(i,j){c=0;}B(inti,intj,intk):A(i,j){c=k;}voiddisplay1(){display();cout<<"c="<<c<<endl;}private:intc;};intmain(){Bb1;Bb2(1);Bb3(1,3);Bb4(1,3,5);b1.display1();b2.display1();b3.display1();b4.display1(); return0;}8:#include<iostream>usingnamespacestd;classA{public:A(){cout<<"constructingA"<<endl;}~A(){c

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論