C++ PrimerPlus第六版第十三章编程练习答案

mac2024-05-28  36

1.

cd.h

#ifndef cd_H_ #define cd_H_ #include<string> #include <iostream> #include <cstring> using std::cout; class Cd { private: char performers[50]; char label[20]; int selections; double playtime; public: Cd(const char * s1, const char * s2, int n, double x); Cd() {} virtual~Cd() {} virtual void Report() const; }; class Classic :public Cd { private: char works[50]; public: Classic(const char * s1, const char *s2, const char * s3, int n, double x); Classic() :Cd() {} virtual void Report() const; }; Cd::Cd(const char * s1, const char * s2, int n, double x) { strncpy_s(performers, s1, 50); performers[49] = '\0'; strncpy_s(label, s2, 20); label[19] = '\0'; selections = n; playtime = x; } void Cd::Report() const { cout << "performers: " << performers << '\n'; cout << "label: " << label << '\n'; cout << "selections: " << selections << '\n'; cout << "playtime: " << playtime << '\n'; } Classic::Classic(const char * s1, const char *s2, const char * s3, int n, double x) :Cd(s2, s3, n, x) { strncpy_s(works, s1, 50); works[49] = '\0'; } void Classic::Report() const { cout << "works: " << works << '\n'; Cd::Report(); } #endif

main.cpp

#include "cd.h" using namespace std; void Bravo(const Cd & disk); int main() { Cd c1("Beatles", "Capitol", 14, 35.5); Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17); Cd *pcd = &c1; cout << "Using object directly:\n"; c1.Report(); c2.Report(); cout << "Using type cd * pointer to objects:\n"; pcd->Report(); pcd = &c2; pcd->Report(); cout << "Calling a function with a Cd reference argument:\n"; Bravo(c1); Bravo(c2); cout << "Testing assignment: "; Classic Copy; Copy = c2; Copy.Report(); return 0; } void Bravo(const Cd & disk) { disk.Report(); }

2.

main.cpp和上一题一样的

#ifndef cd_H_ #define cd_H_ #include <iostream> #include<string> using std::cout; class Cd { private: char *performers; char *label; int selections; double playtime; public: Cd(const char * s1, const char * s2, int n, double x); Cd(); virtual~Cd(); virtual void Report() const; Cd & operator=(const Cd & d); }; class Classic :public Cd { private: char *works; public: Classic(const char * s1, const char *s2, const char * s3, int n, double x); Classic(); ~Classic(); virtual void Report() const; Classic & operator=(const Classic & c); }; Cd::Cd() { performers = nullptr; label = nullptr; selections = 0; playtime = 0.0; } Cd::Cd(const char * s1, const char * s2, int n, double x) { int i = strlen(s1); performers = new char[i + 1]; strcpy_s(performers, i+1,s1); i = strlen(s2); label = new char[i + 1]; strcpy_s(label,i+1, s2); selections = n; playtime = x; } Cd::~Cd() { delete[] performers; delete[] label; } void Cd::Report() const { cout << "performers: " << performers << '\n'; cout << "label: " << label << '\n'; cout << "selections: " << selections << '\n'; cout << "playtime: " << playtime << '\n'; } Cd & Cd::operator=(const Cd & d) { char * temp1 = performers; char * temp2 = label; performers = new char[strlen(d.performers) + 1]; label = new char[strlen(d.label) + 1]; delete[]temp1; delete[]temp2; strcpy_s(performers, strlen(d.performers) + 1, d.performers); strcpy_s(label, strlen(d.label) + 1, d.label); return *this; } Classic::Classic(const char * s1, const char *s2, const char * s3, int n, double x) :Cd(s2, s3, n, x) { int i = strlen(s1); works = new char[i + 1]; strcpy_s(works,i+1 ,s1); } void Classic::Report() const { cout << "works: " << works << '\n'; Cd::Report(); } Classic & Classic::operator=(const Classic & c) { Cd::operator=(c); char * temp = works; works = new char[strlen(c.works) + 1]; strcpy_s(works, strlen(c.works) + 1, c.works); return *this; } Classic::Classic() :Cd() { works = nullptr; } Classic::~Classic() { delete[] works; } #endif

3.

DAM.h

