《C++ Primer Plus》(第6版)第13章编程练习

news/2024/11/15 5:01:34/

《C++ Primer Plus》(第6版)第13章编程练习

  • 《C++ Primer Plus》(第6版)第13章编程练习
    • 1. Cd类
    • 2. 使用动态内存分配重做练习1
    • 3. baseDMA、lacksDMA、hasDMA类
    • 4. Port类和VintagePort类

《C++ Primer Plus》(第6版)第13章编程练习

1. Cd类

以下面的类声明为基础:

class Cd {                    //represents a CD disk
private:char performers[50];char label[20];int selections;          //number of selectionsdouble playtime;        //playing time in minute
public:Cd(char* s1, char* s2, int n, double x);Cd(const Cd& d);Cd();~Cd();void Report() const; //reports all CD dataCd& operator=(const Cd& d);
};

派生出一个Classic类,并添加一组char成员,用于存储指出CD中主要作品的字符串。修改上述声明,使基类的搜有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:

#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(); //use Cd methodc2.Report(); //use Classic methodcout << "Using type cd *pointer to objects:\n";pcd->Report(); //use Cd method for cd objectpcd = &c2;pcd->Report(); //use Classic method for classic objectcout << "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();
}

代码:

cd.h:

#ifndef CD_H_
#define CD_H_class Cd
{ // represents a CD disk
private:char performers[50];char label[20];int selections;  // number of selectionsdouble playtime; // playing time in minutepublic:Cd(char *s1, char *s2, int n, double x);Cd(const Cd &d);Cd();virtual ~Cd();virtual void Report() const; // reports all CD datavirtual Cd &operator=(const Cd &d);
};#endif

cd.cpp:

#include "cd.h"
#include <iostream>
#include <cstring>Cd::Cd(char *s1, char *s2, int n, double x)
{strncpy(performers, s1, 49);performers[49] = '\0';strncpy(label, s2, 19);label[19] = '\0';selections = n;playtime = x;
}
Cd::Cd(const Cd &d)
{strncpy(performers, d.performers, 49);performers[49] = '\0';strncpy(label, d.label, 19);label[19] = '\0';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
{using std::cout;using std::endl;cout << "Information:\n";cout << "Performers: " << performers << endl;cout << "Label: " << label << endl;cout << "Selections: " << selections << endl;cout << "Playtime: " << playtime << endl;
}
Cd &Cd::operator=(const Cd &d)
{if (this == &d)return *this;strncpy(performers, d.performers, 49);performers[49] = '\0';strncpy(label, d.label, 19);label[19] = '\0';selections = d.selections;playtime = d.playtime;return *this;
}

classic.h:

#ifndef CLASSIC_H_
#define CLASSIC_H_#include "cd.h"class Classic : public Cd
{
private:char majorWorks[50];public:Classic(char *m, char *s1, char *s2, int n, double x);Classic(char *m, const Cd &cd);Classic();virtual ~Classic();virtual void Report() const;virtual Classic &operator=(const Classic &c);
};#endif

classic.cpp:

#include "classic.h"
#include <iostream>
#include <cstring>Classic::Classic(char *m, char *s1, char *s2, int n, double x) : Cd(s1, s2, n, x)
{strncpy(majorWorks, m, 49);majorWorks[49] = '\0';
}
Classic::Classic(char *m, const Cd &cd) : Cd(cd)
{strncpy(majorWorks, m, 49);majorWorks[49] = '\0';
}
Classic::Classic() : Cd()
{majorWorks[0] = '\0';
}
Classic::~Classic()
{
}
void Classic::Report() const
{using std::cout;using std::endl;Cd::Report();cout << "Major works: " << majorWorks << endl;
}
Classic &Classic::operator=(const Classic &c)
{if (this == &c)return *this;Cd::operator=(c);strncpy(majorWorks, c.majorWorks, 49);majorWorks[49] = '\0';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(); // use Cd methodc2.Report(); // use Classic methodcout << "Using type cd *pointer to objects:\n";pcd->Report(); // use Cd method for cd objectpcd = &c2;pcd->Report(); // use Classic method for classic objectcout << "Calling a function with a Cd reference argument:\n";Bravo(c1);Bravo(c2);cout << "Testing assignment: ";Classic copy;copy = c2;copy.Report();system("pause");return 0;
}void Bravo(const Cd &disk)
{disk.Report();
}

运行结果:

Microsoft Windows [版本 10.0.19044.2728]
(c) Microsoft Corporation。保留所有权利。C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.1>g++ cd.cpp classic.cpp main.cpp -o main
main.cpp: In function 'int main()':
main.cpp:9:41: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]Cd c1("beatles", "Capitol", 14, 35.5);^
main.cpp:9:41: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:10:104: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);^
main.cpp:10:104: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:10:104: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.1>main
Using object directly:
Information:
Performers: beatles
Label: Capitol
Selections: 14
Playtime: 35.5
Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
Using type cd *pointer to objects:
Information:
Performers: beatles
Label: Capitol
Selections: 14
Playtime: 35.5
Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
Calling a function with a Cd reference argument:  
Information:
Performers: beatles
Label: Capitol
Selections: 14
Playtime: 35.5
Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
Testing assignment: Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
请按任意键继续. . .C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.1>

