c++ primer plus 第13章习题
2016-05-20 14:59
351 查看
//第13章 13.14 #ifndef STACK_H #define STACK_H #include<iostream> using std::ostream; using std::istream; class baseDMA { private: char *label; int rating; public: baseDMA(const char *l = "null", int r = 0); baseDMA(const baseDMA &rs); virtual ~baseDMA(); baseDMA & operator=(const baseDMA &rs); friend ostream & operator<<(ostream &os, const baseDMA &rs); }; class lacksDMA :public baseDMA { 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 baseDMA &rs); friend std::ostream &operator<<(ostream &os, const lacksDMA &rs); }; class hasDMA :public baseDMA { private: char *style; public: hasDMA(const char *s = "none", const char * l = "null", int r = 0); hasDMA(const char *s, const baseDMA &rs); hasDMA(const hasDMA &hs); ~hasDMA(); hasDMA & operator=(const hasDMA &rs); friend ostream & operator<<(ostream &os, const hasDMA &rs); }; #endif
//第13章 13.14 #include"zhan.h" #include<iostream> #include<cstring> baseDMA::baseDMA(const char *l , int r ) { label = new char[std::strlen(l) + 1]; std::strcpy(label, l); rating = r; } baseDMA::baseDMA(const baseDMA &rs) { label = new char[std::strlen(rs.label) + 1]; std::strcpy(label, rs.label); rating = rs.rating; } baseDMA::~baseDMA() { delete[]label; } baseDMA & baseDMA::operator=(const baseDMA &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; } ostream & operator<<(ostream &os, const baseDMA &rs)//此处不要加类名 { os << "Lable:" << rs.label << std::endl; os << "Rating:" << rs.rating << std::endl; return os; } lacksDMA::lacksDMA(const char *c, const char * l, int r) :baseDMA(l,r) { std::strncpy(color, c, 39);//把src所指向的字符串中以src地址开始的前n个字节复制到dest所指的数组中,并返回dest color[39] = '\0'; } lacksDMA::lacksDMA(const char *c, const baseDMA &rs) : baseDMA(rs)//? { std::strncpy(color, c, 39); color[39] = '\0'; } std::ostream &operator<<(ostream &os, const lacksDMA &rs) { os << (const baseDMA&)rs;//? os << "Color: " << rs.color << std::endl; return os; } hasDMA::hasDMA(const char *s, const char * l, int r) :baseDMA(l,r) { style = new char[strlen(s) + 1]; std::strcpy(style, s); } hasDMA::hasDMA(const char *s, const baseDMA &rs) : baseDMA(rs)//? { style = new char[strlen(s) + 1]; std::strcpy(style, s); } hasDMA::hasDMA(const hasDMA &hs) : baseDMA(hs) { style = new char[strlen(hs.style) + 1]; std::strcpy(style, hs.style); } hasDMA::~hasDMA() { delete[]style; } hasDMA & hasDMA::operator=(const hasDMA &rs) { if (this == &rs) return *this; baseDMA::operator=(rs); delete[]style; style = new char[strlen(rs.style) + 1]; std::strcpy(style, rs.style); return *this; } ostream & operator<<(ostream &os, const hasDMA &rs) { os << (const baseDMA &)rs; os << "Style: " << rs.style << std::endl; return os; }
//第13章 13.14 #include<iostream> #include"zhan.h" #include<stdlib.h> int main() { using std::cout; using std::endl; baseDMA shirt("Portabelly", 8); lacksDMA balloon("red", "Buffalo", 4); hasDMA map("Mercator", "Buffalo Keys", 5); cout << "Displaying baseDMA oject:\n"; cout << shirt << endl; cout << "Displaying lacksDMA object:\n"; cout << balloon << endl; cout << "Displaying hasDMA object:\n"; cout << map << endl; lacksDMA balloon2(balloon); cout << "Result of lackdDMA copy:\n"; cout << balloon2 << endl; hasDMA map2; map2 = map; cout << "Result of hasDMA assignment:\n"; cout << map2 << endl; system("pause"); return 0; }
//第13章第1题 #ifndef CLASS_H #define CLASS_H #include<iostream> using std::cin; using std::cout; using std::endl; class Cd { private: char performers[50]; char label[20]; int selections; double playtime; protected: char *perfor(){ return performers; }; char *lab(){ return label; }; int sel(){ return selections; }; double play(){ return playtime; }; public: Cd(char *s1, char *s2, int n, double x) { strcpy(performers, s1);//不用new了,因为performer 是数组 strcpy(label, s2); selections = n; playtime = x; } Cd(const Cd &d) { strcpy(performers, d.performers); strcpy(label, d.label); selections = d.selections; playtime = d.playtime; } Cd(){}; virtual ~Cd(){};//构造函数应为虚函数 virtual void Report()const { cout << "Perfomers:" << performers << endl; cout << "Label:" << label << endl; cout << "selection:" << selections << endl; cout << "playtime:" << playtime << endl; } Cd & Cd::operator=(const Cd &d) { //return Cd(d);//这个狠啊,用的构造函数。这个下边没法赋值,局部变量 if (this == &d) return *this; strcpy(performers, d.performers); strcpy(label, d.label); selections = d.selections; playtime = d.playtime; return *this; } }; class Classic : public Cd { private: char mainLabel[50]; public: Classic(char *s1, char *s2, char *s3, int n, double x) :Cd(s1, s2, n, x) { strcpy(mainLabel, s3);//这个也狠,调用了构造函数完成基类复制。 } Classic(Classic &c) :Cd(c.perfor(), c.lab(),c.sel(), c.play()) { strcpy(mainLabel, c.mainLabel); } Classic(){};//必须有函数体 virtual ~Classic(){}; virtual void Report()const { Cd::Report(); cout << "mainLabel:" << mainLabel << endl<<endl; } Classic & Classic::operator=(const Classic &c) { if (this == &c) return *this; Cd::operator=(c); strcpy(mainLabel, c.mainLabel); return *this; } }; #endif
//第13章第1题,第2题共用主函数 #include<iostream> using namespace std; #include"zhan.h" #include<cstdlib> void Bravo(const Cd &disk); int main() { Cd c1("Beatles", "Capitol", 14, 35.5); Classic c2 = Classic("Piano Sonata int 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 arguement:\n"; Bravo(c1); Bravo(c2); cout << "Testing assignment:"; Cd copy1; copy1 = c1; copy1.Report(); Classic copy; copy = c2; copy.Report(); system("pause"); return 0; } void Bravo(const Cd &disk) { disk.Report(); }
//第13章第2题 #ifndef CLASS_H #define CLASS_H #include<iostream> using std::cin; using std::cout; using std::endl; class Cd { private: char *performers; char *label; int selections; double playtime; public: Cd(char *s1, char *s2, int n, double x) { performers = new char[strlen(s1) + 1]; strcpy(performers, s1);//用new了,因为performer 是指针 label = new char[strlen(s2) + 1]; strcpy(label, s2); selections = n; playtime = x; } Cd(const Cd &d) { performers = new char[strlen(d.performers) + 1]; strcpy(performers, d.performers); label = new char[strlen(d.label) + 1]; strcpy(label, d.label); selections = d.selections; playtime = d.playtime; } Cd(){}; virtual ~Cd(){ delete[]performers; delete[]label; };//构造函数应为虚函数 virtual void Report()const { cout << "Perfomers:" << performers << endl; cout << "Label:" << label << endl; cout << "selection:" << selections << endl; cout << "playtime:" << playtime << endl; } Cd & Cd::operator=(const Cd &d) { //return Cd(d);//这个狠啊,用的构造函数。这个下边没法赋值,局部变量 if (this == &d) return *this; delete[]performers; delete[]label;//释放以前的字符串 performers = new char[strlen(d.performers) + 1]; strcpy(performers, d.performers); label = new char[strlen(d.label) + 1]; strcpy(label, d.label); selections = d.selections; playtime = d.playtime; return *this; } }; class Classic : public Cd { private: char * mainLabel; public: Classic(char *s1, char *s2, char *s3, int n, double x) :Cd(s1, s2, n, x) { mainLabel = new char[strlen(s3) + 1]; strcpy(mainLabel, s3);//这个也狠,调用了构造函数完成基类复制。 } Classic(Classic &c) :Cd(c) { mainLabel = new char[strlen(c.mainLabel) + 1]; strcpy(mainLabel, c.mainLabel); } Classic(){};//必须有函数体 virtual ~Classic(){ delete[]mainLabel; }; virtual void Report()const { Cd::Report(); cout << "mainLabel:" << mainLabel << endl << endl; } Classic & Classic::operator=(const Classic &c) { if (this == &c) return *this; Cd::operator=(c); delete[]mainLabel; mainLabel = new char[strlen(c.mainLabel) + 1]; strcpy(mainLabel, c.mainLabel); return *this; } }; #endif
//第13章第1题,第2题共用主函数 #include<iostream> using namespace std; #include"zhan.h" #include<cstdlib> void Bravo(const Cd &disk); int main() { Cd c1("Beatles", "Capitol", 14, 35.5); Classic c2 = Classic("Piano Sonata int 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 arguement:\n"; Bravo(c1); Bravo(c2); cout << "Testing assignment:"; Cd copy1; copy1 = c1; copy1.Report(); Classic copy; copy = c2; copy.Report(); system("pause"); return 0; } void Bravo(const Cd &disk) { disk.Report(); }
//第13章第3题 #ifndef CLASS_H #define CLASS_H #include<iostream> #include<string> using namespace std; class DMA { private: string label; int rating; public: DMA(const string l = "NULL", int r = 0) { label = l; rating = r; } DMA(const DMA &rs) { label = rs.label; rating = rs.rating; } virtual void test()const = 0{};//纯虚函数。 virtual void test2() { cout << "test2" << endl; } virtual ~DMA(){}; friend ostream &operator<<(ostream &os, const DMA &rs) { os << "label:" << rs.label << " rating :" << rs.rating << endl; return os; } virtual void show() { cout << "label:" << label << " rating:" << rating << " " << endl; } }; class baseDMA :public DMA { public: baseDMA(const string l = "NULL", int r = 0) :DMA(l, r) {} baseDMA(baseDMA &bd) :DMA(bd){} virtual void test()const{};//必须实现虚基类的所有虚函数 virtual ~baseDMA(){}//虚函数是为了继承接口和默认行为 纯虚函数只是继承接口,行为必须重新定义 friend ostream &operator<<(ostream &os, const baseDMA &bd) { os << "This is baseDMA:"; os << (DMA &)bd;//通过强制转换调用基类友元函数 return os; } virtual void show() { cout << "This is baseDMA:"; DMA::show(); cout << endl; } }; class lacksDMA :public DMA { private: string color; public: lacksDMA(const string c = "blank", const string l = "NULL", int r = 0) :DMA(l, r) { color = c; } virtual ~lacksDMA(){}; virtual void test()const{}; friend ostream &operator<<(ostream &os, const lacksDMA &ld) { os << "This is lacksDMA:"; os << (DMA &)ld << " color:" << ld.color; return os; } virtual void show() { cout << "This is lacksDMA:"; DMA::show(); cout << " color:" << color; cout << endl; } }; class hasDMA :public DMA { private: string style; public: hasDMA(const string s = "none", const string l = "null", int r = 0) :DMA(l, r) { style = s; } virtual ~hasDMA(){}; virtual void test()const{}; virtual void show() { cout << "This is hasDMA: "; DMA::show(); cout << " style:" << style; cout << endl; } friend ostream &operator<<(ostream &os, const hasDMA &hd) { os << "This is hasDMA:"; os << (DMA &)hd << " style:" << hd.style; return os; } }; #endif
//第13章第3题 #include<iostream> #include"zhan.h" using namespace std; void main() { DMA *pd[3];//虚基类不能创建对象,但可以创建指向其的指针 for (int i = 0; i < 3; i++) { cout << "\nEnter the label:"; string label; getline(cin, label); cout << "\nEnter the rating:"; int rat; cin >> rat; cout << "Enter the 1 for baseDMA" << endl << "2 for lacksDMA" << endl << "3 for hasDMA" << endl; int temp; cin >> temp; cin.get(); if (temp==1) pd[i] = new baseDMA(label, rat); else if (temp == 2) { cout << "Enter the color:"; string color; getline(cin, color); pd[i] = new lacksDMA(color, label, rat); } else if (temp == 3) { cout << "Enter the style:"; string style; getline(cin, style); pd[i] = new hasDMA(style, label, rat); } else { cout <<"Please try it again!" << endl; i--; } while (cin.get() != '\n') continue; } cout << endl; for (int i = 0; i < 3; i++) { pd[i]->show(); } system("pause"); }
//第13章第4题 #ifndef CLASS_H #define CLASS_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) { brand = new char[strlen(br) + 1]; strcpy(brand, br); strcpy(style, st); bottles = b; } Port(const Port &p) { brand = new char[strlen(p.brand) + 1]; strcpy(brand, p.brand); strcpy(style, p.style); bottles = p.bottles; } virtual~Port(){ delete[]brand; } Port &operator=(const Port &p) { if (this == &p) return *this; delete[]brand;//必须要有 brand = new char[strlen(p.brand) + 1]; strcpy(brand, p.brand); strcpy(style, p.style); bottles = p.bottles; return *this; } Port &operator+=(int b) { bottles += b; return *this; } Port &operator-=(int b) { bottles-= b; return *this; } int BottleCount()const { return bottles; }; virtual void Show()const { cout << "Brand:" << brand<<endl; cout << "Styles:" << style << endl; cout << "Bottles:" << bottles << endl; } friend ostream &operator<<(ostream &os, const Port &p) { cout << p.brand << ", " << p.style << ", " << p.bottles << endl; return os; } }; class VintagePort :public Port { private: char * nickname; int year; public: VintagePort() :Port() { nickname = NULL; year = 0; } 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(const VintagePort &vp) :Port(vp) { nickname = new char[strlen(vp.nickname) + 1]; strcpy(nickname, vp.nickname); year = vp.year; } ~VintagePort(){ delete[]nickname; }; VintagePort &operator=(const VintagePort &vp) { if (this == &vp) return *this; delete[]nickname; Port::operator=(vp); nickname = new char[strlen(vp.nickname) + 1]; strcpy(nickname, vp.nickname); year = vp.year; return *this; } void Show()const { Port::Show(); cout << "nickname:" << nickname << endl; cout << "year:" << year << endl; } friend ostream & operator<<(ostream & os, const VintagePort &vp) { os << (Port &)vp; cout << ","<<vp.nickname << "," << vp.year; return os; } }; #endif
//第13章第4题 #include"zhan.h" int main() { Port *ptr; Port p("Gallo", "tawny", 20); ptr = &p; ptr->Show(); cout << p << endl; VintagePort v("gallo", 20, "ttt", 20); ptr = &v; ptr->Show(); cout << v << endl; system("pause"); return 0; }
相关文章推荐
- L1-007. 念数字(C++)
- C++ Primer Plus第6版18个重点笔记
- c++primer plus 第12章习题
- C++sort()函数的用法
- java面试需要准备什么呢?c++可以看看
- c++类型转换运算符
- C++ builder 添加资源文件
- 【C++】内联inline、继承、重载与虚函数的解释
- c++调用python
- C++ 无锁队列 ABA <3>
- C++第六次实验-输出星号图
- C++&OpenCV实现抠除(也可用于更换)证件照背景
- 基于arm的C++反汇编 qemu-arm环境搭建
- C++作业6
- C/C++函数srand()和rand()的使用
- C++中对象或其对象指针的赋值
- C++课堂作业二之反转链表
- C++生成n个指定1到 n 不同的随机数
- C与C++之间的调用
- C++重载