#ifndef DMA_H_ #define DMA_H_ #include<iostream> #include<cstring> class DMAABS { private: char * label; int rating; public: virtual ~DMAABS(); DMAABS(const char * l = "null", int r = 0); DMAABS(const DMAABS& rs); DMAABS & operator=(const DMAABS&rs); friend std::ostream&operator<<(std::ostream & os, const DMAABS & rs); virtual void view() const = 0; }; class baseDMA :public DMAABS { public: baseDMA(const char * l = "null", int r = 0) :DMAABS(l, r) {} baseDMA(const baseDMA & rs) :DMAABS(rs) {} virtual void view() const; }; class lacksDMA :public DMAABS { private: enum { COL_LEN = 40 }; char color[COL_LEN]; public: lacksDMA(const char *c = "blank", const char * l = "null", int r = 0); lacksDMA(const char *c, const DMAABS & rs); friend std::ostream & operator<<(std::ostream & os, const lacksDMA & rs); virtual void view()const; }; class hasDMA :public DMAABS { private: char * style; public: hasDMA(const char *s = "none", const char * l = "null", int r = 0); hasDMA(const char *s, const DMAABS & rs); hasDMA(const hasDMA& hs); ~hasDMA(); hasDMA & operator=(const hasDMA & rs); friend std::ostream & operator<<(std::ostream & os, const hasDMA& rs); virtual void view()const; }; DMAABS::DMAABS(const char * l, int r) { label = new char[strlen(l) + 1]; strcpy_s(label,strlen(l) + 1, l); rating = r; } DMAABS::DMAABS(const DMAABS& rs) { label = new char[strlen(rs.label) + 1]; strcpy_s(label, strlen(rs.label) + 1,rs.label); rating = rs.rating; } DMAABS::~DMAABS() { delete[] label; } DMAABS & DMAABS::operator=(const DMAABS & rs) { char * temp = label; label = new char[strlen(rs.label) + 1]; strcpy_s(label,strlen(rs.label)+1, rs.label); rating = rs.rating; delete[]temp; return *this; } std::ostream & operator<<(std::ostream & os, const DMAABS & rs) { os << "Label: " << rs.label << std::endl; os << "Rating: " << rs.rating << std::endl; return os; } void DMAABS::view() const { std::cout << "Label: " << label << std::endl; std::cout << "Rating: " << rating << std::endl; } void baseDMA::view() const { DMAABS::view(); } lacksDMA::lacksDMA(const char *c, const char * l, int r) :DMAABS(l, r) { strcpy_s(color, c); color[COL_LEN - 1] = '\0'; } lacksDMA::lacksDMA(const char * c, const DMAABS & rs) : DMAABS(rs) { strcpy_s(color, c); color[COL_LEN - 1] = '\0'; } std::ostream& operator<<(std::ostream& os, const lacksDMA & ls) { os << (const DMAABS&)ls; os << "Color: " << ls.color << std::endl; return os; } void lacksDMA::view() const { DMAABS::view(); std::cout << "Color: " << color << std::endl; } hasDMA::hasDMA(const char * s, const char * l, int r) :DMAABS(l, r) { style = new char[strlen(s) + 1]; strcpy_s(style, strlen(s) + 1, s); } hasDMA::hasDMA(const char * s, const DMAABS & rs) : DMAABS(rs) { style = new char[strlen(s) + 1]; strcpy_s(style, strlen(s) + 1, s); } hasDMA::hasDMA(const hasDMA & hs):DMAABS(hs) { style = new char[strlen(hs.style) + 1]; strcpy_s(style, strlen(hs.style + 1), hs.style); } hasDMA::~hasDMA() { delete[]style; } void hasDMA::view()const { DMAABS::view(); std::cout << "Style: " << style << std::endl; } hasDMA & hasDMA::operator=(const hasDMA & hs) { char * temp = style; DMAABS::operator=(hs); style = new char[strlen(hs.style) + 1]; strcpy_s(style, strlen(hs.style) + 1, hs.style); delete[] temp; return *this; } std::ostream& operator<<(std::ostream& os, const hasDMA& hs) { os << (const DMAABS&)hs; os << "Style: " << hs.style << std::endl; return os; } #endif

main.cpp

