第十三章 编程练习
2016-11-26 06:16
218 查看
编程练习1
编程练习2 ,这里只需修改类,主函数跟上题一样
编程练习3,这道题添加一个抽象基类感觉完全然并乱啊,不得其解。。。
编程练习4
//Classic.h #ifndef CLASSIC_H_ #define CLASSIC_H_ //base class #include <cstring> class Cd { private: char performers[50]; char lable[20]; int selections; double playtime; public: Cd(char * s1, char * s2, int n, double x); Cd(const Cd & d); Cd(); virtual void Report() const; Cd & operator=(const Cd & d); }; class Classic:public Cd { private: char mainselection[50]; public: Classic(){} Classic(char * s, char * s1, char * s2, int n, double x):Cd(s1, s2, n, x) {strcpy(mainselection, s); } Classic(char * s, const Cd & c):Cd(c) {strcpy(mainselection, s); } Classic(const Classic &c); virtual void Report() const; Classic & operator=(const Classic & c); }; #endif
//Classic.cpp #include "Classic.h" #include <cstring> #include <iostream> Cd::Cd(char * s1, char * s2, int n, double x) { strcpy(performers, s1); strcpy(lable, s2); selections = n; playtime = x; } Cd::Cd(const Cd & d) { strcpy(performers, d.performers); strcpy(lable, d.lable); selections = d.selections; playtime = d.playtime; } Cd::Cd() { strcpy(performers, "80s"); strcpy(lable, "Rock"); selections = 0; playtime = 0; } void Cd::Report() const { using std::cout; using std::endl; cout << "Performers of this CD is: " << performers << endl; cout << "Lable of this CD is: " << lable << endl; cout << "This CD has " << selections << " selections, and playtime is " << playtime << " minutes." << endl; } Cd & Cd::operator=(const Cd & d) { if (this == &d) return *this; strcpy(performers, d.performers); strcpy(lable, d.lable); selections = d.selections; playtime = d.playtime; return *this; } Classic::Classic(const Classic &c) { Cd::Cd(c); strcpy(mainselection, c.mainselection); } void Classic::Report() const { using std::cout; using std::endl; Cd::Report(); cout << "Main selection of this CD is: " << mainselection << endl; } Classic & Classic::operator=(const Classic & c) { if (this == &c) return *this; Cd::operator=(c); strcpy(mainselection, c.mainselection); return *this; }
//main.cpp #include "Classic.h" #include <iostream> using std::cout; using std::endl; void Bravo(const Cd &disk); int main() { Cd c1("Beatles", "Capitol", 14, 35.5); Classic c2 = Classic("Piano Sonata in Bflat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17); Cd *pcd = &c1; std::cout << "Using object directly:\n"; c1.Report(); c2.Report(); std::cout << "Using type cd *pointer to object:\n"; pcd->Report(); pcd = &c2; pcd->Report(); std::cout << "Testing assignment: \n"; Classic copy; copy = c2; copy.Report(); system("pause"); return 0; } void Bravo(const Cd &disk) { disk.Report(); }
编程练习2 ,这里只需修改类,主函数跟上题一样
//Classic.h #ifndef CLASSIC_H_ #define CLASSIC_H_ //base class #include <cstring> class Cd { private: char * performers; char * lable; int selections; double playtime; public: Cd(char * s1, char * s2, int n, double x); Cd(const Cd & d); Cd(); ~Cd(); virtual void Report() const; Cd & operator=(const Cd & d); }; class Classic:public Cd { private: char * mainselection; public: Classic(); Classic(char * s, char * s1, char * s2, int n, double x); Classic(char * s, const Cd & c); Classic(const Classic &c); ~Classic(); virtual void Report() const; Classic & operator=(const Classic & c); }; #endif
//classic.cpp #include "Classic.h" #include <cstring> #include <iostream> Cd::Cd(char * s1, char * s2, int n, double x) { int len1 = strlen(s1); int len2 = strlen(s2); performers = new char[len1+1]; lable = new char[len2+1]; strcpy(performers, s1); strcpy(lable, s2); selections = n; playtime = x; } Cd::Cd(const Cd & d) { int len1 = strlen(d.performers); int len2 = strlen(d.lable); performers = new char[len1+1]; lable = new char[len2+1]; strcpy(performers, d.performers); strcpy(lable, d.lable); selections = d.selections; playtime = d.playtime; } Cd::Cd() { int len1 = strlen("80s"); int len2 = strlen("Rock"); performers = new char[len1+1]; lable = new char[len2+1]; strcpy(performers, "80s"); strcpy(lable, "Rock"); selections = 0; playtime = 0; } Cd::~Cd() { delete [] lable; delete [] performers; } void Cd::Report() const { using std::cout; using std::endl; cout << "Performers of this CD is: " << performers << endl; cout << "Lable of this CD is: " << lable << endl; cout << "This CD has " << selections << " selections, and playtime is " << playtime << " minutes." << endl; } Cd & Cd::operator=(const Cd & d) { if (this == &d) return *this; delete [] performers; delete [] lable; int len1 = strlen(d.performers); int len2 = strlen(d.lable); performers = new char[len1+1]; lable = new char[len2+1]; strcpy(performers, d.performers); strcpy(lable, d.lable); selections = d.selections; playtime = d.playtime; return *this; } Classic::Classic(const Classic &c) { Cd::Cd(c); int len = strlen(c.mainselection); mainselection = new char[len+1]; strcpy(mainselection, c.mainselection); } Classic::Classic() { Cd::Cd(); int len = strlen("aaa"); mainselection = new char[len+1]; strcpy(mainselection, "aaa"); } Classic::Classic(char * s, char * s1, char * s2, int n, double x) { Cd::Cd(s1, s2, n, x); int len = strlen(s); mainselection = new char[len+1]; strcpy(mainselection, s); } Classic::Classic(char * s, const Cd & c) { Cd::Cd(c); int len = strlen(s); mainselection = new char[len+1]; strcpy(mainselection, s); } void Classic::Report() const { using std::cout; using std::endl; Cd::Report(); cout << "Main selection of this CD is: " << mainselection << endl; } Classic & Classic::operator=(const Classic & c) { if (this == &c) return *this; Cd::operator=(c); delete [] mainselection; int len = strlen(c.mainselection); mainselection = new char[len+1]; strcpy(mainselection, c.mainselection); return *this; } Classic::~Classic() { delete [] mainselection; }
编程练习3,这道题添加一个抽象基类感觉完全然并乱啊,不得其解。。。
//dma.h #ifndef DMA_H_ #define DMA_H_ #include <iostream> class DMA { private: char *label; int rating; public: DMA() { label = NULL; rating = 0; } DMA(const char *l, const int r); virtual ~DMA() { delete[] label; label = NULL; } char *GetLabel() const { return label; } int GetRating() const { return rating; } DMA &operator=(const DMA &rs); virtual void View() const = 0; }; class baseDMA : public DMA { public: baseDMA() : DMA() {} baseDMA(const char *l, const int r) : DMA(l, r) {} baseDMA &operator=(const baseDMA &rs); virtual void View() const; }; class lacksDMA : public DMA { private: char color[40]; public: lacksDMA() : DMA() { std::strcpy(color, "NULL"); } lacksDMA(const char *l, const int r, const char *c) : DMA(l, r) { std::strcpy(color, c); } lacksDMA &operator=(const lacksDMA &rs); virtual void View() const; }; class hasDMA : public DMA { private: char *style; public: hasDMA() : DMA() { style = NULL; } hasDMA(const char *l, const int r, const char *s); ~hasDMA() { delete[] style; style = NULL; } hasDMA &operator=(const hasDMA &res); virtual void View() const; }; #endif
//dma.cpp #include "DMA.h" DMA::DMA(const char *l, const int r) { label = new char[std::strlen(l) + 1]; std::strcpy(label, l); rating = r; } DMA &DMA::operator=(const DMA &rs) { if (this == &rs) return *this; delete[] label; label = new char[std::strlen(rs.label) + 1]; std::strcpy(label, rs.label); rating = rs.rating; return *this; } baseDMA &baseDMA::operator=(const baseDMA &rs) { if (this == &rs) return *this; DMA::operator=(rs); return *this; } void baseDMA::View() const { std::cout << "label: " << GetLabel() << std::endl; std::cout << "rating: " << GetRating() << std::endl; } lacksDMA &lacksDMA::operator=(const lacksDMA &rs) { if (this == &rs) return *this; DMA::operator=(rs); std::strcpy(color, rs.color); return *this; } void lacksDMA::View() const { std::cout << "label: " << GetLabel() << std::endl; std::cout << "rating: " << GetRating() << std::endl; std::cout << "color: " << color << std::endl; } hasDMA::hasDMA(const char *l, const int r, const char *s) : DMA(l, r) { style = new char[std::strlen(s) + 1]; std::strcpy(style, s); } hasDMA &hasDMA::operator=(const hasDMA &rs) { if (this == &rs) return *this; DMA::operator=(rs); delete[]style; style = new char[std::strlen(rs.style) + 1]; std::strcpy(style, rs.style); return *this; } void hasDMA::View() const { std::cout << "label" << GetLabel() << std::endl; std::cout << "rating: " << GetRating() << std::endl; std::cout << "style: " << style << std::endl; }
//main.cpp #include "DMA.h" const int LEN = 2; int main() { DMA *p[LEN]; for (int i = 0; i < LEN; ++i) { int flag; std::cout << "Please select object,1:lacksDMA 2:hasDMA : "; std::cin >> flag; if (1 == flag) { lacksDMA l("Tree", 100, "BLACK"); p[i] = &l; p[i]->View(); } else { hasDMA h("Tree", 100, "OMG"); p[i] = &h; p[i]->View(); } } system("pause"); return 0; }
编程练习4
//port.h #ifndef PORT_H_ #define PORT_H_ #include <iostream> class Port { private: char *brand; char style[20]; int bottles; public: Port(const char *br = "none", const char *st = "none", int b = 0); Port(const Port &p); virtual ~Port() { delete[] brand; } Port &operator=(const Port &p); Port &operator+=(int b); Port &operator-=(int b); int BottleCount() const { return bottles; } virtual void Show() const; friend std::ostream &operator<<(std::ostream &os, const Port &p); }; class VintagePort : public Port { private: char *nickname; int year; public: VintagePort(); VintagePort(const char *br, int b, const char *nn, int y); VintagePort(const VintagePort &vp); ~VintagePort() { delete[] nickname; } VintagePort &operator=(const VintagePort &vp); virtual void Show() const; friend std::ostream &operator<<(std::ostream &os, const VintagePort &vp); }; #endif
//port.cpp #include "Port.h" #include <iostream> Port::Port(const char *br, const char *st, int b) { brand = new char[strlen(br)+1]; strcpy(brand, br); strncpy(style, st,20); style[19] = '\0'; bottles = b; } Port::Port(const Port &p) { brand = new char[strlen(p.brand)+1]; strcpy(brand, p.brand); strncpy(style, p.style,20); style[19] = '\0'; bottles = p.bottles; } Port & Port::operator=(const Port &p) { if(this==&p) return *this; delete [] brand; brand = new char[strlen(p.brand)+1]; strcpy(brand, p.brand); strncpy(style, p.style,20); style[19] = '\0'; bottles = p.bottles; return *this; } Port & Port::operator+=(int b) { bottles +=b; return *this; } Port & Port::operator-=(int b) { bottles -=b; return *this; } void Port::Show() const { std::cout << "Brand: " << brand << std::endl; std::cout << "Kind: " << style << std::endl; std::cout << "Bottles: " << bottles << std::endl; } std::ostream &operator<<(std::ostream &os, const Port &p) { os << p.brand << ", " << p.style << ", " << p.bottles << '\n'; return os; } VintagePort::VintagePort():Port() { nickname = new char[1]; nickname = nullptr; year = 0; } VintagePort::VintagePort(const char *br, int b, const char *nn, int y):Port(br, "Vintage", b) { nickname = new char[strlen(nn)+1]; strcpy(nickname, nn); year = y; } VintagePort::VintagePort(const VintagePort &vp):Port(vp) { nickname = new char[strlen(vp.nickname) + 1]; strcpy(nickname, vp.nickname); year = vp.year; } VintagePort & VintagePort::operator=(const VintagePort &vp) { if(this == &vp) return *this; Port::operator=(vp); delete[] nickname; nickname = new char[strlen(vp.nickname) + 1]; strcpy(nickname, vp.nickname); year = vp.year; return *this; } void VintagePort::Show() const { Port::Show(); std::cout << "nick name: " << nickname << std::endl; std::cout << "year: " << year << std::endl; } std::ostream &operator<<(std::ostream &os, const VintagePort &vp) { os << (Port &)vp; std::cout << ", " << vp.nickname << ", " << vp.year; return os; }
//main.cpp #include "Port.h" #include <iostream> int main() { Port *ptr; Port p("Gallo", "tawny", 20); ptr = &p; ptr->Show(); std::cout << p << std::endl; VintagePort v("Gallo", 20, "gal", 20); ptr = &v; ptr->Show(); std::cout << v << std::endl; system("pause"); return 0; }
相关文章推荐
- 【学习C++】C++ Primer Plus (第六版)第十三章编程练习1-4
- c primer plus第十三章编程练习答案(纯手打欢迎指错)
- 《Python核心编程》第二版第405页第十三章练习 续二 -Python核心编程答案-自己做的-
- C primer plus(第五版)编程练习第十三章
- 《Python核心编程》第二版第405页第十三章练习 -Python核心编程答案-自己做的-
- 第十三章编程练习(4)
- 《Python核心编程》第二版第407页第十三章练习 续五 -Python核心编程答案-自己做的-
- C Primer Plus (第五版) 第十三章 文件输入/输出 编程练习
- C Primer Plus 第十三章 编程练习 1-13题
- 第十三章编程练习(3)
- C++ Primer Plus第五版 第十三章 编程练习答案
- 《Python核心编程》第二版第405页第十三章练习 续一 -Python核心编程答案-自己做的-
- 《Python核心编程》第二版第407页第十三章练习 续六 -Python核心编程答案-自己做的-
- C++ Primer Plus第六版 第十三章 编程练习答案
- (一四五)第十三章编程练习
- python 核心编程2 第十三章练习
- 第十三章编程练习(1)
- 《Python核心编程》第二版第405页第十三章练习 续三 -Python核心编程答案-自己做的-
- 《Python核心编程》第二版第407页第十三章练习 续四 -Python核心编程答案-自己做的-
- c++ primer plus(第6版)中文版 第十三章编程练习答案