C++ Primer Plus第十三章编程练习答案
1,以下面的类声明为基础:
// base class
class Cd{ // represents a CD diskprivate:
char performers[50] ;
char label[20];
int selections;// number of selectionsdouble playtime; // playing time in minutes
public:
Cd(char * sl,char * s2,int n,double x!;Cd(const cd & d!;
Cd{};
-Cd(};
void Report() const; // reports all CD dataCd & operator=(const cd & d);
派生出一个Classic 类,并添加一组 har 成员,用于存储指出CD 中主要作品的字符串。修改上述声明,使基类的所有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:
#include <iostream>
using namespace std;
#include "classic.h"// which will contain #include cd.hvoid Bravo(const Cd & disk);int main()
Cd cl("Beatles","Capitol",14,35,5);Classic c2 = Classic("piano Sonata in B flat,Fantasia in C""Alfred Brendel","Philips",2,57.17);
Cd *pcd = &cl;
cout <s "Using object directly:\n";cl.Report();
// use Cd method
c2.Report();
//use Classic method
cout << "Using type cd * pointer to objects:\n";pcd->Report();// use cd method for cd objectpcd = &c2;
pcd->Report(); // use Classic method for classic object
cout << "Calling a function with a Cd reference argument:\n";Bravo(c1);Bravo(c2];
cout cc "Testing assignment: ";Classic copyi
copy= c2;
copy.Report()
return 0;
void Bravo(const Cd & disk)
disk.Report();
#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:\n";Classic copy;copy = c2;copy.Report();return 0;
}void Bravo(const Cd &disk)
{disk.Report();
}
#ifndef CD_H_
#define CD_H_class Cd
{
private:char performers[50];char labels[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);
};#endif
#include <iostream>
#include "cd.h"
#include <cstring>Cd::Cd(const char *s1, const char *s2, int n, double x) //用户定义构造函数;
{std::strncpy(performers, s1, 50);performers[49] = '\0';std::strncpy(labels, s2, 20);labels[19] = '\0';selections = n;playtime = x;
}Cd::Cd(const Cd &d) //用户定义构造函数;
{std::strncpy(performers, d.performers, 50);performers[49] = '\0';std::strncpy(labels, d.labels, 50);labels[49] = '\0';selections = d.selections;playtime = d.playtime;
}Cd::Cd() //用户定义默认构造函数;
{performers[0] = '\0';labels[0] = '\0';selections = 0;playtime = 0.0;
}Cd::~Cd()
{
}void Cd::Report() const
{std::cout << "Performers: " << performers << std::endl;std::cout << "Label: " << labels << std::endl;std::cout << "Selections: " << selections << std::endl;std::cout << "Playtime: " << playtime << std::endl;
}Cd &Cd::operator=(const Cd &d)
{if (this == &d){return *this;}std::strncpy(performers, d.performers, 50);performers[49] = '\0';std::strncpy(labels, d.labels, 20);labels[19] = '\0';selections = d.selections;playtime = d.playtime;return *this;
}
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include "cd.h"class Classic : public Cd
{
private:char cdstr[50];public:Classic() : Cd() { cdstr[0] = '\0'; }Classic(const char *s, const char *s1, const char *s2, int n, double x);Classic(const char *s, const Cd &d);~Classic();virtual void Report() const;Classic &operator=(const Classic &cs);
};#endif
#include <iostream>
#include <cstring>
#include "classic.h"Classic::Classic(const char *s, const char *s1, const char *s2, int n, double x) : Cd(s1, s2, n, x)
{std::strncpy(cdstr, s, 50);cdstr[49] = '\0';
}Classic::Classic(const char *s, const Cd &d) : Cd(d)
{std::strncpy(cdstr, s, 50);cdstr[49] = '\0';
}Classic::~Classic()
{
}void Classic::Report() const
{Cd::Report();std::cout << "Major article in the CD is: " << cdstr << std::endl;std::cout.put('\n');
}Classic &Classic::operator=(const Classic &cs)
{if (this == &cs){return *this;}Cd::operator=(cs);std::strncpy(cdstr, cs.cdstr, 50);cdstr[49] = '\0';return *this;
}
2.完成练习 1,但让两个类使用动态内存分配而不是长度固定的数组来记录字符串。
#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:\n";Classic copy;copy = c2;copy.Report();return 0;
}void Bravo(const Cd &disk)
{disk.Report();
}
#ifndef CD_H_
#define CD_H_class Cd //原有的数组修改为指针便于动态分配内存;
{
private:char *performers;char *labels;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);
};#endif
#include <iostream>
#include "cd.h"
#include <cstring>Cd::Cd(const char *s1, const char *s2, int n, double x) //用户定义构造函数;
{performers = new char[std::strlen(s1) + 1];std::strcpy(performers, s1);labels = new char[std::strlen(s2) + 1];std::strcpy(labels, s2);selections = n;playtime = x;
}Cd::Cd(const Cd &d) //用户定义构造函数;
{performers = new char[std::strlen(d.performers) + 1];std::strcpy(performers, d.performers);labels = new char[std::strlen(d.labels) + 1];std::strcpy(labels, d.labels);selections = d.selections;playtime = d.playtime;
}Cd::Cd() //用户定义默认构造函数;
{performers = new char[1];performers[0] = '\0';labels = new char[1];labels[0] = '\0';selections = 0;playtime = 0.0;
}Cd::~Cd()
{delete[] performers;delete[] labels;
}void Cd::Report() const
{std::cout << "Performers: " << performers << std::endl;std::cout << "Label: " << labels << std::endl;std::cout << "Selections: " << selections << std::endl;std::cout << "Playtime: " << playtime << std::endl;
}Cd &Cd::operator=(const Cd &d) //重载赋值运算符;
{if (this == &d){return *this;}delete[] performers; //释放原有已分配的内存空间;delete[] labels; //同上;performers = new char[std::strlen(d.performers) + 1];std::strcpy(performers, d.performers);labels = new char[std::strlen(d.labels) + 1];std::strcpy(labels, d.labels);selections = d.selections;playtime = d.playtime;return *this;
}
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include "cd.h"class Classic : public Cd //公有继承派生类;
{
private:char *cdstr;public:Classic();Classic(const char *s1, const char *s2, const char *s3, int n, double x);Classic(const char *s, const Cd &d);~Classic();virtual void Report() const;Classic &operator=(const Classic &cs);
};#endif
#include <iostream>
#include <cstring>
#include "classic.h"Classic::Classic() : Cd()
{cdstr = new char[1];cdstr[0] = '\0';
}Classic::Classic(const char *s, const char *s1, const char *s2, int n, double x) : Cd(s1, s2, n, x) //基类构造函数的成员列表初始化;
{cdstr = new char[std::strlen(s) + 1];std::strcpy(cdstr, s);
}Classic::Classic(const char *s, const Cd &d) : Cd(d) //基类构造函数的成员列表初始化;
{cdstr = new char[std::strlen(s) + 1];std::strcpy(cdstr, s);
}Classic::~Classic()
{delete[] cdstr;
}void Classic::Report() const
{Cd::Report();std::cout << "Major article in the CD is: " << cdstr << std::endl;std::cout.put('\n');
}Classic &Classic::operator=(const Classic &cs)
{if (this == &cs){return *this;}delete[] cdstr;Cd::operator=(cs);cdstr = new char[std::strlen(cs.cdstr) + 1];std::strcpy(cdstr, cs.cdstr);return *this;
}
3.修改baseDMA-lacksDMA-hasDMA类层次,让三个类都从一个ABC派生而来,然后使用与程序清单13.10 相似的程序对结果进行测试。也就是说,它应使用ABC 指针数组并让用户决定要创建的对象类型。在类定义中添加virtual View()方法以处理数据显示。
#include <iostream>
#include "dma.h"
const int LEN = 3;int main()
{using std::cin;using std::cout;using std::endl;DMA *temp[LEN];char label[50];int rating;char color[40];char style[50];char kind;cout << "Here are the process for creating 3 objects" << endl; //参考书上程序清单13.10让用户进行选择;for (int i = 0; i < LEN; i++){cout << "Enter 1 for baseDMA, 2 for lacksDMA or 3 for hasDMA: ";while (cin >> kind && (kind != '1' && kind != '2' && kind != '3')){cin.clear();while (cin.get() != '\n')continue;cout << "Please enter 1, 2 or 3: ";}if (kind == '1'){cout << "Please enter the label: ";cin >> label; //推荐输入书中样例Portabelly;cout << "Please enter the rating: ";cin >> rating; //推荐输入书中样例8;temp[i] = new baseDMA(label, rating, "baseDMA");}else if (kind == '2'){cout << "Please enter the color: ";cin >> color; //推荐输入书中样例red;temp[i] = new lacksDMA(color, "lacksDMA");}else{cout << "Please enter the style: ";cin >> style; //推荐输入书中样例Mercator;temp[i] = new hasDMA(style, "hasDMA");}}cout << "\nThe results after creating 3 objects" << endl;for (int i = 0; i < LEN; i++){temp[i]->View();}for (int i = 0; i < LEN; i++){delete temp[i];}cout << "Done.\n";return 0;
}
#ifndef DMA_H_
#define DMA_H_
#include <iostream>class DMA
{
private:char *classname; //基类成员设为一个char指针方便分配内存空间保存派生类的类名;protected:const char *show_classname() const { return classname; } //抽象基类DMA的View方法不进行定义因此使用protected让派生类可以访问基类数据;public:DMA(const char *cn = "null"); //声明基类DMA的默认构造函数;DMA(const DMA &rs); //声明基类DMA的复制构造函数;DMA &operator=(const DMA &rs); //声明为基类DMA重载赋值运算符;virtual ~DMA(); //声明基类DMA的虚析构函数;virtual void View() const = 0; //声明基类DMA的纯虚函数;
};class baseDMA : public DMA
{
private:char *label;int rating;public:baseDMA(const char *l = "null", int r = 0, const char *cn = "null"); //声明派生类baseDMA的默认构造函数;baseDMA(const char *l, int r, const DMA &rs); //声明派生类baseDMA的用户定义构造函数;baseDMA(const baseDMA &rs); //声明派生类baseDMA的复制构造函数;~baseDMA(); //声明派生类baseDMA的析构函数;baseDMA &operator=(const baseDMA &rs); //声明为派生类baseDMA重载赋值运算符;virtual void View() const; //声明为派生类baseDMA定义基类虚函数;
};class lacksDMA : public DMA
{
private:enum { COL_LEN = 40 };char color[COL_LEN];public:lacksDMA(const char *c = "blank", const char *cn = "null"); //声明派生类lacksDMA的默认构造函数;lacksDMA(const char *c, const DMA &rs); //声明派生类lacksDMA的用户定义构造函数;lacksDMA(const lacksDMA &rs); //声明派生类lacksDMA的复制构造函数;~lacksDMA(); //声明派生类lacksDMA的析构函数;lacksDMA &operator=(const lacksDMA &rs); //声明为派生类lacksDMA重载赋值运算符;virtual void View() const; //声明为派生类lacksDMA定义基类虚函数;
};class hasDMA : public DMA
{
private:char *style;public:hasDMA(const char *s = "none", const char *cn = "null"); //声明派生类hasDMA的默认构造函数;hasDMA(const char *s, const DMA &rs); //声明派生类hasDMA的用户定义构造函数;hasDMA(const hasDMA &rs); //声明派生类hasDMA的复制构造函数;~hasDMA(); //声明派生类hasDMA的析构函数;hasDMA &operator=(const hasDMA &rs); //声明为派生类hasDMA重载赋值运算符;virtual void View() const; //声明为派生类hasDMA定义基类虚函数;
};#endif
#include "dma.h"
#include <cstring>DMA::DMA(const char *cn) //定义基类DMA的默认构造函数;
{classname = new char[std::strlen(cn) + 1];std::strcpy(classname, cn);
}DMA::DMA(const DMA &rs) //定义基类DMA的复制构造函数;
{classname = new char[std::strlen(rs.classname) + 1];std::strcpy(classname, rs.classname);
}DMA &DMA::operator=(const DMA &rs) //为基类DMA重载赋值运算符;
{if (this == &rs){return *this;}delete[] classname; //先释放后分配;classname = new char[std::strlen(rs.classname) + 1];std::strcpy(classname, rs.classname);return *this;
}DMA::~DMA() //定义基类DMA的析构函数;
{delete[] classname;
}baseDMA::baseDMA(const char *l, int r, const char *cn) : DMA(cn) //定义派生类baseDMA的默认构造函数;
{label = new char[std::strlen(l) + 1];std::strcpy(label, l);rating = r;
}baseDMA::baseDMA(const char *l, int r, const DMA &rs) : DMA(rs) //定义派生类baseDMA的用户定义构造函数;
{label = new char[std::strlen(l) + 1];std::strcpy(label, l);rating = r;
}baseDMA::baseDMA(const baseDMA &rs) : DMA(rs) //定义派生类baseDMA的复制构造函数;
{label = new char[std::strlen(rs.label) + 1];std::strcpy(label, rs.label);rating = rs.rating;
}baseDMA::~baseDMA() //定义派生类baseDMA的析构函数;
{delete[] label;
}baseDMA &baseDMA::operator=(const baseDMA &rs) //为派生类baseDMA重载赋值运算符;
{if (this == &rs){return *this;}delete[] label; //先释放后分配;DMA::operator=(rs); //调用基类DMA的赋值运算符方法初始化派生类baseDMA的基类数据成员;label = new char[std::strlen(rs.label) + 1];std::strcpy(label, rs.label);rating = rs.rating;return *this;
}void baseDMA::View() const //为派生类baseDMA定义基类虚函数;
{std::cout << "Classname: " << show_classname() << std::endl; //调用基类protected中的show_classname方法显示基类数据成员;std::cout << "Label: " << label << std::endl;std::cout << "Rating: " << rating << std::endl;
}lacksDMA::lacksDMA(const char *c, const char *cn) : DMA(cn) //定义派生类lacksDMA的默认构造函数;
{std::strncpy(color, c, COL_LEN - 1);color[COL_LEN - 1] = '\0';
}lacksDMA::lacksDMA(const char *c, const DMA &rs) : DMA(rs) //定义派生类lacksDMA的用户定义构造函数;
{std::strncpy(color, c, COL_LEN - 1);color[COL_LEN - 1] = '\0';
}lacksDMA::lacksDMA(const lacksDMA &rs) : DMA(rs) //定义派生类lacksDMA的复制构造函数;
{std::strncpy(color, rs.color, COL_LEN - 1);color[COL_LEN - 1] = '\0';
}lacksDMA::~lacksDMA()
{
}lacksDMA &lacksDMA::operator=(const lacksDMA &rs) //为派生类lacksDMA重载赋值运算符;
{if (this == &rs){return *this;}DMA::operator=(rs); //调用基类DMA的赋值运算符方法初始化派生类lacksDMA的基类数据成员;std::strncpy(color, rs.color, COL_LEN - 1);color[COL_LEN - 1] = '\0';return *this;
}void lacksDMA::View() const //为派生类lacksDMA定义基类虚函数;
{std::cout << "Classname: " << show_classname() << std::endl; //调用基类protected中的show_classname方法显示基类数据成员;std::cout << "Color: " << color << std::endl;
}hasDMA::hasDMA(const char *s, const char *cn) : DMA(cn) //定义派生类hasDMA的默认构造函数;
{style = new char[std::strlen(s) + 1];std::strcpy(style, s);
}hasDMA::hasDMA(const char *s, const DMA &rs) : DMA(rs) //定义派生类hasDMA的用户定义构造函数;
{style = new char[std::strlen(s) + 1];std::strcpy(style, s);
}hasDMA::hasDMA(const hasDMA &rs) : DMA(rs) //定义派生类hasDMA的复制构造函数;
{style = new char[std::strlen(rs.style) + 1];std::strcpy(style, rs.style);
}hasDMA::~hasDMA() //定义派生类hasDMA的析构函数;
{delete[] style;
}hasDMA &hasDMA::operator=(const hasDMA &rs) //为派生类hasDMA重载赋值运算符;
{if (this == &rs){return *this;}delete[] style; //先释放后分配;DMA::operator=(rs); //调用基类DMA的赋值运算符方法初始化派生类hasDMA的基类数据成员;style = new char[std::strlen(rs.style) + 1];std::strcpy(style, rs.style);return *this;
}void hasDMA::View() const //为派生类hasDMA定义基类虚函数;
{std::cout << "Classname: " << show_classname() << std::endl; //调用基类protected中的show_classname方法显示基类数据成员;std::cout << "Style: " << style << std::endl;
}
4.BenevolentOrderof Programmers用来维护瓶装葡萄酒箱。为描述它BPPortmaster设置了一个Port类,其声明如下:
#include'<iostream>
using- namespace std;
class Port
private:
char *brand;-charkstyle(201//ieawnyrubyintagesiintbottles;-public.飞+A
Port(const char *br"none",~const char:*st "none, int b 0)
Port(const Port 5 p);
copy constructor
virtual.-Port(- delete ll brand;
Port &~operator=(const.Port 5 p):.Port & operator+=(int b) ,
Port 5 operator-=(int bj;available
// addsb-tobottles// subtracts b from bottles, if
--
int'BottleCount() const-f return bottles;virtual void showl const:
friend ostream & operatores(ostream & os,const Port & p);
show()方法按下面的格式显示信息
Brand:GalFo
Kind: tawnyBottles:20
operator<<()函数按下面的格式显示信息(末尾没有换行符):Gal1o,tawnyr20-
PortMaster 完成了-Port类的方法定义后派生了VintagePort类然后被解职-因为不小心将一瓶45度Cockbum泼到了正在准备烤肉调料的人身上VintagePort类如下所示:
class VintagePort :publicPort// style-necessarily=vintage
-ai
private:
ehar.*.nickname;
int year!
public:
VintagePortl:
// 1.e,r ."TheNoble".or "oldvelvet",etc ..vintageyear
VintagePort(const char* ,br;int b,const char *inn,int y);;+yintagePortqconstVintagePort-&vp)------_.-
VintagePort(E-delete l] nicknamerVintageport 5 operator=(const VintagePort vpF;void Show(yconst:....-..
friend ostream operator<<(ostream os,const Vintageport & vp);
您被指定负责完成 VintagePorta
a.第一个任务是重新创建Port 方法定义,因为前任被开除时销毁了方法定义
b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
c.第三个任务是解释为何没有operator-)和operator<<()声明为虚的。
d.第四个任务是提供VintagePort 中各个方法的定义。
#include "port.h"
#include "vintageport.h"int main()
{Port wine1("Gallo", "tawny", 20); //构造Port基类对象;VintagePort wine2("Lafei", 10, "strong wine", 1876); //构造VintagePort基类对象;VintagePort wine3("Merlot", 50, "middle wine", 1976); //构造VintagePort基类对象;cout << "Here is the Port object:\n";wine1.Show(); //调用基类Show方法;cout << wine1 << endl; //调用基类重载输出流运算符;cout << "\nHere are the VintagePort objects:\n";wine2.Show(); //调用派生类Show方法;cout << wine2 << endl; //调用派生类重载输出流运算符;wine3.Show(); //调用派生类Show方法;cout << wine3 << endl; //调用派生类重载输出流运算符;cout << "\nGallo add 20 bottles:\n";wine1 += 20; //调用基类重载+=运算符;wine1.Show();cout << "\nLafei add 10 bottles:\n";wine2 += 10; //调用派生类重载+=运算符;wine2.Show();cout << "\nMerlot minus 10 bottles:\n";wine3 -= 10; //调用派生类重载-=运算符;wine3.Show();VintagePort wine4(wine2); //调用派生类复制构造函数;cout << "\nResult of VintagePort copy:\n";wine4.Show();VintagePort wine5;wine5 = wine3; //调用派生类重载赋值运算符;cout << "\nResult of VintagePort assignment:\n";wine5.Show();return 0;
}
#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);
};#endif
#include "port.h"
#include <cstring>Port::Port(const char *br, const char *st, int b)
{brand = new char[std::strlen(br) + 1]; //new分配内存;std::strcpy(brand, br);std::strncpy(style, st, 20);style[19] = '\0'; //保证字符串是有效的;bottles = b;
}Port::Port(const Port &p)
{brand = new char[std::strlen(p.brand) + 1];std::strcpy(brand, p.brand);std::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[std::strlen(p.brand) + 1];std::strcpy(brand, p.brand);std::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
{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;
}
#ifndef VINTAGEPORT_H_
#define VINTAGEPORT_H_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);
};#endif
#include <cstring>
#include "port.h"
#include "vintageport.h"VintagePort::VintagePort() : Port("none", "vintage", 0) //成员列表初始化派生类的基类对象数据;
{nickname = new char[1];nickname[0] = '\0';year = 0;
}VintagePort::VintagePort(const char *br, int b, const char *nn, int y) : Port(br, "vintage", b) //成员列表初始化派生类的基类对象数据;
{nickname = new char[std::strlen(nn) + 1];std::strcpy(nickname, nn);year = y;
}VintagePort::VintagePort(const VintagePort &vp) : Port(vp) //调用基类复制构造函数初始化派生类的基类对象数据;
{nickname = new char[std::strlen(vp.nickname) + 1];std::strcpy(nickname, vp.nickname);year = vp.year;
}VintagePort &VintagePort::operator=(const VintagePort &vp)
{if (this == &vp){return *this;}delete[] nickname;Port::operator=(vp); //调用基类赋值运算符修改派生类的基类数据成员;nickname = new char[std::strlen(vp.nickname) + 1];std::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; //强制类型转换调用Port基类的重载输出流运算符方法;os << ", " << vp.nickname << ", " << vp.year;return os;
}
相关文章:
C++ Primer Plus第十三章编程练习答案
1,以下面的类声明为基础: // base class class Cd{ // represents a CD disk private: char performers[50] ; char label[20]; int selections;// number of selections double playtime; // playing time in minutes public: Cd(char * sl,char * s2,int n,double…...

Elasticsearch:wildcard - 通配符搜索
Elasticsearch 是一个分布式、免费和开放的搜索和分析引擎,适用于所有类型的数据,例如文本、数字、地理空间、结构化和非结构化数据。 它基于 Apache Lucene 构建,Apache Lucene 是一个全文搜索引擎,可用于各种编程语言。 由于其速…...
配置类安全问题学习小结
目录 一、前言 二、漏洞类型 目录 一、前言 二、漏洞类型 2.1 Strict Transport Security Not Enforced 2.2 SSL Certificate Cannot Be Trusted 2.3 SSL Anonymous Cipher Suites Supported 2.4 "Referrer Policy”Security 头值不安全 2.5 “Content-Security-…...
IMX6ULL移植篇-uboot源码目录
一. uboot 源码分析前提 由于 uboot 会使用到一些经过编译才会生成的文件,因此,我们在分析 uboot的时候,需要先编译一下 uboot 源码工程。 这里所用的开发板是 nand-flash版本。 二. uboot 源码目录及编译 1. uboot 源码目录 uboot源码目…...

SAP MM学习笔记27- 购买依赖(采购申请)
前面已经努力的学习了 购买发注,入库,请求书照合 等功能,还是蛮多内容的哈。 剩下的功能,比如 右侧的 所要量决定,供给元决定,仕入先选择 还没学。 从这章开始,要开始学习它们了。 这一章先来…...
C++零碎记录(八)
14. 运算符重载简介 14.1 运算符重载简介 ① 运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。 ② 对于内置的数据类型的表达式的运算符是不可能改变的。 14.2 加号运算符重载 ① 加号运算符作用&#x…...

基于matlab的扩频解扩误码率完整程序分享
clc; clear; close all; warning off; addpath(genpath(pwd)); r5; N2^r-1;%周期31 aones(1,r); mzeros(1,N); for i1:(2^r-1) temp mod((a(5)a(2)),2); for jr:-1:2 a(j)a(j-1); end a(1)temp; m(i)a(r); end mm*2-1;%双极性码 %产生随…...
算法:轮转数组---循环取模运算
1、题目: 给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 2、分析特点: 轮转 > 取模运算 我们可以使用额外的数组来将每个元素放至正确的位置。用 n 表示数组的长度,我们遍历原数组&a…...

Vue教程
官网vue快速上手 vue示例图 请点击下面工程名称,跳转到代码的仓库页面,将工程 下载下来 Demo Code 里有详细的注释 代码:LearnVue...

算法之双指针题型:
双指针例题小总结: 力扣27: 移除元素 力扣题目链接 双指针分为: 快慢双指针:同一个起点,同向出发 相向双指针:从两端出发,方向相反,终会相遇 经典的双指针(快慢双指…...
vue传递给后端时间格式问题
前端处理 首先前端使用moment.js进行处理 data.userEnrolDate moment(data.userEnrolDate).format(YYYY-MM-DD HH:mm:ss);后端处理 JsonFormat(timezone "GMT8", pattern "yyyy-MM-dd HH:mm:ss") DateTimeFormat(pattern "yyyy-MM-dd HH:mm:ss…...

php使用jwt作登录验证
1 在项目根目录下,安装jwt composer require firebase/php-jwt 2 在登录控制器中加入生成token的代码 use Firebase\JWT\JWT; use Firebase\JWT\Key; class Login extends Cross {/*** 显示资源列表** return \think\Response*/public function index(Request $r…...
【zlm】 PTS DTS
在音视频编码和传输中,PTS(Presentation Time Stamp)和DTS(Decoding Time Stamp)是两个关键的时间戳,用于确保音视频帧的顺序和同步。它们在多媒体处理中扮演重要的角色: PTS(Presen…...

【两周学会FPGA】从0到1学习紫光同创FPGA开发|盘古PGL22G开发板学习之DDR3 IP简单读写测试(六)
本原创教程由深圳市小眼睛科技有限公司创作,版权归本公司所有,如需转载,需授权并注明出处 适用于板卡型号: 紫光同创PGL22G开发平台(盘古22K) 一:盘古22K开发板(紫光同创PGL22G开发…...

第6章 内核模块符号导出实验(iTOP-RK3568开发板驱动开发指南 )
在上一小节中,给大家讲解了驱动模块传参实验,使用insmod命令加载驱动时可以进行参数的传递,但是每一个内核模块之间是相互独立的,那模块间的符号传递要怎样进行呢,让我们带着疑问来进行本章节的学习吧! 6.…...
Android12.0首次开机默认授予app运行时权限(去掉运行时授权弹窗)第二种方法
1.概述 在12.0的系统产品开发中,在6.0以后对于权限的申请,都需要动态申请,所以会在系统首次启动后,在app的首次运行时,会弹出授权窗口,会让用户手动授予app运行时权限,在由于系统产品开发需要要求默认授予app运行时权限,不需要用户默认授予运行时弹窗,所以需要在首次开…...

conda和Python的虚拟环境如何结合使用,以及二者之间到底有什么区别?
问题描述 今天在复现streamlit的代码时(参考Streamlit 讲解专栏(一):安装以及初步应用),根据这篇博文指导,要先用以下指令创建一个虚拟环境: # 创建虚拟环境(使用venv&a…...

宇凡微YE09合封芯片,集成高性能32位mcu和2.4G芯片
合封芯片是指将主控芯片和外部器件合并封装的芯片,能大幅降低开发成本、采购成本、减少pcb面积等等。宇凡微YE09合封芯片,将技术领域推向新的高度。这款高度创新性的芯片融合了32位MCU和2.4G芯片,为各种应用场景提供卓越的功能和性能。 32位M…...

使用perf_analyzer和model-analyzer测试tritonserver的模型性能超详细完整版
导读 当我们在使用tritonserver部署模型之后,通常需要测试一下模型的服务QPS的能力,也就是1s我们的模型能处理多少的请求,也被称为吞吐量。 测试tritonserver模型服务的QPS通常有两种方法,一种是使用perf_analyzer 来测试&#…...

docker 部署springboot(成功、截图)
1.新建sringboot工程并打包 2.编写Dockerfile文件 # 基础镜像使用java FROM openjdk:8 # 作者 MAINTAINER feng # VOLUME 指定了临时文件目录为/tmp。 # 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp VOLUME /tmp # 将jar包添加…...

Linux应用开发之网络套接字编程(实例篇)
服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

VB.net复制Ntag213卡写入UID
本示例使用的发卡器:https://item.taobao.com/item.htm?ftt&id615391857885 一、读取旧Ntag卡的UID和数据 Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click轻松读卡技术支持:网站:Dim i, j As IntegerDim cardidhex, …...
linux 错误码总结
1,错误码的概念与作用 在Linux系统中,错误码是系统调用或库函数在执行失败时返回的特定数值,用于指示具体的错误类型。这些错误码通过全局变量errno来存储和传递,errno由操作系统维护,保存最近一次发生的错误信息。值得注意的是,errno的值在每次系统调用或函数调用失败时…...

C++ Visual Studio 2017厂商给的源码没有.sln文件 易兆微芯片下载工具加开机动画下载。
1.先用Visual Studio 2017打开Yichip YC31xx loader.vcxproj,再用Visual Studio 2022打开。再保侟就有.sln文件了。 易兆微芯片下载工具加开机动画下载 ExtraDownloadFile1Info.\logo.bin|0|0|10D2000|0 MFC应用兼容CMD 在BOOL CYichipYC31xxloaderDlg::OnIni…...

【开发技术】.Net使用FFmpeg视频特定帧上绘制内容
目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法,当前调用一个医疗行业的AI识别算法后返回…...

OPenCV CUDA模块图像处理-----对图像执行 均值漂移滤波(Mean Shift Filtering)函数meanShiftFiltering()
操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C11 算法描述 在 GPU 上对图像执行 均值漂移滤波(Mean Shift Filtering),用于图像分割或平滑处理。 该函数将输入图像中的…...
Redis的发布订阅模式与专业的 MQ(如 Kafka, RabbitMQ)相比,优缺点是什么?适用于哪些场景?
Redis 的发布订阅(Pub/Sub)模式与专业的 MQ(Message Queue)如 Kafka、RabbitMQ 进行比较,核心的权衡点在于:简单与速度 vs. 可靠与功能。 下面我们详细展开对比。 Redis Pub/Sub 的核心特点 它是一个发后…...

人工智能(大型语言模型 LLMs)对不同学科的影响以及由此产生的新学习方式
今天是关于AI如何在教学中增强学生的学习体验,我把重要信息标红了。人文学科的价值被低估了 ⬇️ 转型与必要性 人工智能正在深刻地改变教育,这并非炒作,而是已经发生的巨大变革。教育机构和教育者不能忽视它,试图简单地禁止学生使…...
【Android】Android 开发 ADB 常用指令
查看当前连接的设备 adb devices 连接设备 adb connect 设备IP 断开已连接的设备 adb disconnect 设备IP 安装应用 adb install 安装包的路径 卸载应用 adb uninstall 应用包名 查看已安装的应用包名 adb shell pm list packages 查看已安装的第三方应用包名 adb shell pm list…...
根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的----NTFS源代码分析--重要
根目录0xa0属性对应的Ntfs!_SCB中的FileObject是什么时候被建立的 第一部分: 0: kd> g Breakpoint 9 hit Ntfs!ReadIndexBuffer: f7173886 55 push ebp 0: kd> kc # 00 Ntfs!ReadIndexBuffer 01 Ntfs!FindFirstIndexEntry 02 Ntfs!NtfsUpda…...