// study.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include "pch.h" #include "DAM.h" const int form = 1; int main() { using std::cin; using std::cout; using std::endl; DMAABS * p[form]; char temp[40]; char tcolor[40]; char tstyle[40]; int tempnum; int kind; for (int i = 0; i < form; ++i) { cout << "Enter label: " << endl; cin.getline(temp, 40); cout << "Enter rating: " << endl; cin >> tempnum; cout << "Enter 1 for baseDMA, 2 for lacksDMA, 3 for hasDMA: "; while (cin >> kind && (kind != 1 && kind != 2 && kind != 3)) cout << "Enter either 1, 2 or 3: " << endl; if (kind == 1) p[i] = new baseDMA(temp, tempnum); else if (kind == 2) { cout << "Enter color: " << endl; cin >> tcolor; p[i] = new lacksDMA(tcolor, temp, tempnum); } else if (kind == 3) { cout << "Enter the style: " << endl; cin >> tstyle; p[i] = new hasDMA(tstyle, temp, tempnum); } while (cin.get() != '\n') continue; } cout << endl; for (int i = 0; i < form; i++) { p[i]->view(); cout << endl; } for (int i = 0; i < form; i++) { delete p[i]; } cout << "Done.\n"; return 0; }

4. head.h

#ifndef QUEUE_H_ #define QUEUE_H_ #include<iostream> using namespace std; 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 ostream & operator<<(ostream & os, const Port & p); }; class VintagePort :public Port { private: char * nickname; int year; public: VintagePort() :Port(), nickname(nullptr), year(0) {} VintagePort(const char * br, const char * st, int b, const char * nn, int y); VintagePort(const VintagePort & vp); ~VintagePort() { delete[]nickname; } VintagePort &operator=(const VintagePort & vp); void Show() const; friend ostream & operator<<(ostream & os, const VintagePort & vp); }; Port::Port(const char * br, const char * st, int b) { brand = new char[strlen(br) + 1]; strcpy_s(brand,strlen(br)+1, br); strcpy_s(style, st); style[19] = '\0'; bottles = b; } Port::Port(const Port & p) { brand = new char[strlen(p.brand) + 1]; strcpy_s(brand, strlen(p.brand) + 1, p.brand); strcpy_s(style, p.style); bottles = p.bottles; } Port & Port::operator=(const Port & p) { char * temp = brand; brand = new char[strlen(p.brand) + 1]; strcpy_s(brand, strlen(p.brand) + 1,p.brand); strcpy_s(style, p.style); bottles = p.bottles; delete[]temp; return *this; } Port & Port::operator+=(int b) { bottles += b; return *this; } Port &Port::operator-=(int b) { if (bottles >= b) bottles -= b; else cout << "error"; return *this; } void Port::Show() const { cout << "Brand: " << brand << '\n'; cout << "Kind: " << style << '\n'; cout << "Bottles: " << bottles << '\n'; } ostream & operator<<(ostream & os, const Port & p) { os << p.brand << ", " << p.style << ", " << p.bottles; return os; } VintagePort::VintagePort(const char * br, const char * st, int b, const char * nn, int y) : Port(br, st, b) { nickname = new char[strlen(nn) + 1]; strcpy_s(nickname, strlen(nn) + 1, nn); year = y; } VintagePort::VintagePort(const VintagePort & vp) :Port(vp) { nickname = new char[strlen(vp.nickname) + 1]; strcpy_s(nickname, strlen(vp.nickname) + 1,vp.nickname); year = vp.year; } VintagePort & VintagePort::operator=(const VintagePort & vp) { char *temp = nickname; Port::operator=(vp); nickname = new char[strlen(vp.nickname) + 1]; strcpy_s(nickname, strlen(vp.nickname) + 1,vp.nickname); year = vp.year; delete[] temp; return *this; } void VintagePort::Show()const { Port::Show(); cout << "Nickename: " << nickname << '\n'; cout << "Year: " << year << '\n'; } ostream & operator<<(ostream & os, const VintagePort & vp) { os << (const Port&)vp; os << ", " << vp.nickname << ", " << vp.year; return os; } #endif

main.cpp

#include "head.h" int main() { Port port1("gallo", "tawny", 20); cout << port1 << endl; VintagePort vp("gallo", "vintage", 24, "Old Velvet", 16); VintagePort vp2(vp); cout << vp2 << endl; VintagePort vp3; vp3 = vp; cout << vp3 << endl; Port * p_port; p_port = &port1; p_port->Show(); cout << endl; p_port = &vp; p_port->Show(); cout << endl; return 0; }

b.因为有的方法需要重新定义,有的方法不需要 c.赋值运算符不能继承,operator<<为友元函数,所以不声明为虚的.

最新回复(0)