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

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 x!;Cd(const cd & d!;
Cd{};
-Cd(};
void Report() const; // reports all CD data

Cd & 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&#xff0c;以下面的类声明为基础: // 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 是一个分布式、免费和开放的搜索和分析引擎&#xff0c;适用于所有类型的数据&#xff0c;例如文本、数字、地理空间、结构化和非结构化数据。 它基于 Apache Lucene 构建&#xff0c;Apache Lucene 是一个全文搜索引擎&#xff0c;可用于各种编程语言。 由于其速…...

配置类安全问题学习小结

目录 一、前言 二、漏洞类型 目录 一、前言 二、漏洞类型 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 会使用到一些经过编译才会生成的文件&#xff0c;因此&#xff0c;我们在分析 uboot的时候&#xff0c;需要先编译一下 uboot 源码工程。 这里所用的开发板是 nand-flash版本。 二. uboot 源码目录及编译 1. uboot 源码目录 uboot源码目…...

SAP MM学习笔记27- 购买依赖(采购申请)

前面已经努力的学习了 购买发注&#xff0c;入库&#xff0c;请求书照合 等功能&#xff0c;还是蛮多内容的哈。 剩下的功能&#xff0c;比如 右侧的 所要量决定&#xff0c;供给元决定&#xff0c;仕入先选择 还没学。 从这章开始&#xff0c;要开始学习它们了。 这一章先来…...

C++零碎记录(八)

14. 运算符重载简介 14.1 运算符重载简介 ① 运算符重载&#xff1a;对已有的运算符重新进行定义&#xff0c;赋予其另一种功能&#xff0c;以适应不同的数据类型。 ② 对于内置的数据类型的表达式的运算符是不可能改变的。 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、题目&#xff1a; 给定一个整数数组 nums&#xff0c;将数组中的元素向右轮转 k 个位置&#xff0c;其中 k 是非负数。 2、分析特点&#xff1a; 轮转 > 取模运算 我们可以使用额外的数组来将每个元素放至正确的位置。用 n 表示数组的长度&#xff0c;我们遍历原数组&a…...

Vue教程

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

算法之双指针题型:

双指针例题小总结&#xff1a; 力扣27&#xff1a; 移除元素 力扣题目链接 双指针分为&#xff1a; 快慢双指针&#xff1a;同一个起点&#xff0c;同向出发 相向双指针&#xff1a;从两端出发&#xff0c;方向相反&#xff0c;终会相遇 经典的双指针&#xff08;快慢双指…...

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 在项目根目录下&#xff0c;安装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

在音视频编码和传输中&#xff0c;PTS&#xff08;Presentation Time Stamp&#xff09;和DTS&#xff08;Decoding Time Stamp&#xff09;是两个关键的时间戳&#xff0c;用于确保音视频帧的顺序和同步。它们在多媒体处理中扮演重要的角色&#xff1a; PTS&#xff08;Presen…...

【两周学会FPGA】从0到1学习紫光同创FPGA开发|盘古PGL22G开发板学习之DDR3 IP简单读写测试(六)

本原创教程由深圳市小眼睛科技有限公司创作&#xff0c;版权归本公司所有&#xff0c;如需转载&#xff0c;需授权并注明出处 适用于板卡型号&#xff1a; 紫光同创PGL22G开发平台&#xff08;盘古22K&#xff09; 一&#xff1a;盘古22K开发板&#xff08;紫光同创PGL22G开发…...

第6章 内核模块符号导出实验(iTOP-RK3568开发板驱动开发指南 )

在上一小节中&#xff0c;给大家讲解了驱动模块传参实验&#xff0c;使用insmod命令加载驱动时可以进行参数的传递&#xff0c;但是每一个内核模块之间是相互独立的&#xff0c;那模块间的符号传递要怎样进行呢&#xff0c;让我们带着疑问来进行本章节的学习吧&#xff01; 6.…...

Android12.0首次开机默认授予app运行时权限(去掉运行时授权弹窗)第二种方法

1.概述 在12.0的系统产品开发中,在6.0以后对于权限的申请,都需要动态申请,所以会在系统首次启动后,在app的首次运行时,会弹出授权窗口,会让用户手动授予app运行时权限,在由于系统产品开发需要要求默认授予app运行时权限,不需要用户默认授予运行时弹窗,所以需要在首次开…...

conda和Python的虚拟环境如何结合使用,以及二者之间到底有什么区别?

问题描述 今天在复现streamlit的代码时&#xff08;参考Streamlit 讲解专栏&#xff08;一&#xff09;&#xff1a;安装以及初步应用&#xff09;&#xff0c;根据这篇博文指导&#xff0c;要先用以下指令创建一个虚拟环境&#xff1a; # 创建虚拟环境&#xff08;使用venv&a…...

宇凡微YE09合封芯片,集成高性能32位mcu和2.4G芯片

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

使用perf_analyzer和model-analyzer测试tritonserver的模型性能超详细完整版

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

docker 部署springboot(成功、截图)

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

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

C++_核心编程_多态案例二-制作饮品

#include <iostream> #include <string> using namespace std;/*制作饮品的大致流程为&#xff1a;煮水 - 冲泡 - 倒入杯中 - 加入辅料 利用多态技术实现本案例&#xff0c;提供抽象制作饮品基类&#xff0c;提供子类制作咖啡和茶叶*//*基类*/ class AbstractDr…...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

PHP和Node.js哪个更爽?

先说结论&#xff0c;rust完胜。 php&#xff1a;laravel&#xff0c;swoole&#xff0c;webman&#xff0c;最开始在苏宁的时候写了几年php&#xff0c;当时觉得php真的是世界上最好的语言&#xff0c;因为当初活在舒适圈里&#xff0c;不愿意跳出来&#xff0c;就好比当初活在…...

Python爬虫(二):爬虫完整流程

爬虫完整流程详解&#xff08;7大核心步骤实战技巧&#xff09; 一、爬虫完整工作流程 以下是爬虫开发的完整流程&#xff0c;我将结合具体技术点和实战经验展开说明&#xff1a; 1. 目标分析与前期准备 网站技术分析&#xff1a; 使用浏览器开发者工具&#xff08;F12&…...

DIY|Mac 搭建 ESP-IDF 开发环境及编译小智 AI

前一阵子在百度 AI 开发者大会上&#xff0c;看到基于小智 AI DIY 玩具的演示&#xff0c;感觉有点意思&#xff0c;想着自己也来试试。 如果只是想烧录现成的固件&#xff0c;乐鑫官方除了提供了 Windows 版本的 Flash 下载工具 之外&#xff0c;还提供了基于网页版的 ESP LA…...

鱼香ros docker配置镜像报错:https://registry-1.docker.io/v2/

使用鱼香ros一件安装docker时的https://registry-1.docker.io/v2/问题 一键安装指令 wget http://fishros.com/install -O fishros && . fishros出现问题&#xff1a;docker pull 失败 网络不同&#xff0c;需要使用镜像源 按照如下步骤操作 sudo vi /etc/docker/dae…...

UR 协作机器人「三剑客」:精密轻量担当(UR7e)、全能协作主力(UR12e)、重型任务专家(UR15)

UR协作机器人正以其卓越性能在现代制造业自动化中扮演重要角色。UR7e、UR12e和UR15通过创新技术和精准设计满足了不同行业的多样化需求。其中&#xff0c;UR15以其速度、精度及人工智能准备能力成为自动化领域的重要突破。UR7e和UR12e则在负载规格和市场定位上不断优化&#xf…...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现企业微信功能

1. 开发环境准备 ​​安装DevEco Studio 3.1​​&#xff1a; 从华为开发者官网下载最新版DevEco Studio安装HarmonyOS 5.0 SDK ​​项目配置​​&#xff1a; // module.json5 {"module": {"requestPermissions": [{"name": "ohos.permis…...

基于PHP的连锁酒店管理系统

有需要请加文章底部Q哦 可远程调试 基于PHP的连锁酒店管理系统 一 介绍 连锁酒店管理系统基于原生PHP开发&#xff0c;数据库mysql&#xff0c;前端bootstrap。系统角色分为用户和管理员。 技术栈 phpmysqlbootstrapphpstudyvscode 二 功能 用户 1 注册/登录/注销 2 个人中…...