2. 使用动态内存分配重做练习1

完成练习1,但让两个类使用动态内存分配而不是长度固定的数组来记录字符串。

代码:

cd.h

#ifndef CD_H_
#define CD_H_class Cd
{ // represents a CD disk
private:char *performers;char *label;int selections;  // number of selectionsdouble playtime; // playing time in minutepublic:Cd(char *s1, char *s2, int n, double x);Cd(const Cd &d);Cd();virtual ~Cd();virtual void Report() const; // reports all CD datavirtual Cd &operator=(const Cd &d);
};#endif

cd.cpp:

#include "cd.h"
#include <iostream>
#include <cstring>
using std::strcpy;
using std::strlen;Cd::Cd(char *s1, 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];strcpy(performers, "\0");label = new char[1];strcpy(label, "\0");selections = 0;playtime = 0.0;
}
Cd::~Cd()
{delete[] performers;delete[] label;
}
void Cd::Report() const
{using std::cout;using std::endl;cout << "Information:\n";cout << "Performers: " << performers << endl;cout << "Label: " << label << endl;cout << "Selections: " << selections << endl;cout << "Playtime: " << playtime << endl;
}
Cd &Cd::operator=(const 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;
}

classic.h:

#ifndef CLASSIC_H_
#define CLASSIC_H_#include "cd.h"class Classic : public Cd
{
private:char *majorWorks;public:Classic(char *m, char *s1, char *s2, int n, double x);Classic(char *m, const Cd &cd);Classic();virtual ~Classic();virtual void Report() const;virtual Classic &operator=(const Classic &c);
};#endif

classic.cpp:

#include "classic.h"
#include <iostream>
#include <cstring>
using std::strcpy;
using std::strlen;Classic::Classic(char *m, char *s1, char *s2, int n, double x) : Cd(s1, s2, n, x)
{majorWorks = new char[strlen(m) + 1];strcpy(majorWorks, m);
}
Classic::Classic(char *m, const Cd &cd) : Cd(cd)
{majorWorks = new char[strlen(m) + 1];strcpy(majorWorks, m);
}
Classic::Classic() : Cd()
{majorWorks = new char[1];strcpy(majorWorks, "\0");
}
Classic::~Classic()
{delete[] majorWorks;
}
void Classic::Report() const
{using std::cout;using std::endl;Cd::Report();cout << "Major works: " << majorWorks << endl;
}
Classic &Classic::operator=(const Classic &c)
{if (this == &c)return *this;Cd::operator=(c);majorWorks = new char[strlen(c.majorWorks) + 1];strcpy(majorWorks, c.majorWorks);return *this;
}

main.cpp与练习1的main.cpp相同。

运行结果:

C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.2>g++ cd.cpp classic.cpp main.cpp -o main
main.cpp: In function 'int main()':
main.cpp:9:41: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]Cd c1("beatles", "Capitol", 14, 35.5);^
main.cpp:9:41: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:10:104: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);^
main.cpp:10:104: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:10:104: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.2>main
Using object directly:
Information:
Performers: beatles
Label: Capitol
Selections: 14
Playtime: 35.5
Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
Using type cd *pointer to objects:
Information:
Performers: beatles
Label: Capitol
Selections: 14
Playtime: 35.5
Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
Calling a function with a Cd reference argument:
Information:
Performers: beatles
Label: Capitol
Selections: 14
Playtime: 35.5
Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
Testing assignment: Information:
Performers: Alfred Brendel
Label: Philips
Selections: 2
Playtime: 57.17
Major works: Piano Sonata in B flat, Fantasia in C
请按任意键继续. . .C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.2>

