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

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

《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;
}

相关文章:

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

《C Primer Plus》&#xff08;第6版&#xff09;第13章编程练习《C Primer Plus》&#xff08;第6版&#xff09;第13章编程练习1. Cd类2. 使用动态内存分配重做练习13. baseDMA、lacksDMA、hasDMA类4. Port类和VintagePort类《C Primer Plus》&#xff08;第6版&#xff09;第…...

【多线程】多线程案例

✨个人主页&#xff1a;bit me&#x1f447; ✨当前专栏&#xff1a;Java EE初阶&#x1f447; ✨每日一语&#xff1a;we can not judge the value of a moment until it becomes a memory. 目 录&#x1f35d;一. 单例模式&#x1f364;1. 饿汉模式实现&#x1f9aa;2. 懒汉模…...

【IoT】嵌入式驱动开发:IIC子系统

IIC有三种接口实现方式 三种时序对比: 图1 IIC子系统组成 图2 图3 IIC操作流程 设备端 1.i2c_get_adapter 2.i2c_new_device(相当于register设备) 3.I2c_put_adapter 驱动端 1.填充i2c_driver 2.i2c_add_driver(相当于register驱动) 3.在probe中建立访问方式 client相…...

DJ2-4 进程同步(第一节课)

目录 2.4.1 进程同步的基本概念 1. 两种形式的制约关系 2. 临界资源&#xff08;critical resource&#xff09; 3. 生产者-消费者问题 4. 临界区&#xff08;critical section&#xff09; 5. 同步机制应遵循的规则 2.4.2 硬件同步机制 1. 关中断 2. Test-and-Set …...

AI独立开发者:一周涨粉8万赚2W美元;推特#HustleGPT GPT-4创业挑战;即刻#AIHackathon创业者在行动 | ShowMeAI周刊

&#x1f440;日报&周刊合辑 | &#x1f3a1;生产力工具与行业应用大全 | &#x1f9e1; 点赞关注评论拜托啦&#xff01; 这是ShowMeAI周刊的第7期。聚焦AI领域本周热点&#xff0c;及其在各圈层泛起的涟漪&#xff1b;拆解AI独立开发者的盈利案例&#xff0c;关注中美AIG…...

不要迷信 QUIC

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

【28】Verilog进阶 - RAM的实现

VL53 单端口RAM 1 思路 简简单单,读取存储器单元值操作即可 2 功能猜想版 说明: 下面注释就是我对模块端口信号 自己猜测的理解。 因为题目并没有说清楚,甚至连参考波形都没有给出。 唉,这就完全是让人猜测呢,如果一点学术背景的人来刷题,指定不容易!! 好在,我有较为…...

【MySQL】聚合查询

目录 1、前言 2、插入查询结果 3、聚合查询 3.1 聚合函数 3.1.1 count 3.1.2 sum 3.1.3 avg 3.1.4 max 和 min 4、GROUP BY 子句 5、HAVING 关键字 1、前言 前面的内容已经把基础的增删改查介绍的差不多了&#xff0c;也介绍了表的相关约束&#xff0c; 从本期开始…...

初时STM32单片机

目录 一、单片机基本认知 二、STM系列单片机命名规则 三、标准库与HAL库区别 四、通用输入输出端口GPIO 五、推挽输出与开漏输出 六、复位和时钟控制&#xff08;RCC&#xff09; 七、时钟控制 八、中断和事件 九、定时器介绍 一、单片机基本认知 单片机和PC电脑相比…...

debian部署docker(傻瓜式)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 debian10部署dockerdebian10部署docker&#xff08;傻瓜式&#xff09;一、准备工作二、**使用 APT 安装&#xff0c;注意要先配置apt网络源**1.配置网络源2.官方下载三、安装…...

JS判断是否为base64字符串如何转换为图片src格式

