[c++]String字符串类的运算符重载
2015-06-07 12:40
627 查看
在c++中有一个新定义的类型string,可以不用那么麻烦的操作字符串,并且一些高级的运算符重载让她的使用更加便捷
下面是String类的定义和成员函数的定义:
再下面是测试程序:
输入S3以后不知道为什么就成了大写
下面是String类的定义和成员函数的定义:
#ifndef operator_operator_h #define operator_operator_h #include<iostream> #include<string.h> using namespace std; class String { friend ostream& operator<<(ostream &out, const String &s); friend istream& operator>>(istream &in, String &s); public: String(const char *str = NULL) { if(str == NULL) { m_data = new char[1]; m_data[0] = '\0'; } else { m_data = new char[strlen(str)+1]; strcpy(m_data,str); } } String(const String &s) { m_data = new char[strlen(s.m_data)+1]; strcpy(m_data,s.m_data); } String& operator=(const String &s) { if(this != &s) { free(m_data); m_data = new char[strlen(s.m_data)+1]; strcpy(m_data,s.m_data); } return *this; } ~String() { delete []m_data; } friend ostream& operator<<(ostream &out, const String &s) { out<<s.m_data; return out; } friend istream& operator>>(istream &in, String &s) { in>>s.m_data; return in; } String operator+(const String &s); //s = s1 + s2 String operator+=(const String &s); //s1 += s2 char& operator[](int index); bool operator==(String &s); bool operator!=(String &s); bool operator>(String &s); //s1 > s2 bool operator<=(String &s); bool operator<(String &s); bool operator>=(String &s); private: char *m_data; }; String String::operator+=(const String &s) //s1 += s2 { m_data = new char[sizeof(m_data)+1]; strcat(m_data,s.m_data); return *this; } String String::operator+(const String &s)//s = s1 + s2 { strcat(m_data, s.m_data); return *this; } bool String::operator==(String &s) { if(strcmp(m_data, s.m_data)==0) return true; return false; } bool String::operator!=(String &s) { if(strcmp(m_data,s.m_data)!=0) return true; return false; } bool String::operator>(String &s) //s1 > s2 { if(strcmp(m_data,s.m_data)>0) return true; return false; } bool String::operator<=(String &s) { if(strcmp(m_data,s.m_data)>=0) return true; return false; } bool String::operator<(String &s) { if(strcmp(m_data,s.m_data)<0) return true; return false; } bool String::operator>=(String &s) { if(strcmp(m_data,s.m_data)<=0) return true; return false; } char& String::operator[](int index) { // char*p= m_data; // return *(p+index); return m_data[index]; } #endif
再下面是测试程序:
#include "operator.h" int main() { String s("s"); String s1("s"); String s2; //String s3; //s2 = s+s1; //s+=s1; //cout<<"s+=s1="<<s<<endl; cout<<"s[2] = "<<s[0]<<endl; // if(s == s1) // cout<<1<<endl; // if(s != s1) // cout<<0<<endl; // if(s > s1) // cout<<"s>s1"<<endl; // if(s >= s1) // cout<<"s>=s1"<<endl; // if(s < s1) // cout<<"s<s1"<<endl; // if(s <=s1) // cout<<"s<=s1"<<endl; //cin>>s3; //cout<<"s3="<<s3<<endl;//请分开测试 return 0; }
输入S3以后不知道为什么就成了大写
相关文章推荐
- 深入理解C++中public、protected及private用法
- C++程序实例
- chapter11test4
- c++设计模式--解释器模式
- C++ 画星号图形——空心矩形(核心代码记录)
- 设计模式C++实现-解释器模式
- Visual C++剪贴板操作不完全攻略
- 《爱编程,爱c++十二期-第三章-c++函数思想》
- 单例模式——C++实现自动释放单例类的实例
- c和c++关于const的一些区别
- gcc编译C++程序
- HDU-1038-Biker's Trip Odometer(C++ && 提高英语能力!)
- chapter11test3
- C++中this指针的用法详解
- c++实现两个文件的拼接。
- c++虚函数表
- c++虚函数表
- NYOJ 49 开心的小明(01背包问题)
- NYOJ 47 过河问题
- C++模板中的静态