em có một lớp kế thừa ,đã định nghĩa hàm friend nhập ,xuất cho lớp cơ sở ,sau đó em tái sử dụng cho lớp kế thừa mà không biết nó sai chỗ nào !ai biết thì giúp em với :
Code:
#include"iostream"
#include"string"
using namespace std;
class CNguoi
{
protected:
char m_strHoTen[30];
int m_iTuoi;
char m_strDiaChi[30];
public:
friend istream & operator>>(istream & is,CNguoi& x)
{
fflush(stdin);
cout<<"Nhap ten :";
is.getline(x.m_strHoTen,30);
cout<<"nhap tuoi :";
is>>x.m_iTuoi;
fflush(stdin);
cout<<"Nhap dia chi :";
is.getline(x.m_strDiaChi,30);
return is;
}
friend ostream & operator<<(ostream &os,CNguoi &x)
{
os<<"Ten :"<<x.m_strHoTen<<endl;
os<<"Tuoi :"<<x.m_iTuoi<<endl;
os<<"Dia chi :"<<x.m_strDiaChi;
return os;
}
};
class CHocSinh :public CNguoi
{
protected:
float m_fToan;
float m_fVan;
float m_fDTB;
public:
friend istream & operator>>(istream &is,CHocSinh &x)
{
is>>(CNguoi)x;//thay bang cin>>(CNguoi)x cung khong duoc
cout<<"Nhap diem toan :";
is>>x.m_fToan;
cout<<"Nhap diem van :";
is>>x.m_fVan;
x.m_fDTB = (x.m_fVan + x.m_fToan )/2;
return is;
}
friend ostream & operator<<(ostream &os,CHocSinh &x)
{
os<<(CNguoi)x;//thay bang cout<<(CNguoi)x cung khong duoc
os<<"ho ten :"<<x.m_strHoTen<<endl;
os<<"toan :"<<x.m_fToan<<" van :"<<x.m_fVan;
os<<" DTB :"<<x.m_fDTB;
return os;
}
};
void main()
{
CHocSinh obj;
cin>>obj;
cout<<obj;
}
Hi vọng và có lẽ không thất vọng 