需求背景 &#xff1a; 如何判断后端给返回的 字符串 是否为 base-64 位 呢 &#xff1f; 以及如果判断为是的话&#xff0c;如何给它进行转换为 img 标签可使用的那种 src 格式 呢 &#xff1f; 1、判断字符串是否为 base64 以下方法&#xff0c;可自行挨个试试&#xff0c;…...

【SpringMVC】SpringMVC方式,向作用域对象共享数据(ModelAndView、Model、map、ModelMap)

个人简介&#xff1a;Java领域新星创作者&#xff1b;阿里云技术博主、星级博主、专家博主&#xff1b;正在Java学习的路上摸爬滚打&#xff0c;记录学习的过程~ 个人主页&#xff1a;.29.的博客 学习社区&#xff1a;进去逛一逛~ 向域对象共享数据一、使用 原生ServletAPI二、…...

本科课程【移动互联网应用开发(Android开发)】实验3 - Activity及数据存储

大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。 如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!! Good better best, never let it rest, until good is better, and better best. 近期会把自己本科阶段的一些课程设计、实验报…...

为何在 node 项目中使用固定版本号,而不使用 ~、^?

以语雀 文档为准 使用 ~、^ 时吃过亏希望版本号掌握在自己手里&#xff0c;作者自己升级&#xff08;跟随官方进行升级&#xff0c;就算麻烦作者&#xff0c;也不想麻烦使用者&#xff09;虽然 pnpm 很好用&#xff0c;但是不希望在项目中用到&#xff08;临时性解决问题可以选…...

leetcode -- 876.链表的中间节点

文章目录&#x1f428;1.题目&#x1f407;2. 解法1-两次遍历&#x1f340;2.1 思路&#x1f340;2.2 代码实现&#x1f401;3. 解法2-快慢指针&#x1f33e;3.1 思路&#x1f33e;3.2 **代码实现**&#x1f42e;4. 题目链接&#x1f428;1.题目 给你单链表的头结点head&#…...

企业网络安全防御策略需要考虑哪些方面?

随着企业数字化转型的加速&#xff0c;企业网络安全面临越来越多的威胁。企业网络安全不仅仅关乎企业数据的安全&#xff0c;还关系到企业的声誉和利益&#xff0c;因此&#xff0c;建立全面的网络安全防御策略至关重要。 企业网络安全防御策略的实现需要考虑以下几个方面&…...

文心一言 vs. GPT-4 —— 全面横向比较

文心一言 vs. GPT-4 —— 全面横向比较 3月15日凌晨&#xff0c;OpenAI发布“迄今为止功能最强大的模型”——GPT-4。我第一时间为大家奉上了体验报告《OpenAI 发布GPT-4——全网抢先体验》。 时隔一日&#xff0c;3月16日下午百度发布大语言模型——文心一言。发布会上&…...

【进阶数据结构】二叉搜索树经典习题讲解

&#x1f308;感谢阅读East-sunrise学习分享——[进阶数据结构]二叉搜索树 博主水平有限&#xff0c;如有差错&#xff0c;欢迎斧正&#x1f64f;感谢有你 码字不易&#xff0c;若有收获&#xff0c;期待你的点赞关注&#x1f499;我们一起进步 &#x1f308;我们在之前已经学习…...

PyTorch 之 神经网络 Mnist 分类任务

文章目录一、Mnist 分类任务简介二、Mnist 数据集的读取三、 Mnist 分类任务实现1. 标签和简单网络架构2. 具体代码实现四、使用 TensorDataset 和 DataLoader 简化本文参加新星计划人工智能(Pytorch)赛道&#xff1a;https://bbs.csdn.net/topics/613989052 一、Mnist 分类任…...

如何实现用pillow库来实现给图片加滤镜?

使用Pillow库可以非常容易地给图片加滤镜。Pillow库是Python图像处理的一个强大库&#xff0c;提供了多种滤镜效果&#xff0c;如模糊、边缘检测、色彩增强等。 下面是使用Pillow库实现给图片加滤镜的简单步骤&#xff1a; 安装Pillow库&#xff1a;首先需要安装Pillow库。可…...