3. baseDMA、lacksDMA、hasDMA类

修改baseDMA-lacksDMA-hasDMA类层次,让三个类都从一个ABC派生而来,然后使用与程序清单13.10相似的程序对结果进行测试。也就是说,它应使用ABC指针数组,并让用户决定要创建的对象类型。在类定义中添加virtual View()方法以处理数据显示。

代码:

dma.h:

#ifndef DMA_H_
#define DMA_H_#include <iostream>class ABC
{
public:ABC(){};~ABC(){};virtual void View() = 0;
};class baseDMA : public ABC
{
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 std::ostream &operator<<(std::ostream &os, const baseDMA &rs);virtual void View();
};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<<(std::ostream &os, const lacksDMA &rs);virtual void View();
};
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 &hs);friend std::ostream &operator<<(std::ostream &os, const hasDMA &hs);virtual void View();
};#endif

dma.cpp:

#include "dma.h"
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::strcpy;
using std::strlen;
using std::strncpy;// baseDMA methods
baseDMA::baseDMA(const char *l, int r)
{label = new char[strlen(l) + 1];strcpy(label, l);rating = r;
}
baseDMA::baseDMA(const baseDMA &rs)
{label = new char[strlen(rs.label) + 1];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[strlen(rs.label) + 1];strcpy(label, rs.label);rating = rs.rating;return *this;
}
std::ostream &operator<<(std::ostream &os, const baseDMA &rs)
{os << "Label: " << rs.label << endl;os << "Rating: " << rs.rating << endl;return os;
}
void baseDMA::View()
{cout << "Label: " << label << endl;cout << "Rating: " << rating << endl;
}// lacksDMA methods
lacksDMA::lacksDMA(const char *c, const char *l, int r) : baseDMA(l, r)
{strncpy(color, c, 39);color[39] = '\0';
}
lacksDMA::lacksDMA(const char *c, const baseDMA &rs) : baseDMA(rs)
{strncpy(color, c, COL_LEN - 1);color[COL_LEN - 1] = '\0';
}
std::ostream &operator<<(std::ostream &os, const lacksDMA &ls)
{os << (const baseDMA &)ls;os << "Color: " << ls.color << endl;return os;
}
void lacksDMA::View()
{baseDMA::View();cout << "Color: " << color << endl;
}// hasDMA methods
hasDMA::hasDMA(const char *s, const char *l, int r) : baseDMA(l, r)
{style = new char[strlen(s) + 1];strcpy(style, s);
}
hasDMA::hasDMA(const char *s, const baseDMA &rs) : baseDMA(rs)
{style = new char[strlen(s) + 1];strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA &hs) : baseDMA(hs)
{style = new char[strlen(hs.style) + 1];strcpy(style, hs.style);
}
hasDMA::~hasDMA()
{delete[] style;
}
hasDMA &hasDMA::operator=(const hasDMA &hs)
{if (this == &hs)return *this;baseDMA::operator=(hs);delete[] style;style = new char[strlen(hs.style) + 1];strcpy(style, hs.style);return *this;
}
std::ostream &operator<<(std::ostream &os, const hasDMA &hs)
{os << (const baseDMA &)hs;os << "Style: " << hs.style << endl;return os;
}
void hasDMA::View()
{baseDMA::View();cout << "Style: " << style << endl;
}

main.cpp:

#include "dma.h"
#include <iostream>
#include <string>
using namespace std;int main()
{int n;cout << "How many DMAs do you have? ";cin >> n;ABC *p_dma[n];char *t_label = new char[50];int t_rating;int kind;for (int i = 0; i < n; i++){cout << "Enter label: ";cin.getline(t_label, 50);cout << "Enter rating: ";cin >> t_rating;cout << "Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: ";while (cin >> kind && kind != 1 && kind != 2 && kind != 3)cout << "Enter either 1 or 2 or 3: ";cin.ignore();switch (kind){case 1:p_dma[i] = new baseDMA(t_label, t_rating);break;case 2:char t_color[40];cout << "Enter color: ";cin.getline(t_color, 40);p_dma[i] = new lacksDMA(t_color, t_label, t_rating);break;case 3:char *t_style = new char[20];cout << "Enter style: ";cin.getline(t_style, 20);p_dma[i] = new hasDMA(t_style, t_label, t_rating);break;}while (cin.get() != '\n')continue;}cout << endl;for (int i = 0; i < n; i++){p_dma[i]->View();cout << endl;}for (int i = 0; i < n; i++){delete p_dma[i];}cout << "Done.\n"<< endl;system("pause");return 0;
}

