问题遇到的现象和发生背景
本问题来源于C++ primes plus 第十三章类继承的课后习题第四题编程题,为了防止有歧义所有代码如下,只是想知道这种格式是什么意思 os<<(const Port &)vp
问题相关代码,请勿粘贴截图
#include
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);
};
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);
};
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)
{
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;
/brand = p.brand;
strcpy(style, p.style);
bottles = p.bottles;/
}
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;
}
std::ostream& operator<<(ostream& os, const Port& p)
{
os << p.brand << "," << p.style << "," << p.bottles;
return os;
}
VintagePort::VintagePort(const char* br, int b, const char* nn, int y):Port(br,"none",b)
{
nickname = new char[strlen(nn) + 1];
strcpy(nickname, nn);
year = y;
}
VintagePort::VintagePort() :Port()
{
nickname = new char[5];
strcpy(nickname, "none");
year = 0;
}
VintagePort::VintagePort(const VintagePort& vp) :Port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
}
VintagePort& VintagePort::operator=(const VintagePort& vp)
{
if (this == &vp)
return *this;
delete[]nickname;
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::show()const
{
Port::show();
cerr << "Nickname" << nickname;
cerr << "Year:" << year;
}
ostream& operator<<(ostream os, const VintagePort& vp)
{
** os << (const Port&)vp;
```c++
```**
os << "," << vp.nickname << vp.year << endl;
return os;
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
原因可能跟全篇代码无关,希望各位C++er能告诉我这种是什么类型,谢谢!