微分中值定理

极值 目录 极值 费马引理 ​编辑 罗尔定理 拉格朗日中值定理 例题&#xff1a; 例2 例3 两个重要结论&#xff1a; ​编辑 柯西中值定理&#xff1a; 如何用自己的语言理解极值呢&#xff1f; 极大值和极小值的类似&#xff0c;我们不再进行说明 极值点有什么特点吗&…...

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

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

LeetCode:704. 二分查找

&#x1f34e;道阻且长&#xff0c;行则将至。&#x1f353; &#x1f33b;算法&#xff0c;不如说它是一种思考方式&#x1f340;算法专栏&#xff1a; &#x1f449;&#x1f3fb;123 一、&#x1f331;704. 二分查找 题目描述&#xff1a;给定一个 n 个元素有序的&#xff…...

Java 到底是值传递还是引用传递?

C 语言是很多变成语言的母胎&#xff0c;包括 Java。对于 C 语言来说&#xff0c;所有的方法参数都是通过 “值” 传递的&#xff0c;也就是说&#xff0c;传递给被调用方法的参数值存放在临时变量中&#xff0c;而不是存放在原来的变量中。这就意味着&#xff0c;被调用的方法…...

Apollo 配置变更原理

我们经常用到apollo的两个特性&#xff1a;1.动态更新配置&#xff1a;apollo可以动态更新Value的值&#xff0c;也可以修改environment的值。2.实时监听配置&#xff1a;实现apollo的监听器ConfigChangeListener&#xff0c;通过onChange方法来实时监听配置变化。你知道apollo…...

聊聊「订单」业务的设计与实现

订单&#xff0c;业务的核心模块&#xff1b; 一、背景简介 订单业务一直都是系统研发中的核心模块&#xff0c;订单的产生过程&#xff0c;与系统中的很多模块都会高度关联&#xff0c;比如账户体系、支付中心、运营管理等&#xff0c;即便单看订单本身&#xff0c;也足够的复…...

血细胞智能检测与计数软件(Python+YOLOv5深度学习模型+清新界面版)

摘要&#xff1a;血细胞智能检测与计数软件应用深度学习技术智能检测血细胞图像中红细胞、镰状细胞等不同形态细胞并可视化计数&#xff0c;以辅助医学细胞检测。本文详细介绍血细胞智能检测与计数软件&#xff0c;在介绍算法原理的同时&#xff0c;给出Python的实现代码以及Py…...

高速PCB设计指南(十五)

掌握IC封装的特性以达到最佳EMI抑制性能 将去耦电容直接放在IC封装内可以有效控制EMI并提高信号的完整性&#xff0c;本文从IC内部封装入手&#xff0c;分析EMI的来源、IC封装在EMI控制中的作用&#xff0c;进而提出11个有效控制EMI的设计规则&#xff0c;包括封装选择、引脚结…...

GPT-4:我不是来抢你饭碗的,我是来抢你锅的

目录 一、GPT-4&#xff0c;可媲美人类 二、它和ChatGPT 有何差别&#xff1f; 01、处理多达2.5万字的长篇内容 02、分析图像的能力&#xff0c;并具有「幽默感」 03、生成网页 三、题外话 四、小结 GPT-4的闪亮登场&#xff0c;似乎再次惊艳了所有人。 看了GPT-4官方的…...

Scala环境安装【傻瓜式教程】

文章目录安装scala环境依赖Java环境安装下载sacla的sdk包安装Scala2.12检查安装是否成功idea配置idea安装scala插件项目配置新建maven项目添加框架支持选择scala创建测试类安装scala环境依赖 Java环境安装 sacla环境安装之前需要先确认Java jdk安装完成 java具体安装步骤略&…...