《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》(第6版)第13章编程练习《C Primer Plus》(第6版)第13章编程练习1. Cd类2. 使用动态内存分配重做练习13. baseDMA、lacksDMA、hasDMA类4. Port类和VintagePort类《C Primer Plus》(第6版)第…...

【多线程】多线程案例
✨个人主页:bit me👇 ✨当前专栏:Java EE初阶👇 ✨每日一语:we can not judge the value of a moment until it becomes a memory. 目 录🍝一. 单例模式🍤1. 饿汉模式实现🦪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. 临界资源(critical resource) 3. 生产者-消费者问题 4. 临界区(critical section) 5. 同步机制应遵循的规则 2.4.2 硬件同步机制 1. 关中断 2. Test-and-Set …...
AI独立开发者:一周涨粉8万赚2W美元;推特#HustleGPT GPT-4创业挑战;即刻#AIHackathon创业者在行动 | ShowMeAI周刊
👀日报&周刊合辑 | 🎡生产力工具与行业应用大全 | 🧡 点赞关注评论拜托啦! 这是ShowMeAI周刊的第7期。聚焦AI领域本周热点,及其在各圈层泛起的涟漪;拆解AI独立开发者的盈利案例,关注中美AIG…...

不要迷信 QUIC
很多人都在强调 QUIC 能解决 HoL blocking 问题,不好意思,我又要泼冷水了。假设大家都懂 QUIC,不再介绍 QUIC 的细节,直接说问题。 和 TCP 一样,QUIC 也是一个基于连接的,保序的可靠传输协议,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、前言 前面的内容已经把基础的增删改查介绍的差不多了,也介绍了表的相关约束, 从本期开始…...

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

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

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

【SpringMVC】SpringMVC方式,向作用域对象共享数据(ModelAndView、Model、map、ModelMap)
个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~ 个人主页:.29.的博客 学习社区:进去逛一逛~ 向域对象共享数据一、使用 原生ServletAPI二、…...
本科课程【移动互联网应用开发(Android开发)】实验3 - Activity及数据存储
大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。 如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!! Good better best, never let it rest, until good is better, and better best. 近期会把自己本科阶段的一些课程设计、实验报…...
为何在 node 项目中使用固定版本号,而不使用 ~、^?
以语雀 文档为准 使用 ~、^ 时吃过亏希望版本号掌握在自己手里,作者自己升级(跟随官方进行升级,就算麻烦作者,也不想麻烦使用者)虽然 pnpm 很好用,但是不希望在项目中用到(临时性解决问题可以选…...

leetcode -- 876.链表的中间节点
文章目录🐨1.题目🐇2. 解法1-两次遍历🍀2.1 思路🍀2.2 代码实现🐁3. 解法2-快慢指针🌾3.1 思路🌾3.2 **代码实现**🐮4. 题目链接🐨1.题目 给你单链表的头结点head&#…...
企业网络安全防御策略需要考虑哪些方面?
随着企业数字化转型的加速,企业网络安全面临越来越多的威胁。企业网络安全不仅仅关乎企业数据的安全,还关系到企业的声誉和利益,因此,建立全面的网络安全防御策略至关重要。 企业网络安全防御策略的实现需要考虑以下几个方面&…...

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

【进阶数据结构】二叉搜索树经典习题讲解
🌈感谢阅读East-sunrise学习分享——[进阶数据结构]二叉搜索树 博主水平有限,如有差错,欢迎斧正🙏感谢有你 码字不易,若有收获,期待你的点赞关注💙我们一起进步 🌈我们在之前已经学习…...

PyTorch 之 神经网络 Mnist 分类任务
文章目录一、Mnist 分类任务简介二、Mnist 数据集的读取三、 Mnist 分类任务实现1. 标签和简单网络架构2. 具体代码实现四、使用 TensorDataset 和 DataLoader 简化本文参加新星计划人工智能(Pytorch)赛道:https://bbs.csdn.net/topics/613989052 一、Mnist 分类任…...
如何实现用pillow库来实现给图片加滤镜?
使用Pillow库可以非常容易地给图片加滤镜。Pillow库是Python图像处理的一个强大库,提供了多种滤镜效果,如模糊、边缘检测、色彩增强等。 下面是使用Pillow库实现给图片加滤镜的简单步骤: 安装Pillow库:首先需要安装Pillow库。可…...
React Native在HarmonyOS 5.0阅读类应用开发中的实践
一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强,React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 (1)使用React Native…...

剑指offer20_链表中环的入口节点
链表中环的入口节点 给定一个链表,若其中包含环,则输出环的入口节点。 若其中不包含环,则输出null。 数据范围 节点 val 值取值范围 [ 1 , 1000 ] [1,1000] [1,1000]。 节点 val 值各不相同。 链表长度 [ 0 , 500 ] [0,500] [0,500]。 …...

从零实现STL哈希容器:unordered_map/unordered_set封装详解
本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说,直接开始吧! 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...

UR 协作机器人「三剑客」:精密轻量担当(UR7e)、全能协作主力(UR12e)、重型任务专家(UR15)
UR协作机器人正以其卓越性能在现代制造业自动化中扮演重要角色。UR7e、UR12e和UR15通过创新技术和精准设计满足了不同行业的多样化需求。其中,UR15以其速度、精度及人工智能准备能力成为自动化领域的重要突破。UR7e和UR12e则在负载规格和市场定位上不断优化…...
C++.OpenGL (20/64)混合(Blending)
混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...
探索Selenium:自动化测试的神奇钥匙
目录 一、Selenium 是什么1.1 定义与概念1.2 发展历程1.3 功能概述 二、Selenium 工作原理剖析2.1 架构组成2.2 工作流程2.3 通信机制 三、Selenium 的优势3.1 跨浏览器与平台支持3.2 丰富的语言支持3.3 强大的社区支持 四、Selenium 的应用场景4.1 Web 应用自动化测试4.2 数据…...
【前端异常】JavaScript错误处理:分析 Uncaught (in promise) error
在前端开发中,JavaScript 异常是不可避免的。随着现代前端应用越来越多地使用异步操作(如 Promise、async/await 等),开发者常常会遇到 Uncaught (in promise) error 错误。这个错误是由于未正确处理 Promise 的拒绝(r…...
redis和redission的区别
Redis 和 Redisson 是两个密切相关但又本质不同的技术,它们扮演着完全不同的角色: Redis: 内存数据库/数据结构存储 本质: 它是一个开源的、高性能的、基于内存的 键值存储数据库。它也可以将数据持久化到磁盘。 核心功能: 提供丰…...

云原生时代的系统设计:架构转型的战略支点
📝个人主页🌹:一ge科研小菜鸡-CSDN博客 🌹🌹期待您的关注 🌹🌹 一、云原生的崛起:技术趋势与现实需求的交汇 随着企业业务的互联网化、全球化、智能化持续加深,传统的 I…...
Spring事务传播机制有哪些?
导语: Spring事务传播机制是后端面试中的必考知识点,特别容易出现在“项目细节挖掘”阶段。面试官通过它来判断你是否真正理解事务控制的本质与异常传播机制。本文将从实战与源码角度出发,全面剖析Spring事务传播机制,帮助你答得有…...