【C++ 自写String】

CString.hpp

#include <iostream>
#include <string.h>#pragma warning(disable:4996)
using namespace std;class CString {
private:int len;char* data;public:CString():data(nullptr),len(0) {cout << "0空构造\n";}CString(const char* _data) {if (!_data) {len = 0;data = new char(1);*data = '\0';cout << "1空构造\n";}else {len = (int)strlen(_data);data = new char[strlen(_data) + 1];memset(data, 0, len + 1);strcpy(data, _data);cout << "char*构造\n";}}const char* getValue() const {return data;}int getLength() const{return len;}CString (const CString& str){len = str.getLength();data = new char[len+1];memset(data, 0, len + 1);strcpy(data, str.getValue());cout << "str构造\n";}CString& operator=(const CString & str){if (this == &str) {return *this;}len = str.getLength();delete[] data;data = new char[len + 1];strcpy(data, str.getValue());return *this;}CString operator+(const CString &str) const{CString nstr;nstr.len = this->len + str.getLength();nstr.data = new char[nstr.len + 1];strcpy(nstr.data, (*this).data);strcat(nstr.data, str.getValue());return nstr;}~CString() {delete[] data;}};class String {
private:char* data; // 存储字符数组的指针
public:String(const char* str = "") : data(new char[strlen(str) + 1]) {strcpy(data, str);}~String() {delete[] data;}const char* getData() const {return data;}
};

Shape.hpp

#include<iostream>class Shape {
public:virtual double printArea() = 0;
};class Circle :public Shape {
private:double r;
public:Circle(double _r) { r = _r; }virtual double printArea() {return r * r * 3.14159;}
};class Rectangle :public Shape {
private:double w, h;
public:Rectangle(double _w, double _h) { w = _w; h = _h; }virtual double printArea() {return w * h;}
};class Triangle :public Shape {
private:double w, h;
public:Triangle(double _w, double _h){w = _w;h = _h;}virtual double printArea() {return w * h /2;}
};

cmain.cpp

#include "CString.hpp"
#include "Shape.hpp"
#include <vector>int main() 
{
#if 1CString s1("123");CString s2(s1);CString s3 = s1 + s2;CString s4;s4 = s3;cout << s4.getLength() << "," << s4.getValue() << endl;vector<Shape*> ss;Shape* c = new Circle(2.1);Shape* r = new Rectangle(4, 5);Shape* t = new Triangle(4,2);ss.push_back(c);ss.push_back(r);ss.push_back(t);for (auto v = ss.begin(); v != ss.end(); v ++) {cout<< "area:" << (*v)->printArea() << endl;Shape* ptr = *v;delete ptr;ptr = nullptr;}ss.erase(ss.begin(), ss.end());
#endifreturn 0;
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/427343.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【详细解释深度学习图像分类检测评价指标】准确率Accuracy、精确率Precision、召回率Recall、mAP等(一文搞懂,建议收藏)

前言&#xff1a; &#x1f60a;&#x1f60a;&#x1f60a;欢迎来到本博客&#x1f60a;&#x1f60a;&#x1f60a; &#x1f31f;&#x1f31f;&#x1f31f; 本专栏主要是记录工作中、学习中关于AI(Deep Learning)相关知识并分享。 &#x1f60a;&#x1f60a;&#x1f…

函数递归(Recursion)一篇便懂

递归的概念 在 C 语言中&#xff0c;递归&#xff08;Recursion&#xff09;是一种函数调用自身的编程技术。当一个函数在其定义中调用自身时&#xff0c;就称为递归函数。 了解递归思想 把⼀个大型复杂问题层层转化为⼀个与原问题相似&#xff0c;但规模较小的子问题来求解…

常用界面设计组件 —— 容器组件

2.6 容器组件2.6.1 QGroupBox2.6.2 QScrollArea2.6.3 QToolBox2.6.4 QTabWidget2.6.5 QStackedWidget 2.6 容器组件 为了将界面上的各个组件的分布设计得更加美观&#xff0c;经常 使用一些容器类&#xff0c;如 QgoupBox、QtabWidget、 QToolBox等。 2.6.1 QGroupBox 实例效…

UE4运用C++和框架开发坦克大战教程笔记(十五)(第46~48集)

UE4运用C和框架开发坦克大战教程笔记&#xff08;十五&#xff09;&#xff08;第46~48集&#xff09; 46. 批量加载 UClass 功能测试批量加载多个同类 UClass 资源 47. 创建单个资源对象测试加载并创建单个 UClass 资源对象 48. 创建同类资源对象 46. 批量加载 UClass 功能 逻…

Web开发4:单元测试

在Web开发中&#xff0c;单元测试是一种重要的开发实践&#xff0c;它可以帮助我们确保代码的质量和可靠性。通过编写和运行单元测试&#xff0c;我们可以验证代码的正确性&#xff0c;减少错误和缺陷&#xff0c;并提高代码的可维护性。本文将介绍单元测试的概念、好处以及如何…

Go后端开发 -- 即时通信系统

Go后端开发 – 即时通信系统 文章目录 Go后端开发 -- 即时通信系统一、即时通信系统1.整体框架介绍2.基础server构建3.用户上线及广播功能4.用户消息广播机制5.用户业务封装6.用户在线查询7.修改用户名8.超时强踢9.私聊功能10.完整代码 二、客户端实现1.建立连接2.命令行解析3.…

蓝桥杯省赛无忧 课件41 选择排序

01 选择排序的思想 02 选择排序的实现 03 例题讲解 #include <iostream> using namespace std; void selectionSort(int arr[], int n) {int i, j, min_index;// 移动未排序数组的边界for (i 0; i < n-1; i) {// 找到未排序的部分中最小元素的索引min_index i;for (…

使用Fiddler进行弱网测试

测试APP、web经常需要用到弱网测试&#xff0c;也就是在信号差、网络慢的情况下进行测试。我们自己平常在使用手机APP时&#xff0c;在地铁、电梯、车库等场景经常会遇到会话中断、超时等情况&#xff0c;这种就属于弱网。 普通的弱网测试可以选择第三方工具对带宽、丢包、延时…

自然语言处理--基于HMM+维特比算法的词性标注

自然语言处理作业2--基于HMM维特比算法的词性标注 一、理论描述 词性标注是一种自然语言处理技术&#xff0c;用于识别文本中每个词的词性&#xff0c;例如名词、动词、形容词等&#xff1b; 词性标注也被称为语法标注或词类消疑&#xff0c;是语料库语言学中将语料库内单词…

面试题之RocketMq

1. RocketMq的组成及各自的作用&#xff1f; 在RocketMq中有四个部分组成&#xff0c;分别是Producer&#xff0c;Consumer&#xff0c;Broker&#xff0c;以及NameServer&#xff0c;类比于生活中的邮局&#xff0c;分别是发信者&#xff0c;收信者&#xff0c;负责暂存&#…

和硕拿下AI Pin代工大单公司 | 百能云芯

和硕公司近日成功中标AI Pin代工大单&#xff0c;AI Pin被认为是继iPhone之后的下一个划时代产品&#xff0c;吸引了全球科技圈的广泛关注。和硕公司对此表示&#xff0c;他们不会只专注于单一客户&#xff0c;而是期望在下半年有更多新品上市&#xff0c;为公司带来丰硕的业绩…

creo草绘3个实例学习笔记

creo草绘3个实例 文章目录 creo草绘3个实例草绘01草绘02草绘03 草绘01 草绘02 草绘03