当前位置: 首页 > news >正文

C++Primerplus 编程练习 第十三章

第十三章

第一题

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//classic.h
#pragma once
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;Cd &operator=(const Cd &d);
};
class Classic : public Cd
{
private:                                                                           char name[80];public:Classic();Classic(const char *s1, const char *s2,const char*s3, int n, double x);Classic(const Classic & cl);void Report() const;Classic &operator=(const Classic &cl);
};
//classic.cpp
#include "classic.h"#include <cstring>
#include <iostream>Cd::Cd(const char *s1, const char *s2, int n, double x)
{strcpy(performers, s1);strcpy(label, s2);selections = n;playtime = x;
}
Cd::Cd(const Cd &d)
{strcpy(performers, d.performers);strcpy(label, d.label);selections = d.selections;playtime = d.playtime;
}
Cd::Cd()
{performers[0] = '\0';label[0] = '\0';selections = 0;playtime = 0;
}
void Cd::Report() const
{using std::cout;using std::endl;cout << "performers is " << performers << endl; cout << "     label is " << label << endl;cout << "selections is " << selections << endl;cout << "  playtime is " << playtime << endl;
}
Cd &Cd::operator=(const Cd &d)
{if (this == &d) return *this;strcpy(performers, d.performers);strcpy(label, d.label);selections = d.selections;playtime = d.playtime;return *this;
}Classic::Classic() { name[0] = '\0'; }
Classic::Classic(const char *s1, const char *s2, const char *s3, int n,double x): Cd(s2, s3, n, x)
{strcpy(name, s1);
}
Classic::Classic(const Classic &cl) : Cd(cl) { strcpy(name, cl.name); }
void Classic::Report() const
{using std::cout;using std::endl;Cd::Report();cout << "main album is " << name << endl;
}
Classic &Classic::operator=(const Classic &cl)
{if (this == &cl) return *this;Cd::operator=(cl);strcpy(name, cl.name);return *this;
}
//main.cpp
#include <iostream>
using namespace std;
#include "classic.h"
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(); }

第二题

在这里插入图片描述

//classic.h
#pragma once
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;Cd &operator=(const Cd &d);
};
class Classic : public Cd
{
private:char *name;public:Classic();Classic(const char *s1, const char *s2,const char*s3, int n, double x);Classic(const Classic & cl);~Classic();                                                                    void Report() const;Classic &operator=(const Classic &cl);
};
//classic.cpp
#include "classic.h"#include <cstring>
#include <iostream>Cd::Cd(const char *s1, const char *s2, int n, double x)
{performers = new char[strlen(s1) + 1];strcpy(performers, s1);label = new char[strlen(s2) + 1];strcpy(label, s2);selections = n;playtime = x;
}
Cd::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::Cd()
{performers = new char[1];performers[0] = '\0';label = new char[1];label[0] = '\0';selections = 0;playtime = 0; 
}
Cd::~Cd()
{delete[] performers;delete[] label;
}
void Cd::Report() const
{using std::cout;using std::endl;cout << "performers is " << performers << endl;cout << "     label is " << label << endl;cout << "selections is " << selections << endl;cout << "  playtime is " << playtime << endl;
}
Cd &Cd::operator=(const Cd &d)
{if (this == &d) return *this;delete[] performers;performers = new char[strlen(d.performers) + 1];strcpy(performers, d.performers);delete[] label;label = new char[strlen(d.label) + 1];strcpy(label, d.label);selections = d.selections;playtime = d.playtime;return *this;
}
Classic::Classic()                                                                 
{name = new char[1];name[0] = '\0';
}
Classic::Classic(const char *s1, const char *s2, const char *s3, int n,double x): Cd(s2, s3, n, x)
{name = new char[strlen(s1) + 1];strcpy(name, s1);
}
Classic::Classic(const Classic &cl) : Cd(cl)
{name = new char[strlen(cl.name) + 1];strcpy(name, cl.name);
}
Classic::~Classic(){delete [] name;
}
void Classic::Report() const
{using std::cout;using std::endl;Cd::Report();cout << "main album is " << name << endl;
}
Classic &Classic::operator=(const Classic &cl)
{if (this == &cl) return *this;Cd::operator=(cl);delete [] name;name = new char[strlen(cl.name)+1];strcpy(name, cl.name);return *this;
}

测试的主函数与第一题完全相同

第三题

在这里插入图片描述
这是原本DMA的定义
在这里插入图片描述
在这里插入图片描述
下面是他们的实现
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
现在要把他们抽象为ABC继承

