【C++ Primer Plus习题】13.4

2024-09-09 17:44
文章标签 c++ plus 习题 primer 13.4

本文主要是介绍【C++ Primer Plus习题】13.4,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

问题:

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

解答:
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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1151849

相关文章

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性: