您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > C++ primer plus(第6版)中文版编程练习答案第12章
1、//cow.h#ifndefCOW_H_#defineCOW_H_#includeiostream#includestring#includestdio.husingnamespacestd;classCow{private:charname[20];char*hobby;doubleweight;public:Cow();Cow(constchar*nm,constchar*ho,doublewt);Cow(constCow&c);~Cow();Cow&operator=(constCow&c);voidShowCow()const;};#endif//cow.cpp#includecow.hCow::Cow(){name[0]='\0';hobby=newchar[1];hobby[0]='\0';weight=0;}Cow::Cow(constchar*nm,constchar*ho,doublewt){strcpy_s(name,20,nm);hobby=newchar[strlen(ho)+1];strcpy_s(hobby,strlen(ho)+1,ho);weight=wt;}Cow::Cow(constCow&c){strcpy_s(name,20,c.name);hobby=newchar[strlen(c.hobby)+1];strcpy_s(hobby,strlen(c.hobby)+1,c.hobby);weight=c.weight;}Cow::~Cow(){delete[]hobby;}Cow&Cow::operator=(constCow&c){if(this==&c)return*this;delete[]hobby;hobby=newchar[strlen(c.hobby)+1];strcpy_s(hobby,strlen(c.hobby)+1,c.hobby);strcpy_s(name,20,c.name);weight=c.weight;return*this;}voidCow::ShowCow()const{coutCowname:nameendl;coutCowhobby:hobbyendl;coutCowweight:weightendl;}//main.cpp#includecow.hintmain(){Cowco1;Cowco2(cow1,sport,123);Cowco3(co2);co1=co2;co1.ShowCow();co2.ShowCow();co3.ShowCow();system(pause);return0;}2、//String.h#ifndefSTRING_H_#defineSTRING_H_#includeiostream#includestdio.h#includecstring#includestring.husingnamespacestd;classString{public:String(constchar*s);String();String(constString&);~String();intlength()const{returnlen;}String&operator=(constString&st);String&operator=(constchar*);char&operator[](inti);constchar&operator[](inti)const;voidstringlow();voidstringup();inthas(constcharch);Stringoperator+(constchar*s);friendStringoperator+(constchar*s,constString&st);friendbooloperator(constString&st1,constString&st2);friendbooloperator(constString&st1,constString&st2);friendbooloperator==(constString&st1,constString&st2);friendStringoperator+(constString&st1,constString&st2);friendostream&operator(ostream&os,constString&st);friendistream&operator(istream&is,String&st);staticintHowMany();private:char*str;intlen;staticintnum_strings;staticconstintCINLIM=80;};#endif//String.cpp#includeString.hintString::num_strings=0;intString::HowMany(){returnnum_strings;}String::String(constchar*s){len=strlen(s);str=newchar[len+1];strcpy_s(str,len+1,s);num_strings++;}String::String(){len=4;str=newchar[1];str[0]='\0';num_strings++;}String::String(constString&st){num_strings++;len=st.len;str=newchar[len+1];strcpy_s(str,len+1,st.str);}String::~String(){--num_strings;delete[]str;}String&String::operator=(constString&st){if(this==&st)return*this;delete[]str;len=st.len;str=newchar[len+1];strcpy_s(str,len+1,st.str);return*this;}String&String::operator=(constchar*s){delete[]str;len=strlen(s);str=newchar[len+1];strcpy_s(str,len+1,s);return*this;}char&String::operator[](inti){returnstr[i];}constchar&String::operator[](inti)const{returnstr[i];}voidString::stringlow(){for(inti=0;ilen;i++){if(isupper(str[i]))str[i]=tolower(str[i]);}}voidString::stringup(){for(inti=0;ilen;i++){if(islower(str[i]))str[i]=toupper(str[i]);}}intString::has(constcharch){intcounts=0;for(inti=0;ilen;i++){if(str[i]==ch)counts++;}returncounts;}booloperator(constString&st1,constString&st2){return(strcmp(st1.str,st2.str)0);}booloperator(constString&st1,constString&st2){returnst2st1;}booloperator==(constString&st1,constString&st2){return(strcmp(st1.str,st2.str)==0);}StringString::operator+(constchar*s){intlens=strlen(s)+len;char*ps=newchar[lens+1];strcpy_s(ps,lens+1,str);strcat_s(ps,lens+1,s);returnString(ps);}Stringoperator+(constchar*s,constString&st){intlens=strlen(s)+st.len;char*ps=newchar[lens+1];strcpy_s(ps,lens+1,s);strcat_s(ps,lens+1,st.str);returnString(ps);}Stringoperator+(constString&st1,constString&st2){intlens=st1.len+st2.len;char*ps=newchar[lens+1];strcpy_s(ps,lens+1,st1.str);strcat_s(ps,lens+1,st2.str);returnString(ps);}ostream&operator(ostream&os,constString&st){osst.str;returnos;}istream&operator(istream&is,String&st){chartemp[String::CINLIM];is.get(temp,String::CINLIM);if(is)st=temp;while(is&&is.get()!='\n')continue;returnis;}//main.cpp#includeString.hintmain(){Strings1(andIamaC++student.);Strings2=Pleaseenteryourname:;Strings3;couts2;cins3;s2=Mynameis+s3;couts2.\n;s2=s2+s1;s2.stringup();coutThestring\ns2\ncontainss2.has('A')'A'charactersinit.\n;s1=red;Stringrgb[3]={String(s1),String(green),String(blue)};coutEnterthenameofaprimarycolorformixinglight:;Stringans;boolsuccess=false;while(cinans){ans.stringlow();for(inti=0;i3;i++){if(ans==rgb[i]){coutThat'sright!\n;success=true;break;}}if(success)break;elsecoutTryagain!\n;}coutBye\n;system(pause);return0;}3、//stock.h#ifndefSTOCK10_H_#defineSTOCK10_H_#includestring#includeiostream#includestdio.husingnamespacestd;classStock{private:char*company;longshares;doubleshare_val;doubletotal_val;voidset_tot(){total_val=shares*share_val;}public:Stock();Stock(constchar*co,longn=0,doublepr=0.0);~Stock();voidbuy(longnum,doubleprice);voidsell(longnum,doubleprice);voidupdate(doubleprice);friendostream&operator(ostream&os,constStock&s);constS
本文标题:C++ primer plus(第6版)中文版编程练习答案第12章
链接地址:https://www.777doc.com/doc-6184910 .html