//dma.h
#pragma once
class DMA
{
private:char* label;int rating;public:DMA(const char* l = "null", int r = 0);DMA(const DMA& d);virtual ~DMA() { delete[] label; };DMA& operator=(const DMA& d);virtual void View() const = 0;
};
class baseDMA : public DMA
{
public:baseDMA(const char* l = "null", int r = 0):DMA(l,r){}void View() const { DMA::View(); }
};
class lacksDMA : public DMA
{
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 DMA& rs);void View() const;
};
class hasDMA : public DMA
{
private:char* style;public:hasDMA(const char* s = "none", const char* l = "null", int r = 0);hasDMA(const char* s, const DMA& rs);hasDMA(const hasDMA& hs);~hasDMA(){delete [] style;}hasDMA& operator=(const hasDMA& rs);void View() const;
};
//dma.cpp
#include "dma.h"#include <cstring>
#include <iostream>                                                                
using std::cout;
using std::endl;DMA::DMA(const char* l, int r) : rating(r)
{label = new char[strlen(l) + 1];strcpy(label, l);
}
DMA::DMA(const DMA& d) : rating(d.rating)
{label = new char[strlen(d.label) + 1];strcpy(label, d.label);
}
DMA& DMA::operator=(const DMA& d)
{if (this == &d) return *this;label = new char[strlen(d.label) + 1];strcpy(label, d.label);rating = d.rating;return *this;
}
void DMA::View() const
{cout << "Label: " << label << endl;                                            cout << "Rating: " << rating << endl;
}lacksDMA::lacksDMA(const char* c, const char* l, int r) : DMA(l, r)
{strncpy(color, c, COL_LEN - 1);color[COL_LEN - 1] = '\0';
}
lacksDMA::lacksDMA(const char* c, const DMA& rs) : DMA(rs)
{strncpy(color, c, COL_LEN - 1);color[COL_LEN - 1] = '\0';
}
void lacksDMA::View() const
{DMA::View();cout << "Color: " << color << endl;
}
hasDMA::hasDMA(const char* s, const char* l, int r) : DMA(l, r)
{style = new char[strlen(s) + 1];strcpy(style, s);
}
hasDMA::hasDMA(const char* s, const DMA& rs) : DMA(rs)
{style = new char[strlen(s) + 1];strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA& hs) : DMA(hs)
{style = new char[strlen(hs.style) + 1];strcpy(style, hs.style);
}
hasDMA& hasDMA::operator=(const hasDMA& hs)
{if (this == &hs) return *this;DMA::operator=(hs);delete[] style;style = new char[strlen(hs.style) + 1];strcpy(style, hs.style);return *this;
}
void hasDMA::View() const
{DMA::View();cout << "Style: " << style << endl;
}
//main.cpp
#include <iostream>
#include <string>#include "dma.h"
int main()
{using namespace std;const int LIMITS = 4;DMA* dmas[LIMITS];for (int i = 0; i < LIMITS; i++) {string label;int rating;char kind;cout << "请输入标签名: ";cin >> label;cout << "请输入等级: ";cin >> rating;cout << "请选择类型 1是base 2是 lacks 3是 has: ";while (cin >> kind && (kind != '1' && kind != '2' && kind != '3'))cout << "请输入1或2或3: ";if (kind == '1') {dmas[i] = new baseDMA(label.data(),rating);  // 由于string不能直接传给const// char*,需要用data方法取出数据} else if (kind == '2') {string color;cout << "请输入颜色: ";cin >> color;dmas[i] = new lacksDMA(color.data(), label.data(), rating);} else {string style;cout << "请输入风格";cin >> style;dmas[i] = new hasDMA(style.data(), label.data(), rating);}}for (int i = 0; i < LIMITS; i++) {dmas[i]->View();}for (int i = 0; i < LIMITS; i++) {delete dmas[i];}return 0;
}

第四题

在这里插入图片描述
在这里插入图片描述

//port.h
#pragma once
#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, 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.cpp
#include "port.h"#include <cstring>
#include <iostream>
Port::Port(const char *br, const char *st, int b) : bottles(b)
{brand = new char[strlen(br) + 1];strcpy(brand, br);strncpy(style, st, 19);
}
Port::Port(const Port &p)
{brand = new char[strlen(p.brand) + 1];strcpy(brand, p.brand);strncpy(style, p.style, 19);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, 19);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
{cout << "Brand: " << brand << endl;cout << "Kind: " << style << endl;cout << "Bottles: " << bottles << endl;
}
ostream &operator<<(ostream &os, const Port &p)
{os << p.brand << ", " << p.style << ", " << p.bottles;return os;
}
VintagePort::VintagePort()
{nickname = new char[1];nickname[0] = '\0';year = 0;
}
VintagePort::VintagePort(const char *br, int b, const char *nn, int y): Port(br, "none", 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();cout << "nickname: " << nickname << endl;cout << "year: " << year << endl;
}
ostream &operator<<(ostream &os, const VintagePort &vp)
{os << (const Port &)vp;os << ", " << vp.nickname << ", " << vp.year;return os;
}
http://www.dtcms.com/a/406818.html

相关文章:

  • Custom SRP 11 - Post Processing
  • 【Linux】进程替换
  • wordpress调用目录网址seo查询
  • 【C++】模版专题
  • K8s实践中的重点知识
  • 云栖2025 | 人工智能平台 PAI 年度发布
  • 【文献管理工具】学术研究的智能助手—Zotero 文献管理工具详细图文安装教程
  • H5平台网站建设wordpress 会话已过期
  • 建论坛网站印度人通过什么网站做国际贸易
  • UniApp ConnectSocket连接websocket
  • 正点原子【第四期】Linux之驱动开发学习笔记-5.1 设备树下的LED驱动实验
  • uniapp中全局封装一个跨组件的复制粘贴方法
  • 新奇特:神经网络烘焙坊(上),权重矩阵的甜蜜配方之谜
  • 分布式调度问题:定时任务
  • labelimg(目标检测标注工具)的安装、使用教程和问题解决
  • 【MFC】项目结构梳理
  • 中小企业声音克隆技术落地实践:痛点分析与轻量化解决方案建议
  • High precision single-photon object detection via deep neural networks,OE2024
  • 网站编程入门php做外贸网站好吗
  • 网站制作排名php自己写框架做网站
  • VMware+RockyLinux+ikuai+docker+cri-docker+k8s 自用 实践笔记(二)
  • Lambda
  • html网站开发代码公司网页设计实例教程
  • MySQL异步I/O性能优化全解析
  • SQL 执行计划解析:从 EXPLAIN 到性能优化的完整指南
  • jupyter notebook继续学习
  • 力扣2381. 字母移位 II
  • 平和县建设局网站安徽经工建设集团网站
  • Vue 配置代理
  • CatCTF2022 web wp