使用string.h创建简单的String类

时间:2015-12-04 14:11:24

标签: c++ string

我必须使用string.h构建一个String类(当你看到代码时你就会明白)。请原谅我可怜的英语。 我不知道问题出在哪里。它只是在没有任何错误消息的情况下崩溃。

String.h

#ifndef _STRING_H
#define _STRING_H

class String{
public:
    char* s1;
    char* s2;
    char* stemp;


    //The Constructors
    String();
    String(char* so);
    String(char* so, char* st);

    //Member Functions
    int slength(char* s1);  //Calculate the length of Sting
    void scopy(char* so,const char* st);    // copy string value to another String


};



#endif // _STRING_H

String.cpp

#include <string.h>
#include "String.h"

//The Constructors
String::String(){}

String::String(char* so){
    s1 = so;
}
String::String(char* so, char* st){
    s1 = so;
    s2 = st;
}


//Member Functions

int String::slength(char* so){
    return strlen(so);
}

void String::scopy(char* so,const char* st){

    strcpy(so,st);
}

main.cpp

#include <iostream>
#include "String.h"


using namespace std;

int main()
{

    String str("Hello","World");


    cout<<"The First  String is : "<<str.s1<<endl;
    cout<<"The Second String is : "<<str.s2<<endl;
    cout<<"-------------------------------------"<<endl;

    cout<<"The First String contains "<<str.slength(str.s1)<<" Letters"<<endl;
    cout<<"The Second String contains "<<str.slength(str.s2)<<" Letters"<<endl;

    cout<<"-------------------------------------"<<endl;

    cout<<"Copying The Second String in the First String . . ."<<endl;
    str.scopy(str.s1,str.s2);
    cout<<"The First  String is : "<<str.s1<<endl;
    cout<<"The Second String is : "<<str.s2<<endl;


    return 0;
}

2 个答案:

答案 0 :(得分:3)

字符串文字已分配给str.s1,并在strcpy中作为String::scopy()的第一个参数传递。这非常糟糕,因为不允许修改字符串文字。

答案 1 :(得分:0)

我刚刚解决了问题

(STRING.H)

#ifndef STRING_H
#define STRING_H

class String{
    public:
        //Data Members
        char* s1;
        int len ;

        //The Constructors
        String();
        String(char* so);

        //Member Functions
        int slength(String &s1);  //Calculate the length of Sting
        void scopy(String &so,String &st);    // copy string value to another String
        int scompare(String &so, String &st); // compare two string and returns which one is bigger
        void scombine(String &so, String &st);    // combine two strings into one string
        //The Destructor
        ~String();

};



#endif // STRING_H

(String.cpp)

#include <string.h>
#include "String.h"

//The Constructors
String::String(){
    len = 0;
    s1 = new char[len + 1];
}

String::String(char* so){
    len = strlen(so);
    s1 =  new char[len + 1];
    strcpy(s1, so);

}


//Member Functions

int String::slength(String &so){
    return strlen(so.s1);
}

void String::scopy(String &so,String &st){

    strcpy(so.s1,st.s1);
}

int String::scompare(String &so, String &st){
    return strcmp(so.s1,st.s1);

}


void String::scombine(String &so, String &st){
    len = so.len + st.len;
    delete s1;
    s1 = new char[len + 1];
    strcpy(s1,so.s1);
    strcat(s1,st.s1);

}


//The Destructor
String::~String(){}

(main.cpp中)

#include <iostream>
#include "String.h"


using namespace std;

int main()
{

String str1("Hello");
String str2("World");
String str;


cout<<endl<<"The First  String is : "<<str1.s1<<endl;
cout<<"The Second String is : "<<str2.s1<<endl;
cout<<endl<<"-------------------------------------"<<endl;

cout<<endl<<"The First  String contains "<<str.slength(str1)<<" Letters"<<endl;
cout<<"The Second String contains "<<str.slength(str2)<<" Letters"<<endl;

cout<<endl<<"-------------------------------------"<<endl;

cout<<endl<<"After Comparing the Two Strings we Find out That"<<endl;
switch(str.scompare(str1,str2)){
    case 1:
        cout<<"The First String is Bigger than the Second String";
        break;
    case 0:
        cout<<"The two Strings are the equal";
        break;
    default:
        cout<<"The Second String is Bigger than the First String";
}

cout<<endl<<endl<<"-------------------------------------"<<endl;

cout<<endl<<"After Combining The two Strings"<<endl;
str.scombine(str1,str2);
cout<<str.s1<<endl;


cout<<endl<<"-------------------------------------"<<endl;

cout<<endl<<"After copying The Second String in the First String . . ."<<endl;
str.scopy(str1,str2);
cout<<"The First  String is : "<<str1.s1<<endl;
cout<<"The Second String is : "<<str2.s1<<endl;


    return 0;
}