运行结果:

Microsoft Windows [版本 10.0.19044.2728]
(c) Microsoft Corporation。保留所有权利。C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.3>g++ dma.cpp main.cpp -o mainC:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.3>main
Enter label: Portabelly
Enter rating: 8
Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: 1Enter label: Blimpo
Enter rating: 4
Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: 2
Enter color: redEnter label: Buffalo Keys
Enter rating: 5
Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: 3
Enter style: MercatorEnter label: lable1
Enter rating: 10
Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: 1Enter label: lable2
Enter rating: 3
Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: 1Enter label: lable3
Enter rating: 1
Enter 1 for baseDMA or 2 for lacksDMA or 3 for hasDMA: 9
Enter either 1 or 2 or 3: 1Label: Portabelly
Rating: 8Label: Blimpo
Rating: 4
Color: redLabel: Buffalo Keys
Rating: 5
Style: MercatorLabel: lable1
Rating: 10Label: lable2
Rating: 3Label: lable3
Rating: 1Done.请按任意键继续. . .C:\Users\81228\Documents\Program\VScode C++ Program\chapter13\13.3>

4. Port类和VintagePort类

Benevolent Order of Programmers用来维护瓶装葡萄酒箱。为描述它,BOP Portmaster设置了一个Port类,并声明如下:

#include <iostream>
using namespace std;
class Port
{
private:char *brand;char style[20]; // i.e., tawny, ruby, vintageint bottles;public:Port(const char *br = "none", const char *st = "none", int b = 0);Port(const Port &p); // copy constructorvirtual ~Port() { delete[] brand; }Port &operator=(const Port &p);Port &operator+=(int b); // add b to bottlesPort &operator-=(int b); // subtracts b from bottles , if availableint BottleCount() const { return bottles; }virtual void Show() const;friend ostream &operator<<(ostream &os, const Port &p);
};

show()方法按下面的格式显示信息:

Brand : Gallo
Kind : tawny
Bottles : 20

operator<<()函数按下面的格式显示信息(末尾没有换行符):

Gallo, tawny, 20

PortMaster完成了Port类的方法定义后派生了VintagePort类,然后被解职——因为不小心将一瓶45度Cockburn泼到了正在准备烤肉调料的人身上,VintagePort类如下显示:

class VintagePort : public Port //style necessarily = "vintage"
{
private:char* nickname; //i.e. , "The Noble" or "Old Velvet", etc.int year; //vintage year
public:VintagePort();VintagePort(const char* br, const char* st, 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);
};

您被制定指定负责完成VintagePort。
a.第一个任务是重新创建Port方法定义,因为前任被开除时销毁了方法定义。
b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
c.第三个任务解释为何没有将operator = () 和operator << ()声明为虚的。
d.第四个任务是提供VintagePort中各个方法的定义。

答:

a.第一个任务是重新创建Port方法定义,因为前任被开除时销毁了方法定义。

#include "port.h"
#include <iostream>
#include <cstring>
using namespace std;Port::Port(const char *br, const char *st, int b)
{brand = new char[strlen(br) + 1];strcpy(brand, br);strcpy(style, st);bottles = b;
}
Port::Port(const Port &p) // copy constructor
{brand = new char[strlen(p.brand) + 1];strcpy(brand, p.brand);strcpy(style, 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(brand, p.brand);strcpy(style, p.style);bottles = p.bottles;return *this;
}
Port &Port::operator+=(int b) // add b to bottles
{bottles += b;return *this;
}
Port &Port::operator-=(int b) // subtracts b from bottles , if available
{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;
}

b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。

当派生类成员函数需要处理派生类独有的成员变量时,需要重新定义方法,如果不需要处理派生类独有的成员变量,直接调用基类的方法即可。

c.第三个任务解释为何没有将operator = () 和operator << ()声明为虚的。

a).如果要在派生类中重新定义基类的方法,则将它设置为虚方法。但是基类的operator = () 和派生类的operator = () 形参不一样,根本不是一个类方法,不存在重新定义的问题,因从也不必声明为虚的。
b).operator<<()函数是友元函数,友元函数不能是虚函数,因为友元不是类成员,而只有类成员才能是虚函数。

