Code:
class nguoi
{
protected:
char *hoten;
int namsinh;
public:
nguoi(){hoten="default",namsinh=1977;}
nguoi(char *ht,int ns):namsinh(ns){hoten=strdup(ht);}
virtual void xuat()const {cout<<"nguoi an com"<<endl;}
virtual nguoi* khoitao(){return new nguoi(*this);}
};
////////////////////////////////////////////////////////////////////////////
class sinhvien:public nguoi
{
protected:
char *maso;
public:
sinhvien(char *n,char *ms,int ns):nguoi(n,ns){maso=strdup(ms);}
void xuat()const {cout<<"sv an xoi"<<endl;}
virtual nguoi* khoitao(){return new sinhvien(*this);}
};
/////////////////////////////////////////////////////////////////////////////
class congnhan:public nguoi
{
protected:
double luong;
public:
congnhan(char *n,int ns,double l):nguoi(n,ns),luong(l){}
void xuat()const {cout<<"cong nhan an chao"<<endl;}
virtual nguoi* khoitao(){return new congnhan(*this);}
};
void xuatds(nguoi *an[],int n)
{
for(int i=0;i<3;i++)
{
an[i]->xuat();
}
}
void main()
{
nguoi *a[3];
nguoi *c=new nguoi("VietCong",1945);
a[0]=new sinhvien("NguyenVanA","06666",1999);
a[1]=new nguoi("NhaThai",1989);
a[2]=new congnhan("LyCuaBo",1988,2000);
xuatds(a,3);
}
Theo mình được biết thì đây là phương thức thiết lập ảo.Nhưng không hiểu chỗ tại sao cái dòng màu tím ấy không ghi được như thế này:
Code:
virtual congnhan* khoitao(){return new congnhan(*this);}
Nếu mình thay dòng màu tím thành dòng trên thì nó báo lỗi
Code:
error C2555: 'congnhan::khoitao' : overriding virtual function differs from 'nguoi::khoitao' only by return type or calling convention