手捧星光945 2022-04-21 17:19 采纳率: 85%
浏览 15
已结题

C++重载运算符里的格式

问题遇到的现象和发生背景

本问题来源于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能告诉我这种是什么类型,谢谢!

  • 写回答

1条回答 默认 最新

  • 对象被抛出 2022-04-21 19:00
    关注

    重载<<输出, 自定义的class和struct是不能直接cout的, 必须重载这个操作符才能使用cout来输出, 具体输出的内容也要重载

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月11日
  • 已采纳回答 10月3日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错