Code:
#include <iostream> //#include <conio.h> #include <string.h> using namespace std; class SINHVIEN { private: char *hoten; protected: int sbd; public: void get_name(void); void get_sbd(void); void put_name(void) { cout<<"hoten: "<<hoten<<endl; } void put_sbd(void) { cout<<"sbd: "<<sbd<<endl; } }; void SINHVIEN::get_name() { char S[30]; cout<<"nhap ho ten: "; gets(S); int k=strlen(S); hoten = new char[k+1]; strcpy(hoten,S); } void SINHVIEN::get_sbd(void) { int x; cout<<"nhap so bao danh: "; cin>>x; sbd=x; } class DIEMTHI:public SINHVIEN { protected: float mon1,mon2; public: void get_diem(void); void put_diem(void); }; void DIEMTHI::get_diem() { float m1,m2; cout<<"nhap diem mon 1:"; cin>>m1; mon1=m1; cout<<"nhap diem mon 2:"; cin>>m2; mon2=m2; } void DIEMTHI::put_diem() { cout<<"mon1: "<<mon1<<endl; cout<<"mon2: "<<mon2<<endl; } class UUTIEN { protected: float u; public: void get_ut(void); void put_ut(void) { cout<<"uu tien: "<<u<<endl; } }; void UUTIEN::get_ut() { float u1; cout<<"nhap diem uu tien: "; cin>>u1; u=u1; } class KETQUA:public DIEMTHI,public UUTIEN { private: float tong; public: friend ostream& operator <<(ostream&, KETQUA &); }; ostream& operator << (ostream& dout, KETQUA& k) { k.tong=k.mon1+k.mon2+k.u; k.put_name(); k.put_sbd(); k.put_diem(); k.put_ut(); dout<<"tong diem: "<<k.tong<<endl; return dout; } int main() { const int n = 2; KETQUA S[n]; int i; for(i=0;i<n;i++) { S[i].get_name(); S[i].get_sbd(); S[i].get_diem(); S[i].get_ut(); } for(i=0;i<n;i++) cout<<S[i]; //getche(); return 0; }
lôi.png
Comment