I have to build a String class using string.h
(you will understand when you see the code). Please forgive my poor English.
I don't know where the problem is. It just crashes without any error messages.
(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;
}