主题:类类类
程序能编译,但运行时出现
after normal block的错误,不懂???
#include <iostream.h>
#include <cstring>
#include <cstdlib>
#include <cstddef>
class stringvar
{ public:
void operator=(const stringvar &amount1);
stringvar();
stringvar(int size);
stringvar(const char a[]);
stringvar(const stringvar& string_object);
~stringvar();
int length() const;
void input_line(istream& ins);
friend ostream& operator<<(ostream& outs,const stringvar& the_string);
friend stringvar copy_piece(const stringvar &amount,int i,int j);
friend char one_char(const stringvar &amount,int k);
friend stringvar set_char(const stringvar &amount,char x1,char x2);
friend bool operator==(const stringvar &amount1,const stringvar &amount2);
friend stringvar operator+(const stringvar &amount1,const stringvar &amount2);
friend istream& operator>>(istream& ins,const stringvar& the_string);
private:
char *value;
int max_length;
};
void conversation(int max_name_size);
void main()
{ conversation(100);
cout<<"End of demonstration!\n";
}
void conversation(int max_name_size)
{
stringvar name1(max_name_size),name2("Borg");
cout<<"what is your name?\n";
name1.input_line(cin);
cout<<"we are "<<name1<<endl;
cout<<"we will meet again "<<name2<<endl;
stringvar name3,name11;
char name;
name3=copy_piece(name1,1,3);
cout<<"name3 is "<<name3<<endl;
name=one_char(name1,2);
cout<<"name is "<<name<<endl;
name11=set_char(name1,'x','y');
cout<<"name11 is "<<name11<<endl;
stringvar name4("abcdefg");
stringvar name5("abcd");
if(name4==name5)
cout<<"name4 is equal to name5!\n";
stringvar name6,name7;
name6=name4;
cout<<"name6 ="<<name6<<endl;
name7=name4+name5;
cout<<"name7 ="<<name7<<endl;
}
stringvar::stringvar():max_length(100)
{value=new char[max_length+1];
value[0]='\0';
}
stringvar::stringvar(int size):max_length(size)
{value=new char[max_length+1];
value[0]='\0';
}
stringvar::stringvar(const char a[]):max_length(strlen(a))
{value=new char[max_length+1];
strcpy(value,a);
}
stringvar::stringvar(const stringvar& string_object):max_length(string_object.length())
{value=new char[max_length+1];
strcpy(value,string_object.value);
}
stringvar::~stringvar()
{delete [] value;
}
int stringvar::length() const
{ return strlen(value);
}
void stringvar::input_line(istream& ins)
{ ins.getline(value,max_length+1);
}
ostream& operator<<(ostream& outs,const stringvar& the_string)
{ outs<<the_string.value;
return outs;
}
stringvar copy_piece(const stringvar &amount,int i,int j)
{int k,l;
stringvar temp;
for(l=0,k=i-1;k<=j-1;l++,k++)
temp.value[l]=amount.value[k];
return temp;
}
char one_char(const stringvar &amount,int k)
{char ch;
ch=amount.value[k-1];
return ch;
}
stringvar set_char(const stringvar &amount,char x1,char x2)
{ int i;
for(i=0;i<amount.max_length;i++)
if(amount.value[i]==x1)
amount.value[i]=x2;
return amount;
}
bool operator==(const stringvar &amount1,const stringvar &amount2)
{int i;
if(amount1.length()>=amount2.length())
i=amount2.length();
else
i=amount1.length();
return (strncmp(amount1.value,amount2.value,i)==0);
}
stringvar operator+(const stringvar &amount1,const stringvar &amount2)
{strcat(amount1.value,amount2.value);
return amount1;
}
istream& operator>>(istream& ins,const stringvar& the_string)
{ int i;
for(i=0;i<the_string.length();i++)
ins>>the_string.value[i];
return ins;
}
void stringvar::operator=(const stringvar &amount1)
{ int new_length=strlen(amount1.value);
if(new_length>max_length)
{
delete [] value;
max_length=new_length;
value=new char[max_length+1];
}
for(int i=0;i<new_length;i++)
value[i]=amount1.value[i];
value[new_length]='\0';
}
after normal block的错误,不懂???
#include <iostream.h>
#include <cstring>
#include <cstdlib>
#include <cstddef>
class stringvar
{ public:
void operator=(const stringvar &amount1);
stringvar();
stringvar(int size);
stringvar(const char a[]);
stringvar(const stringvar& string_object);
~stringvar();
int length() const;
void input_line(istream& ins);
friend ostream& operator<<(ostream& outs,const stringvar& the_string);
friend stringvar copy_piece(const stringvar &amount,int i,int j);
friend char one_char(const stringvar &amount,int k);
friend stringvar set_char(const stringvar &amount,char x1,char x2);
friend bool operator==(const stringvar &amount1,const stringvar &amount2);
friend stringvar operator+(const stringvar &amount1,const stringvar &amount2);
friend istream& operator>>(istream& ins,const stringvar& the_string);
private:
char *value;
int max_length;
};
void conversation(int max_name_size);
void main()
{ conversation(100);
cout<<"End of demonstration!\n";
}
void conversation(int max_name_size)
{
stringvar name1(max_name_size),name2("Borg");
cout<<"what is your name?\n";
name1.input_line(cin);
cout<<"we are "<<name1<<endl;
cout<<"we will meet again "<<name2<<endl;
stringvar name3,name11;
char name;
name3=copy_piece(name1,1,3);
cout<<"name3 is "<<name3<<endl;
name=one_char(name1,2);
cout<<"name is "<<name<<endl;
name11=set_char(name1,'x','y');
cout<<"name11 is "<<name11<<endl;
stringvar name4("abcdefg");
stringvar name5("abcd");
if(name4==name5)
cout<<"name4 is equal to name5!\n";
stringvar name6,name7;
name6=name4;
cout<<"name6 ="<<name6<<endl;
name7=name4+name5;
cout<<"name7 ="<<name7<<endl;
}
stringvar::stringvar():max_length(100)
{value=new char[max_length+1];
value[0]='\0';
}
stringvar::stringvar(int size):max_length(size)
{value=new char[max_length+1];
value[0]='\0';
}
stringvar::stringvar(const char a[]):max_length(strlen(a))
{value=new char[max_length+1];
strcpy(value,a);
}
stringvar::stringvar(const stringvar& string_object):max_length(string_object.length())
{value=new char[max_length+1];
strcpy(value,string_object.value);
}
stringvar::~stringvar()
{delete [] value;
}
int stringvar::length() const
{ return strlen(value);
}
void stringvar::input_line(istream& ins)
{ ins.getline(value,max_length+1);
}
ostream& operator<<(ostream& outs,const stringvar& the_string)
{ outs<<the_string.value;
return outs;
}
stringvar copy_piece(const stringvar &amount,int i,int j)
{int k,l;
stringvar temp;
for(l=0,k=i-1;k<=j-1;l++,k++)
temp.value[l]=amount.value[k];
return temp;
}
char one_char(const stringvar &amount,int k)
{char ch;
ch=amount.value[k-1];
return ch;
}
stringvar set_char(const stringvar &amount,char x1,char x2)
{ int i;
for(i=0;i<amount.max_length;i++)
if(amount.value[i]==x1)
amount.value[i]=x2;
return amount;
}
bool operator==(const stringvar &amount1,const stringvar &amount2)
{int i;
if(amount1.length()>=amount2.length())
i=amount2.length();
else
i=amount1.length();
return (strncmp(amount1.value,amount2.value,i)==0);
}
stringvar operator+(const stringvar &amount1,const stringvar &amount2)
{strcat(amount1.value,amount2.value);
return amount1;
}
istream& operator>>(istream& ins,const stringvar& the_string)
{ int i;
for(i=0;i<the_string.length();i++)
ins>>the_string.value[i];
return ins;
}
void stringvar::operator=(const stringvar &amount1)
{ int new_length=strlen(amount1.value);
if(new_length>max_length)
{
delete [] value;
max_length=new_length;
value=new char[max_length+1];
}
for(int i=0;i<new_length;i++)
value[i]=amount1.value[i];
value[new_length]='\0';
}