当前位置: 首页> 健康> 母婴 > 【C++ Primer Plus习题】13.4

【C++ Primer Plus习题】13.4

时间:2025/7/9 10:47:22来源:https://blog.csdn.net/qq_74047911/article/details/142061600 浏览次数:0次

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

这里是引用
在这里插入图片描述

解答:
main.cpp

#include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout << p1 << std::endl<<endl;std::cout << p2 << std::endl << endl;Port p3 = p2;p3.Show();p3 += 3;p3.Show();Port p4 = p2;p3 -= 2;p3.Show();cout << endl;VintagePort vp1("Vabc", 50, "hn", 1983);vp1.Show();cout << endl;VintagePort vp2;vp2.Show();cout << endl;vp1 -= 3;vp2 = vp1;std::cout << vp2 << std::endl;return 0;
}

port.h

#pragma once
#include <iostream>
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 override;friend ostream& operator<<(ostream& os, const VintagePort& vp);
};

port.cpp

#include "port.h"Port::Port(const char* br, const char* st, int b)
{brand = new char[strlen(br) + 1];strcpy_s(brand, strlen(br) + 1, br);strcpy_s(style, strlen(br) + 1, st);bottles = b;
}
Port::Port(const Port& p)
{brand = new char[strlen(p.brand) + 1];strcpy_s(brand, strlen(p.brand) + 1, p.brand);strcpy_s(style, strlen(p.style) + 1, p.style);bottles = p.bottles;
}
Port& Port::operator=(const Port& p)
{if (this == &p)return *this;if (brand)delete[] brand;brand = new char[strlen(p.brand) + 1];strcpy_s(brand, strlen(p.brand) + 1, p.brand);strcpy_s(style, strlen(p.style) + 1, p.style);bottles = p.bottles;return *this;
}
Port& Port::operator+=(int b)
{this->bottles += b;return *this;
}
Port& Port::operator-=(int b)
{this->bottles -= b;return *this;
}
void Port::Show()const
{cout << "Brand:" << this->brand << endl;cout << "Kind:" << this->style << endl;cout << "Bottles:" << this->bottles << endl;
}
ostream& operator<<(ostream& os, const Port& p)
{os << p.brand << "," << p.style << "," << p.bottles;return os;
}VintagePort::VintagePort():Port()
{nickname = new char[1];nickname[0] = '\0';year = 0;
}
VintagePort::VintagePort(const char* br, int b, const char* nn, int y):Port(br,"none",b)
{nickname = new char[strlen(nn) + 1];strcpy_s(nickname, strlen(nn) + 1, nn);year = y;
}
VintagePort::VintagePort(const VintagePort& vp):Port(vp)
{nickname = new char[strlen(vp.nickname) + 1];strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);year = vp.year;
}
VintagePort& VintagePort::operator=(const VintagePort& vp)
{if (this == &vp)return *this;if (nickname)delete[] nickname;Port::operator=(vp);nickname = new char[strlen(vp.nickname) + 1];strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);year = vp.year;return *this;
}
void VintagePort::Show()const
{Port::Show();cout << "nickname:" << nickname << endl;cout << "year:" << year << endl;
}
ostream& operator<<(ostream& os, const VintagePort& vp)
{os << (const Port&)vp << "," << vp.nickname << "," << vp.year << endl;return os;
}

运行结果:
在这里插入图片描述

考查点:

  • 继承
  • 虚函数

2024年9月9日16:05:29

关键字:【C++ Primer Plus习题】13.4

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: