运算符重载实现字符串串接
来源:互联网 发布:spark sql 数据仓库 编辑:程序博客网 时间:2024/05/16 02:11
/*建立一个类 String,连接两个字符串后。具体要求:(1)私有数据成员 int Length; //字符串的长度 char *s; //指向字符串的指针(2)公有成员 构造函数 //缺省参数的构造函数、以对象作为参数构造函数、以一个字符串常量作为参数的构造函数 析构函数 拷贝构造函数 String operator +(String &); //重载“+”运算符 String operator =(String &); //重载“=”运算符 void show(); //输出两个字符串和结果字符*/#include<iostream>using namespace std;class String{private: int length; char *s;public: String(); String(String &); String(char *); String operator + (String &); String operator = (String &); void show(); ~String();};String::String(){ length = 0; s = new char; s = NULL;}String::String(String &string){ s = new char; this->length = string.length; char *str_s = s; while (*string.s) { *str_s = *string.s; str_s++; string.s++; } *str_s = '\0';}String::String(char *temp){ this->length = strlen(temp); s = new char; char *ptr = s; while (*temp) { *ptr = *temp; ptr++; temp++; } *ptr = '\0';}String String::operator+(String &string){ char *temp_s1 = this->s; char *temp_s2 = string.s; while (*temp_s1) temp_s1++; while (*temp_s2) { *temp_s1 = *temp_s2; temp_s1++; temp_s2++; } *temp_s1 = '\0'; return *this;}String String::operator=(String &string){ this->length = string.length; char *this_s = this->s, *string_s = string.s; while (*string_s) { *this_s = *string_s; this_s++; string_s++; } *this_s = '\0'; return *this;}void String::show(){ cout << "长度为:" << length << endl; cout << "字符串为:" << s;}String::~String(){ delete s;}int main(){ char a[] = "abcdefg", b[] = "ABCDEFG"; String str1(a); String str2(str1); String str3 = str2 + str1; str3.show(); system("pause"); return 0;}
阅读全文
0 0
- 运算符重载实现字符串串接
- 运算符重载实现字符串串接
- 简单实现运算符重载
- 通过运算符重载实现复数运算
- ”^“运算符重载实现幂指数运算
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 汇编字符串串操作指令
- 巨型整数运算符重载实现部分
- 运算符重载(实现CString类)
- 矩阵类的实现,运算符重载
- 实现复数类中的运算符重载
- 实现Time类中的运算符重载
- 用运算符重载实现复数相加
- C++实现的复数运算符重载
- 实现复数类的运算符重载
- 实现负数中的运算符的重载
- 实现复数类中的运算符重载
- 实现复数类中的运算符重载
- php 数组元素快速去重
- oracle 11g的安装过程
- caffe中forward过程总结(1)
- 人工智能对人们生活的影响。
- GiT 常用操作
- 运算符重载实现字符串串接
- 决策树算法介绍与代码编写
- 通用无限分类的方法
- JS学习(9)----JS Functions
- HDU5135Little Zu Chongzhi's Triangles(状态压缩DP)
- Javascript函数绑定
- 前端div的弹出,html弹窗思路
- I
- 从单一WAR到多活, 记述一个创业公司的架构演变