13-1
注意char*前面加const,不然就会报错
Classis.h

#ifndef CLASSIC_H_
#define CLASS_H_
#include <string>
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(const Cd& d);Cd();virtual ~Cd();virtual void Report() const;virtual Cd& operator=(const Cd& d);
};
class Classic:public Cd
{private:char major[50];
public:Classic(const char* s1, const char* s2, const char* s3, int n, double x);Classic();~Classic();void Report();Classic& operator=(const Classic& d);
};
#endif // !CLASSIC_H_

Classic.cpp

#include <iostream>
#include "Classic.h"Cd::Cd(const char* s1, const char* s2, int n, double x)
{strcpy_s(performers, strlen(s1) + 1, s1);strcpy_s(label, strlen(s2) + 1, s2);selections = n;playtime = x;
}
Cd::Cd(const Cd& d)
{strcpy_s(performers, strlen(d.performers) + 1, d.performers);strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;
}
Cd::Cd()
{performers[0] = '\0';label[0] = '\0';selections = 0;playtime = 0.0;
}
Cd::~Cd()
{}
void Cd::Report() const
{std::cout << "Preformers: " << performers << '\n';std::cout << "Label: " << label << '\n';std::cout << "selectiongs: " << selections << '\n';std::cout << "playtime: " << playtime << '\n';
}
Cd& Cd::operator=(const Cd& d)
{if (this == &d)return *this;strcpy_s(performers, strlen(d.performers) + 1, d.performers);strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;return *this;
}
Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x):Cd(s2, s3, x, n)
{strcpy_s(major, strlen(s1)+1, s1);
}
Classic::Classic():Cd()
{major[0] = '\0';
}
Classic::~Classic()
{}
void Classic::Report()
{Cd::Report();std::cout << "Major: " << major << '\n';
}
Classic& Classic::operator=(const Classic& d)
{if (this == &d)return *this;Cd::operator=(d);strcpy_s(major, strlen(d.major) + 1, d.major);return *this;
}

main.cpp

#include <iostream>
#include "Classic.h"
using std::cout;
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 << "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();
}

13-2
主函数仍然不变
Classic.h

#ifndef CLASSIC_H_
#define CLASSIC_H_
#include <string>
class Cd {private:char* performers;char* label;int selections;double playtime;
public:Cd(const char* s1, const char* s2, int n, double x);Cd(const Cd& d);Cd();virtual ~Cd();virtual void Report() const;virtual Cd& operator=(const Cd& d);
};
class Classic:public Cd
{private:char* major;
public:Classic(const char* s1, const char* s2, const char* s3, int n, double x);Classic();~Classic();void Report();Classic& operator=(const Classic& d);
};
#endif // !CLASSIC_H_

Classic.cpp

#include <iostream>
#include "Classic.h"Cd::Cd(const char* s1, const char* s2, int n, double x)
{performers = new char[strlen(s1) + 1];strcpy_s(performers, strlen(s1) + 1, s1);label = new char[strlen(s2) + 1];strcpy_s(label, strlen(s2) + 1, s2);selections = n;playtime = x;
}
Cd::Cd(const Cd& d)
{performers = new char[strlen(d.performers) + 1];strcpy_s(performers, strlen(d.performers) + 1, d.performers);label = new char[strlen(d.label) + 1];strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;
}
Cd::Cd()
{performers = new char[1];performers[0] = '\0';label = new char[1];label[0] = '\0';selections = 0;playtime = 0.0;
}
Cd::~Cd()
{delete[] performers;delete[] label;
}
void Cd::Report() const
{std::cout << "Preformers: " << performers << '\n';std::cout << "Label: " << label << '\n';std::cout << "selectiongs: " << selections << '\n';std::cout << "playtime: " << playtime << '\n';
}
Cd& Cd::operator=(const Cd& d)
{if (this == &d)return *this;delete[] performers;delete[] label;performers = new char[strlen(d.performers) + 1];strcpy_s(performers, strlen(d.performers) + 1, d.performers);label = new char[strlen(d.label) + 1];strcpy_s(label, strlen(d.label) + 1, d.label);selections = d.selections;playtime = d.playtime;return *this;
}
Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x):Cd(s2, s3, x, n)
{major = new char[strlen(s1) + 1];strcpy_s(major, strlen(s1)+1, s1);
}
Classic::Classic():Cd()
{major = new char[1];major[0] = '\0';
}
Classic::~Classic()
{delete[] major;
}
void Classic::Report()
{Cd::Report();std::cout << "Major: " << major << '\n';
}
Classic& Classic::operator=(const Classic& d)
{if (this == &d)return *this;Cd::operator=(d);delete[] major;major = new char[strlen(d.major) + 1];strcpy_s(major, strlen(d.major) + 1, d.major);return *this;
}

