C++实现一个自己的string类

string2.h

#ifndef STRING2_H_
#define STRING2_H_
#include<iostream>
using std::ostream;
using std::istream;
class String{
    
    
private:
    char *str;
    int len;
    static int num_strings;
    static const int Cinlim = 80;
public:
//构造函数
    String(const char *s);
    String();
    String(const String &);
    ~String();
    int length() const{
    
    return len;}
//重载运算符
    String & operator = (const String &);
    String & operator = (const char *);
    char & operator[](int i);
    const char & operator[](int i) const;
   
//重载比较符
    friend bool operator<(const String &st,const String &st2);
    friend bool operator>(const String &st1,const String &st2);
    friend bool operator==(const String &st,const String &st2);
//重载输入输出
    friend ostream & operator <<(ostream & os,const String &st);
    friend istream & operator>>(istream & is,String & st);
//静态函数
    static int HowMany();
//新增功能

    friend String operator+(const String&s1,const String &s2) ;
    String  operator+(const char *) const;
    void StringLow();
    void StringHigh();
    const int HowManyChar(const char &c);

};
#endif

string2.cpp

//在两个字符串相加时应该开辟一个空间,不然运行不出来
//字符串string型相加时,最好用友元函数,不然main函数的s2="My name is"+s3会报错,显示“没有这个运算符”
#include<cstring>
#include<cctype>
#include"string2.h"
using std::cout;
using std::cin;
int String::num_strings=0;
//静态方法
int String::HowMany(){
    
    
    return num_strings;
}

//类方法
String::String(const char *s){
    
    
    len=std::strlen(s);
    str=new char[len+1];
    std::strcpy(str,s);
    num_strings++;
}

String::String(){
    
    
    len=4;
    //书上给的方法
    str=new char[1];
    str[0]='\0';
    num_strings++;

}

String::String(const String&st){
    
    
    num_strings++;
    len=st.len;
    str=new char[len+1];
    std::strcpy(str,st.str);

}

String::~String(){
    
    
    --num_strings;
    delete [] str;
}

//重载运算符
String &String::operator=(const String & st){
    
    
    if(this==&st){
    
    
        return *this;
    }
    delete [] str;
    len=st.len;
    str=new char[len+1];
    std::strcpy(str,st.str);
    return *this;
}

String &String::operator=(const char * s){
    
    
    delete[] str;
    len = std::strlen(s);
    str=new char[len+1];
    std::strcpy(str,s);
    return *this;
}

char & String::operator[](int i){
    
    
    return str[i];
}
const char & String::operator[](int i) const{
    
    
    return str[i];
}



//重载关系比较符
bool operator<(const String &st1,const String &st2){
    
    
    return (std::strcmp(st1.str,st2.str)<0);
}

bool operator>(const String &st1,const String &st2){
    
    
    return st2<st1;
}

bool operator==(const String &st1,const String &st2){
    
    
    return (std::strcmp(st1.str,st2.str)==0);
}
//重载输入输出
ostream & operator<<(ostream &os,const String &st){
    
    
    os<<st.str;
    return os;
}

istream & operator>>(istream & is,String &st){
    
    
    char temp[String::Cinlim];
    is.get(temp,String::Cinlim);
    if(is){
    
    
        st=temp;
    }
    while(is&&is.get()!='\n'){
    
    
        continue;
    }
    return is;
}

String operator+(const String&s1,const String &s2)  {
    
    
    char *temp=new char[s1.len+s2.len+1];
    strcpy_s(temp,s1.len+1,s1.str);
    strcpy_s(temp+s1.len,s2.len+1,s2.str);
    temp[s1.len+s2.len]='\0';
    return String(temp);
}
String String::operator+(const char *s) const{
    
    
    int length=len+strlen(s);
    char *temp=new char[length+1];
    strcpy_s(temp,len+1,str);
    strcpy_s(temp+len,std::strlen(s)+1,s);
    temp[length]='\0';
    return String(temp);
}


void String::StringLow(){
    
    
    for(int i=0;i<len;i++){
    
    
        if(str[i]>='A'&&str[i]<='Z'){
    
    
            str[i]=tolower(str[i]);
        }
    }
}

void String::StringHigh(){
    
    
    for(int i=0;i<len;i++){
    
    
        if(str[i]>='a'&&str[i]<='z'){
    
    
            str[i]=toupper(str[i]);
        }
    }
}

const int String::HowManyChar(const char &c){
    
    
    int sum=0;
    for(int i=0;i<len;i++){
    
    
        if(str[i]==c){
    
    
                sum++;
        }
    }
    return sum;
}

main.cpp

#include<iostream>
using namespace std;
#include"string2.h"
int main(){
    
    
    String s1(" And I am a C++ Student.");
    String s2="Please enter your name: ";
    String s3;
    cout<<s2;
    cin>>s3;
    s2="My name is "+s3;
    cout<<s2<<".\n";
    s2=s2+s1;
    s2.StringHigh();
    cout<<"The string\n"<<s2<<"\ncontains"<<s2.HowManyChar('A')<<"'A' in it.\n";
    s1="red";
    String rgb[3]={
    
    String(s1),String("green"),String("blue")};
    cout<<"Enter the name of a primary color for mixing light: ";
    String ans;
    bool succees=false;
    while(cin>>ans){
    
    
        ans.StringLow();
        for(int i=0;i<3;i++){
    
    
            if(ans==rgb[i]){
    
    
                cout<<"That's right!"<<endl;
                succees=true;
                break;
            }
        }
        if(succees){
    
    
            break;
        }else{
    
    
            cout<<"Try again!"<<endl;
        }
    }
    cout<<"bye!"<<endl;
    return 0;
    

}

测试结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qaaaaaaz/article/details/130329796