C++ primer plus中文编程练习答案第12章.docx
《C++ primer plus中文编程练习答案第12章.docx》由会员分享,可在线阅读,更多相关《C++ primer plus中文编程练习答案第12章.docx(14页珍藏版)》请在三一办公上搜索。
1、C+ primer plus中文编程练习答案第12章1、 /cow.h #ifndef COW_H_ #define COW_H_ #include #include #include using namespace std; class Cow private: char name20; char *hobby; double weight; public: Cow; Cow(const char *nm, const char *ho, double wt); Cow(const Cow &c); Cow; Cow &operator=(const Cow &c); voidShowCow
2、const; ; #endif /cow.cpp #includecow.h Cow:Cow name0 = 0; hobby = new char1; hobby0 = 0; weight = 0; Cow:Cow(const char *nm, const char *ho, double wt) strcpy_s(name, 20, nm); hobby = new charstrlen(ho)+1; strcpy_s(hobby, strlen(ho) + 1, ho); weight = wt; Cow:Cow(const Cow &c) strcpy_s(name, 20, c.n
3、ame); hobby = new charstrlen(c.hobby) + 1; strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby); weight = c.weight; Cow:Cow deletehobby; Cow &Cow:operator=(const Cow &c) if (this = &c) return *this; deletehobby; hobby = new charstrlen(c.hobby) + 1; strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby); strcpy_s(na
4、me, 20, c.name); weight = c.weight; return *this; void Cow:ShowCowconst cout Cow name: name endl; cout Cow hobby: hobby endl; cout Cow weight: weight endl; /main.cpp #includecow.h int main Cow co1; Cow co2(cow1, sport, 123); Cow co3(co2); co1 = co2; co1.ShowCow; co2.ShowCow; co3.ShowCow; system(paus
5、e); return 0; 2、 /String.h #ifndef STRING_H_ #define STRING_H_ #include #include #include #include using namespace std; class String public: String(const char *s); String; String(const String &); String; int lengthconst return len; String &operator=(const String &st); String &operator=(const char *)
6、; char&operator(inti); const char &operator(inti)const; voidstringlow; voidstringup; int has(const char ch); String operator+(const char *s); friend String operator+(const char *s, const String &st); friendbool operator(const String &st1, const String &st2); friendbool operator=(const String &st1, c
7、onst String &st2); friend String operator+(const String &st1, const String &st2); friendostream&operator(istream&is, String &st); staticintHowMany; private: char *str; intlen; staticintnum_strings; staticconstint CINLIM = 80; ; #endif /String.cpp #include String.h int String:num_strings = 0; int Str
8、ing:HowMany returnnum_strings; String:String(const char *s) len = strlen(s); str = new charlen + 1; strcpy_s(str, len + 1, s); num_strings+; String:String len = 4; str = new char1; str0 = 0; num_strings+; String:String(const String &st) num_strings+; len = st.len; str = new charlen + 1; strcpy_s(str
9、, len + 1, st.str); String:String -num_strings; deletestr; String &String:operator=(const String &st) if (this = &st) return *this; deletestr; len = st.len; str = new charlen + 1; strcpy_s(str, len + 1, st.str); return *this; String &String:operator=(const char *s) deletestr; len = strlen(s); str =
10、new charlen + 1; strcpy_s(str, len + 1, s); return *this; char&String:operator(inti) returnstri; const char &String:operator(inti)const returnstri; void String:stringlow for (inti = 0; ilen; i+) if (isupper(stri) stri = tolower(stri); void String:stringup for (inti = 0; ilen; i+) if (islower(stri) s
11、tri = toupper(stri); int String:has(const char ch) int counts = 0; for (inti = 0; ilen; i+) if (stri = ch) counts+; return counts; bool operator(const String &st1, const String &st2) return (strcmp(st1.str, st2.str) (const String &st1, const String &st2) return st2 st1; bool operator=(const String &
12、st1, const String &st2) return (strcmp(st1.str, st2.str) = 0); String String:operator+(const char *s) int lens = strlen(s) + len; char *ps = new charlens + 1; strcpy_s(ps, lens + 1, str); strcat_s(ps, lens + 1, s); return String(ps); String operator+(const char *s, const String &st) int lens = strle
13、n(s) + st.len; char *ps = new charlens + 1; strcpy_s(ps, lens + 1, s); strcat_s(ps, lens + 1, st.str); return String(ps); String operator+(const String &st1,const String &st2) int lens = st1.len + st2.len; char *ps = new charlens + 1; strcpy_s(ps, lens + 1, st1.str); strcat_s(ps, lens + 1, st2.str);
14、 return String(ps); ostream&operator(ostream&os, const String &st) os(istream&is, String &st) char tempString:CINLIM; is.get(temp, String:CINLIM); if (is) st = temp; while (is &is.get != n) continue; return is; /main.cpp #includeString.h int main String s1( and I am a C+ student.); String s2 = Pleas
15、e enter your name: ; String s3; cout s3; s2 = My name is + s3; cout s2 .n; s2 = s2 + s1; s2.stringup; cout The stringn s2 ncontains s2.has(A) A characters in it.n; s1 = red; String rgb3 = String(s1), String(green), String(blue) ; coutans) ans.stringlow; for (inti = 0; i 3; i+) if (ans = rgbi) cout T
16、hats right!n; success = true; break; if (success) break; else cout Try again!n; cout Byen; system(pause); return 0; 3、 /stock.h #ifndef STOCK10_H_ #define STOCK10_H_ #include #include #include using namespace std; class Stock private: char *company; long shares; doubleshare_val; doubletotal_val; voi
17、dset_tot total_val = shares*share_val; public: Stock; Stock(const char *co, long n = 0, double pr = 0.0); Stock; void buy(long num, double price); void sell(long num, double price); void update(double price); friendostream&operator(ostream&os, const Stock &s); const Stock &topval(const Stock &s)cons
18、t; ; #endif /stock.cpp #include stock.h Stock:Stock cout Default constructor calledn; company = new char1; company0 = 0; shares = 0; share_val = 0.0; total_val = 0.0; Stock:Stock(const char *co, long n, double pr) cout Constructor using co calledn; company = new charstrlen(co) + 1; strcpy_s(company,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ primer plus中文编程练习答案第12章 plus 中文 编程 练习 答案 12
链接地址:https://www.31ppt.com/p-3153805.html