13-3
虽然不是很理解,但是这个套路是明白了
dma.h

#ifndef DMA_H_
#define DMA_H_
#include <iostream>class ABC
{char* fullname;int level;
public:ABC(const char* f = "null", int I = 0);ABC(const ABC& ab);virtual ~ABC();ABC& operator=(const ABC& ab);virtual void show();
};class baseDMA: public ABC
{private:char* label;int rating;
public:baseDMA(const char* l = "null", int r = 0, const char* f = "null", int lv = 0);baseDMA(const baseDMA& rs);~baseDMA();baseDMA& operator=(const baseDMA& rs);virtual void show();
};class lacksDMA: public ABC
{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);virtual void show();
};class hasDMA: public ABC
{private:char* style;
public:hasDMA(const char* s = "none", const char* l = "null", int r = 0);hasDMA(const char* s, const baseDMA& hs);hasDMA(const hasDMA& hs);~hasDMA();hasDMA& operator=(const hasDMA& rs);virtual void show();
};
#endif // !DMA_H_

dma.cpp

#include<cstring>
#include<string>
#include "dma.h"ABC::ABC(const char* a, int b)
{fullname = new char[strlen(a) + 1];strcpy_s(fullname, strlen(a) + 1, a);level = b;
}
ABC::ABC(const ABC& ab)
{fullname = new char[strlen(ab.fullname) + 1];strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);level = ab.level;
}
ABC::~ABC()
{delete[] fullname;
}
ABC& ABC::operator=(const ABC& ab)
{if (this == &ab)return *this;delete[] fullname;fullname = new char[strlen(ab.fullname) + 1];strcpy_s(fullname, strlen(ab.fullname) + 1, ab.fullname);level = ab.level;return *this;
}
void ABC::show()
{std::cout << "fullname: " << fullname << std::endl;std::cout << "level: " << level << std::endl;
}baseDMA::baseDMA(const char* l, int r, const char* f, int lv):ABC(f, lv)
{label = new char[strlen(l) + 1];strcpy_s(label, strlen(l) + 1, l);rating = r;
}
baseDMA::baseDMA(const baseDMA& rs):ABC(rs)
{label = new char[strlen(rs.label) + 1];strcpy_s(label, strlen(rs.label) + 1, rs.label);rating = rs.rating;
}
baseDMA::~baseDMA()
{delete[] label;
}
baseDMA& baseDMA::operator=(const baseDMA& rs)
{if (this == &rs)return *this;ABC::operator=(rs);delete[] label;label = new char[strlen(rs.label) + 1];strcpy_s(label, strlen(rs.label) + 1, rs.label);rating = rs.rating;return *this;
}
void baseDMA::show()
{ABC::show();std::cout << "label: " << label << std::endl;std::cout << "rating: " << rating << std::endl;
}
lacksDMA::lacksDMA(const char* c, const char* l, int r):ABC(l, r)
{strcpy_s(color, strlen(c) + 1, c);
}
lacksDMA::lacksDMA(const char* c, const baseDMA& rs):ABC(rs)
{strcpy_s(color, strlen(c) + 1, c);
}
void lacksDMA::show()
{ABC::show();std::cout << "color: " << color << std::endl;
}
hasDMA::hasDMA(const char* s, const char* l, int r):ABC(l, r)
{style = new char[strlen(s) + 1];strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const char* s, const baseDMA& hs):ABC(hs)
{style = new char[strlen(s) + 1];strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const hasDMA& hs):ABC(hs)
{style = new char[strlen(hs.style) + 1];strcpy_s(style, strlen(hs.style) + 1, hs.style);
}
hasDMA::~hasDMA()
{delete[] style;
}
hasDMA& hasDMA::operator=(const hasDMA& hs)
{if (this == &hs)return *this;ABC::operator=(hs);delete[] style;style = new char[strlen(hs.style) + 1];strcpy_s(style, strlen(hs.style) + 1, hs.style);return *this;
}
void hasDMA::show()
{ABC::show();std::cout << "style: " << style << std::endl;
}

