Lỗi khi dùng delete trong hàm hủy

em xóa delete trong lớp HanhKhach thì hết lỗi còn nếu không xóa thì trình biên dịch khi chạy in ra một đống lằng ngoằng. Mong mọi người giúp em
ps: mặc dù em để cout trong hàm hủy mà sao lúc kết thúc chương trình vẫn ko thấy in ra gì ạ.

#include<iostream>
#include<string>
using namespace std;
class date {
    protected:
    int day, month,year;
    public:
    date(){
        day=month=year=0;
    }
~ date(){
        day=month=year=0;
    }
    void nhap(){
        cout <<"nhap ngay: "; cin>>this->day;
        cout << "nhap thang: "; cin>>this->month;
        cout <<"nhap nam: "; cin>> this->year;
    }
    void xuat(){
        cout << "Ngay-thang-nam: "<<this->day<<"-"<<this->month<<"-"<<this->year<<endl;
    }
};
class Vemaybay{
    protected:
    string tenchuyen;
    date ngaybay;
    int giave;
    public:
    Vemaybay(){
        tenchuyen="";
        giave=0;
    }
    ~ Vemaybay(){
        tenchuyen="";
        giave=0;
    }
    void nhap(){
        cout<< "nhap ten chuyen bay: ";fflush(stdin);
       getline(cin,this->tenchuyen);
        cout<< "nhap ngay bay: "<<endl;
        ngaybay.nhap();
        cout<<"nhap gia ve: "; cin>>this->giave;
    }
    void xuat(){
        cout<< "ten chuyen bay: "<<this->tenchuyen<<endl;
        cout<<"ngay bay: ";
        ngaybay.xuat();
        cout<<"gia ve: "<<this->giave<<endl;
    }
    int getgiave(){
        return this->giave;
    }
};
class Nguoi{
    protected:
    string hoten;
    string gioitinh;
    int tuoi;
    public:
    Nguoi(){
        hoten="";
        gioitinh="";
        tuoi=0;
    }
    ~Nguoi(){
        hoten=gioitinh="";
        tuoi=0;
    }
    void nhap(){
        cin.ignore();
        cout<<"ho va ten: "; fflush(stdin);
        getline(cin,this->hoten);
        cout<<"gioi tinh: ";
        getline(cin,this->gioitinh);fflush(stdin);
        cout<<"nhap tuoi: "; cin>>this->tuoi;
    }
    void xuat(){
        cout<<"ho va ten: "<<this->hoten<<endl;
        cout<< "gioi tinh: "<<this->gioitinh<<endl;
        cout<<"tuoi: "<<this->tuoi<<endl;
    }
};
class HanhKhach:public Nguoi
{
   protected:
   Vemaybay *ve;
   int soluong;
   int tongtien;
   public:
   HanhKhach(){
       soluong=0;
       this->ve=new Vemaybay[soluong];
        tongtien=0;
   }
   ~  HanhKhach(){
       soluong=0;
      // delete[] ve;
        tongtien=0;
        cout<<"da bi huy roi nhe"<<endl;
   }
   void nhap(){
       Nguoi :: nhap();
       cout << "nhap so ve mua: "; cin>>this->soluong;
       this->ve=new Vemaybay[this->soluong];
       for(int i=0;i<this->soluong;i++){
           ve[i].nhap();
           tongtien+=ve[i].getgiave();
       }
   }
   void xuat(){
       cout<<"thong tin khach hang: "<<endl;
       Nguoi :: xuat();
       cout<<"thong tin chuyen bay: "<<endl;
       for(int i=0;i<this->soluong;i++){
           ve[i].xuat();
           cout<<endl;
       }
       cout <<"tong tien: "<<this->tongtien<<endl;
   }
    bool operator > ( HanhKhach &ojb){
        if(this->tongtien>ojb.tongtien) return true;
        else return false;
    }
   void swap(HanhKhach &ojb1, HanhKhach &ojb2){
           HanhKhach temp;
           temp=ojb1;
           ojb1=ojb2;
           ojb2=temp;
       }
  
};
void bubblesort(HanhKhach arr[],int n){
       for(int i=0;i<n;i++){
           for(int j=i+1;j<n;j++){
               if(arr[j]>arr[i]) swap(arr[i],arr[j]);
           }
       }
   }
 int main(){
     int n;
     cout<<"nhap so luong khach hang: "; cin>>n;
     HanhKhach *arr=new HanhKhach[n];
    for(int i=0;i<n;i++){
        arr[i].nhap();
    }
   bubblesort(arr,n);
    cout<<"================================================"<<endl;
    for(int i=0;i<n;i++){
        arr[i].xuat();
    }
    return 0;
 }

em post bài xong có đọc lại không vậy?

1 Like
HanhKhach(){
  soluong=0;
  this->ve=new Vemaybay[soluong]; //soluong = ??
  tongtien=0;
}
...
cout << "nhap so ve mua: "; cin>>this->soluong;
this->ve=new Vemaybay[this->soluong]; // leaked

Về destructor, bạn chỉ cần de-allocate dynamic resources trong hàm này thôi, mấy kiểu int, char, float,... thì ko cần đâu.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?