[转 http://www.dogame.com.cn/gamerc/jc_show.php?id=6051】
main函数:
#include <iostream>
#include "strings.h"
using namespace std;
int main(){
Strings s3;
Strings s4;
Strings s5;
s3.display();
Strings s1("hello");
s1.display();
Strings s2(s1);
s2.display();
s3 = s2;
s3.display();
s4 = s1+s2;
s4.display();
s5 = s4 + "good";
s5.display();
return 0;
}
strings.h函数
#ifndef _STRINGS_
#define _STRINGS_
#include<iostream>
using namespace std;
class Strings{
public:
Strings();
Strings(char *s);
Strings(const Strings &s);
Strings& operator = (const Strings& s);
Strings& operator + (const Strings& s);
Strings& operator + (const char *s);
~Strings(){
if(str != NULL)
delete [] str;
cout<<"~Strings() is called"<<endl;
}
display();
private:
char *str;
};
#endif
strings.cpp函数
#include "strings.h"
#include <iostream>
using namespace std;
Strings::Strings(){ // 默认构造函数
str = new char('A'); //初试化为字符A
//str = "nothing"; 试试如此初试话 :-) 呵呵
cout<<"Strings is called"<<endl;
}
Strings::Strings(char *s){
str = new char[strlen(s) + 1]; // 带一个参数的构造函数
if(str)
strcpy(str, s);
cout<<"Strings(char *s) is called"<<endl;
}
Strings::Strings(const Strings &s){ // copy构造函数
str = new char[strlen(s.str) + 1];
if(str)
strcpy(str, s.str);
cout<<"Strings(const Strings &s) is called "<<endl;
}
Strings& Strings::operator = (const Strings &s){ // 赋值运算
if(this != &s){ // 如果str不是它本身
if(str != NULL)
delete [] str; //防止内存泄露
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
cout<<"Strings(const Strings &s) is called "<<endl;
}
return *this;
}
Strings& Strings::operator + (const Strings& s){ // 加法运算
char * temp;
temp = new char[strlen(str)+strlen(s.str)+1]; //需要重新申请内存,原来的内存不够
strcpy(temp, str);
delete [] str;
strcat(temp,s.str);
str = temp;
return *this;
}
Strings& Strings::operator + (const char *s){ // 加法运算
char * temp;
temp = new char[strlen(str)+strlen(s)+1];
strcpy(temp, str);
delete [] str;
strcat(temp,s);
str = temp;
return *this;
}
Strings::display(){ // 显示函数
char *p=str;
while(*p != '\0'){
cout<<*p;
p ++;
}
cout<<endl;
cout<<"display is called "<<endl;
}
分享到:
相关推荐
必须实现如下操作,字符串比较、求串的长度、判断串是否为空、将串置空、字符串赋值(包括两个字符串类复制,一个字符串赋值到CmyString对象)、求字符串中的一个字符或改变字符串中的一个字符(采用重载[]),完成...
用C++实现的一个简单的字符串类 解压直接可运行的一个VS2008的工程文件
基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用基于Keil实现字符串函数string.h的简单应用...
一个简单的字符串加密解密算法,使用C#语言实现。。。
此课程设计的研究对象是字符串,研究的目的就是对字符串进行如下五项基本功能的实现,使大家理解并熟练掌握用汇编语言实施对字符串的操作。基本功能主要有:1.字符串输入操作。此功能地实现主要是通过DOS的0AH号功能...
jquery加密字符串。常用来处理密码,只需引入,然后按照实例去使用
很简单的程序,主要实现字符串的查找替换删除插入等功能,初始版本,有bug,功能不完善。VS2008
本文实例为大家分享了python实现字符串加密成纯数字的具体代码,供大家参考,具体内容如下 说明: 该加密算法仅仅是做一个简单的加密,安全性就不谈了,哈哈. 算法流程: 1.字符串以utf8编码成字节数组 2.把每一个...
可导入文件,实现内容转换ASCII,转成大写,转成小写,保存,替换,统计的小工具,很实用哦!
在VB.NET下利用Socket实现简单的字符串发送接收程序
是个简单的C语言写的递归实现字符串反向输出。
用指针计算字符串长度,简单的小程序,c语言
java实现的md5加密实现类对所有的字符串进行加密,加密后生成一串加密后的字符串。
这段代码实现了一个简单的文本处理工具TextProcessor,其中使用了Java的字符串操作。在类的构造方法中,首先接收一个文本字符串作为输入。然后实现了两个方法:countWords()和replaceWord()。countWords()方法用于...
自己封装的一些简单函数呵呵呵,主要是关于字符串的,自己用的!
使用eclipse实现Android多线程下的简单字符串计数器
字符串加密,简单算法实现字符串加密
这是一个用Java编写的简易字符串加密文件,可以对你输入的任何字符串实现加密解密功能
本代码主要利用MATLAB工具实现MATLAB 将数值嵌入字符串示例,简单明了,易于理解