main.cpp

#include <iostream>
#include "dma.h"
using std::cout;int main()
{baseDMA a1("asdfadsf", 9, "dsfadfa", 1);lacksDMA a2("dfafda", "daf d", 2);hasDMA a3("sdfadfas", "dfqwedsa", 3);cout << "baseDMA:\n";a1.show();cout << "lacksDMA:\n";a2.show();cout << "hasDMA:\n";a3.show();baseDMA a4 = a1;lacksDMA a5 = a2;hasDMA a6 = a3;return 0;
}

13-4
Port.h

#ifndef PORT_H_
#define PORT_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();VintagePort(const char* br, const char* st, int b, const char* nn, int y);VintagePort(const VintagePort& vp);~VintagePort() { delete[] nickname; }void Show() const;friend ostream& operator<<(ostream& os, const VintagePort& vp);
};
#endif // !PORT_H_

Port.cpp

#include "Port.h"
#include <cstring>
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, 20, st);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, 20, p.style);bottles = p.bottles;
}Port& Port::operator=(const Port& p)
{if (this == &p)return *this;delete[] 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;return *this;return *this;// TODO: 在此处插入 return 语句
}Port& Port::operator+=(int b)
{bottles += b;return *this;// TODO: 在此处插入 return 语句
}Port& Port::operator-=(int b)
{bottles -= b;return *this;    // TODO: 在此处插入 return 语句
}void Port::Show() const
{std::cout << "Brand: " << brand << std::endl;std::cout << "King: " << style << std::endl;std::cout << "Bottles: " << bottles << std::endl;
}ostream& operator<<(ostream& os, const Port& p)
{std::cout << p.brand << ", " << p.style << ", " << p.bottles << std::endl;return os;// TODO: 在此处插入 return 语句
}ostream& operator<<(ostream& os, const VintagePort& vp)
{os << (const Port&)vp;std::cout << vp.nickname << ", " << vp.year << std::endl;return os;// TODO: 在此处插入 return 语句
}VintagePort::VintagePort()
{nickname = new char[1];nickname[0] = '\0';year = 0;
}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)
{nickname = new char[strlen(vp.nickname) + 1];strcpy_s(nickname, strlen(vp.nickname), vp.nickname);year = vp.year;
}void VintagePort::Show() const
{Port::Show();std::cout << "Nickname: " << nickname << std::endl;std::cout << "Year: " << year << std::endl;
}

main.cpp

#include <iostream>
#include "Port.h"
using std::cout;int main()
{Port wine1("Gallo", "tawny", 20);VintagePort wine2("Romance Conti", "vintage", 10, "The Noble", 1876);VintagePort wine3("Merlot", "ruby", 30, "Old Velvet", 1888);cout << "Show Port object:\n";wine1.Show();cout << wine1 << endl;cout << "Show VintagePort object:\n";wine2.Show();cout << wine2 << endl;cout << "Show VintagePort object:\n";wine3.Show();cout << wine3 << endl;Port(wine4) = wine1;cout << "Show VintagePort object:\n";wine4.Show();cout << wine4<< endl;return 0;
}

