code này thì chạy được nhưng khi hiện ra cái kết quả thì màn hình c nó đơ lại
thêm hàm getch vào thì cũng đc nhưng khi ấn 1 phím khác thì nó lại tiếp tục đơ
em test trên cùng 1 bản dev C++5.3.0.4 nhưng mà win xp thì ngon lành còn win 7 thì cứ bị đơ
#include<iostream>
#include <string.h>
#include<stdio.h>
#include <conio.h>
using namespace std;
class cstring{
char *s;
int dodai;
public:
cstring();
~cstring(){
delete []s;
dodai = 0;
}
void nhap();
void xuat();
friend cstring operator +(const cstring a,const cstring b);
const cstring& operator=(const cstring &);
friend cstring operator +=(cstring a, const cstring b);
};
cstring::cstring()
{
dodai=0;
s=NULL;
}
void cstring::nhap()
{
cout<<"Nhap do dai: ";
cin>>dodai;
fflush(stdin);
s = new char[dodai+1];
cout<<"nhap chuoi: ";
cin.get(s,dodai+1);
cin.ignore();
}
void cstring::xuat()
{
cout<<s;
}
const cstring& cstring::operator=(const cstring &a)
{
dodai=a.dodai;
delete []s;
s = new char[dodai+1];
strcpy(s,a.s);
return *this;
}
cstring operator+(const cstring a,const cstring b)
{
cstring tam;
delete []tam.s;
tam.s=new char[a.dodai+b.dodai+1];
strcpy(tam.s,a.s);
strcat(tam.s,b.s);
return tam;
}
cstring operator +=(cstring a, const cstring b)
{
cstring tam;
tam= a+b;
a=tam;
return a;
}
main()
{
cstring a,b,c;
a.nhap();
cout<<"a=";
a.xuat();
cout<<endl;
b.nhap();
cout<<"b=";
b.xuat();
cout<<endl<<"gan c=a"<<endl<<"c=";
c=a;
c.xuat();
cout<<endl;
// a+=b;
// cout<<"a+=b =";
// a.xuat();
c=a+b;
cout<<"a+b=c=";
c.xuat();
cout<<endl;
getch();
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?