手捧星光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日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改