C++PrimerPlus学习——第十三章编程练习相关推荐

  1. C++PrimerPlus学习——第四章编程练习

    **疫情期间学习C++ 4-1 需要使用cin.get()设置读取位数,避免空格导致无法读取多个词 #include <iostream> struct info_people //def ...

  2. C++PrimerPlus学习——第十七章编程练习

    17-1 不知道有没有理解错题意,参考list17.14 #include <iostream>int main() {using std::cout;using std::cin;usi ...

  3. C++PrimerPlus学习——第十一章编程练习

    11-1 应该是修改list11.15,当当官方店买的,难道是盗版书吗... 打开file之后,操作跟cout类似 vect.h #ifndef VECT_h_ #define VECT_h_ #in ...

  4. C++PrimerPlus学习——第七章编程练习

    感觉变困难了很多,必须要注意细节,不如就会出各种bug 7-1 #include <iostream> double average(double a, double b);int mai ...

  5. C++PrimerPlus学习——第六章编程练习

    6-1 有个问题,如果输入的字符既不是数字也不是字母是不是应该原样输出呢? #include <iostream> #include <cctype>int main() {u ...

  6. MVC3学习第十三章 佟掌柜第二弹——MVC3下利用陕北吴旗娃的分页控件实现数据分页...

    本章学习内容 1.了解陕北吴旗娃的Mvc分页控件 2.利用分页控件实现MVC3下的商品分页 3.利用分页控件实现MVC3下一个页面多个分页以及ajax分页效果 1.了解陕北吴旗娃的Mvc分页控件 在w ...

  7. CCNA学习指南十三章

    IPV6 IPV6地址类型: 单播地址:单播地址分为: 1.全球单播地址,是典型的.可路由的通用地址,像IPV4中的通用地址一样. 2.链路本地地址,就是IPV4中的专用(私有)地址,它们是不能路由的 ...

  8. java aio socket_java核心学习(三十三) 网络编程---AIO实现异步Socket通信

    AIO需要操作系统的支持,在linux内核2.6版本中加入了对真正异步IO的支持,java从jdk1.7开始支持AIO 核心类有AsynchronousSocketChannel .Asynchron ...

  9. [转]Windows Shell 编程 第十三章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7988004】...

    第十三章 Windows脚本环境 现在的许多开发人员以前都是在MS-DOS环境下编程的.几乎所有人都接触过批处理文件--一种基于文本命令的文件.这种文件使你能够在一个可执行命令中组合多个指令.批处理文 ...

最新文章

  1. 用word2007发布blog
  2. *迭代 分支回收、创建偷懒脚本
  3. (转)所有iOS设备的屏幕分辨率
  4. css实验内容,12个令人惊叹的CSS实验项目
  5. 一点一点看JDK源码(五)java.util.ArrayList 后篇之forEach
  6. 草稿--深度学习cache系列
  7. 我的世界服务器的文件名叫什么,我的世界 外国服务器叫什么名字 | 手游网游页游攻略大全...
  8. 5分钟 0元搭建个人独立博客网站(一)
  9. PHP下载CSS文件中的图片
  10. java模块化发布选型_Java模块化开发
  11. ip转换器哪个好用_中英文翻译软件哪个好?试试这两个就知道了
  12. [转]MVP+WCF+三层结构搭建项目框架
  13. 单源最短路径的Bellman-Ford算法。
  14. windows应用程序签名
  15. 计算机数据类型误差怎么解决,测绘数据常见误差类型及处理方法
  16. 家庭生涯妙招,必定要看哦
  17. html的空心箭头,CSS实现空心三角指示箭头
  18. 如何选择适合你的兴趣爱好(六十一),瓷器
  19. ASML EUV 光源的极限工程
  20. 基于色彩恒常( color constancy)特性的Frankle-McCann Retinex图像增强

热门文章

  1. 深入研究java.lang.Runtime类【转】
  2. windows 下 git 禁用 CRLF 转换 LF
  3. vs运行时候冒了这个错:无法启动IIS Express Web 服务器~Win10
  4. python编码器_自编码器和分类器python
  5. Git fetch pull 详解
  6. linux查询内核参数命令,Linux内核启动参数详解
  7. java 视图解析器_SpringMVC——视图和视图解析器
  8. [转载] python通过adb获取android手机耗电量
  9. treeset java_Java TreeSet add()方法与示例
  10. 数据库如何处理数据库太大_网络数据库中的数据处理