sau khi viết code xong, debug thì không hiểu chuyện gì xảy ra, ace nào xem và hướng dẩn giúp em với thanks!
code of em đây
#include <iostream>
using namespace std;
class String
{
private :
char* Str;
int n;
public:
String();
~String();
String operator +(const String &) const;
friend bool operator ==(const String &, const String &) ;
friend bool operator !=(const String &, const String &);
friend istream & operator >>(istream & , String & );
friend ostream & operator <<(ostream &, const String &);
};
void main()
{
String x, y, z;
cin>>x;
cin>>y;
x + y;
cout<<"chuoi tong: "<< x;
/*if(x==y)
cout << " chuoi x:' "<< x << " ' bang " << "chuoi y: ' "<<y<<" '";
if(x!=y)
cout << " chuoi x:' "<< x << " ' khac " << "chuoi y: ' "<<y<<" '";*/
}
String :: String()
{
Str = new char[400];
n = 0;
if(Str == NULL)
{
cout<<"cap phat that bai";
exit(0);
}
}
String ::~String()
{
delete [] Str;
}
String String :: operator +(const String & x) const
{
String z;
z.n = this->n+x.n;
for(int i=0; i < this->n; i++)
{
z.Str[i]=this->Str[i];
}
for(int i=0; i < x.n; i++)
z.Str[i+this->n] = x.Str[i];
cout<<z;
return z;
}
bool operator ==(const String & x, const String & y)
{
if(x.n != y.n)
return false;
for(int i=0; i<x.n;i++)
{
if(x.Str[i] != y.Str[i])
return false;
}
return true;
}
bool operator !=(const String & x, const String & y)
{
if( x==y)
return false;
return true;
}
istream & operator >>(istream & in_put , String & x )
{
in_put.getline(x.Str,400);
x.n = strlen(x.Str);
return in_put;
}
ostream & operator <<(ostream & out, const String & x)
{
for(int i=0;i<x.n;i++)
out << x.Str[i];
out << "\n" ;
return out;
}
code of em đây
#include <iostream>
using namespace std;
class String
{
private :
char* Str;
int n;
public:
String();
~String();
String operator +(const String &) const;
friend bool operator ==(const String &, const String &) ;
friend bool operator !=(const String &, const String &);
friend istream & operator >>(istream & , String & );
friend ostream & operator <<(ostream &, const String &);
};
void main()
{
String x, y, z;
cin>>x;
cin>>y;
x + y;
cout<<"chuoi tong: "<< x;
/*if(x==y)
cout << " chuoi x:' "<< x << " ' bang " << "chuoi y: ' "<<y<<" '";
if(x!=y)
cout << " chuoi x:' "<< x << " ' khac " << "chuoi y: ' "<<y<<" '";*/
}
String :: String()
{
Str = new char[400];
n = 0;
if(Str == NULL)
{
cout<<"cap phat that bai";
exit(0);
}
}
String ::~String()
{
delete [] Str;
}
String String :: operator +(const String & x) const
{
String z;
z.n = this->n+x.n;
for(int i=0; i < this->n; i++)
{
z.Str[i]=this->Str[i];
}
for(int i=0; i < x.n; i++)
z.Str[i+this->n] = x.Str[i];
cout<<z;
return z;
}
bool operator ==(const String & x, const String & y)
{
if(x.n != y.n)
return false;
for(int i=0; i<x.n;i++)
{
if(x.Str[i] != y.Str[i])
return false;
}
return true;
}
bool operator !=(const String & x, const String & y)
{
if( x==y)
return false;
return true;
}
istream & operator >>(istream & in_put , String & x )
{
in_put.getline(x.Str,400);
x.n = strlen(x.Str);
return in_put;
}
ostream & operator <<(ostream & out, const String & x)
{
for(int i=0;i<x.n;i++)
out << x.Str[i];
out << "\n" ;
return out;
}
Comment