d.第四个任务是提供VintagePort中各个方法的定义。

#include "VintagePort.h"
#include <iostream>
#include <cstring>
using namespace std;VintagePort::VintagePort() : Port()
{
}
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(nickname, nn);int year = y;
}
VintagePort::VintagePort(const VintagePort &vp) : Port(vp)
{nickname = new char[strlen(vp.nickname) + 1];strcpy(nickname, vp.nickname);int 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);strcpy(nickname, vp.nickname);year = vp.year;return *this;
}
void VintagePort::show() const
{Port::Show();cout << "Nick Name: " << 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.ppmy.cn/news/33587.html

相关文章

基于STM32的ADC采样及各式滤波实现(HAL库,含VOFA+教程)

前言&#xff1a;本文为手把手教学ADC采样及各式滤波算法的教程&#xff0c;本教程的MCU采用STM32F103ZET6。以HAL库的ADC采样函数为基础进行教学&#xff0c;通过各式常见滤波的实验结果进行分析对比&#xff0c;搭配VOFA工具直观的展示滤波效果。ADC与滤波算法都是嵌入式较为…

应用服务漏洞扫描与利用

任务环境说明: 服务器场景:Server15服务器场景操作系统:未知(关闭链接)使用命令nmap探测目标靶机的服务版本信息,将需要使用的参数作为FLAG进行提交;FLAG:sV 通过上述端口访问靶机系统并探测隐藏的页面,将找到的敏感文件、目录名作为FLAG(形式:[敏感文件或目录1,敏感…

不要迷信 QUIC

很多人都在强调 QUIC 能解决 HoL blocking 问题&#xff0c;不好意思&#xff0c;我又要泼冷水了。假设大家都懂 QUIC&#xff0c;不再介绍 QUIC 的细节&#xff0c;直接说问题。 和 TCP 一样&#xff0c;QUIC 也是一个基于连接的&#xff0c;保序的可靠传输协议&#xff0c;T…

redis 存储一个map 怎么让map中其中一个值设置过期时间,而不是过期掉整个map?

文章目录 redis 存储一个map 怎么让map中其中一个值设置过期时间,而不是过期掉整个map?Java 中 怎么 实现?方案一: Jedis方案二: Lettuce方案三: Redisson方案四: Jedisson方案五: RedisTemplate那种方式 效率最高 ?拓展:结语redis 存储一个map 怎么让map中其中一个值设置过…

Java面试总结篇

引用介绍 1.线程安全不安全的概念 ​ 线程安全: 指多个线程在执行同一段代码的时候采用加锁机制,使每次的执行结果和单线程执行的结果都是一样的,不存在执行程序时出现意外结果。 ​ 线程不安全: 是指不提供加锁机制保护,有可能出现多个线程先后更改数据造成所得到的数据是脏…

【Arduino 和 MPU6050 加速度计和陀螺仪教程】

【Arduino 和 MPU6050 加速度计和陀螺仪教程】 1. 概述2. 工作原理3. Arduino 和 MPU60504. MPU6050 Arduino 代码5. MPU6050 方向跟踪 – 3D 可视化1. 概述 在本教程中,我们将学习如何将MPU6050加速度计和陀螺仪传感器与Arduino一起使用。首先,我将解释MPU6050的工作原理以…

springboot http转https

springboot http转https 一、安全证书的生成 可以使用jdk自带的证书生成工具&#xff0c;jdk自带一个叫keytool的证书管理工具&#xff0c;可以用它来实现签名的证书。 1、进入cmd命令控制终端 2、生成一个证书 别名&#xff1a;alias tomcat 密码&#xff1a;keypass 123…

ThinkPHP01:数据库和模型

ThinkPHP01&#xff1a;数据库和模型一、开启调试模式二、配置文件三、URL解析四、数据库五、模型1. 定义模型2. 使用模型① 查询记录② 新增记录③ 删除记录④ 更新记录3. 字段设置4. 模型获取器5. 模型修改器6. 模型查询范围7. 模型数据集8. 模型的自动时间戳9. 模型的只读字…