Trang

Thứ Hai, 28 tháng 3, 2011

Hàm friend của hai lớp

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
class DIEM;
class SINHVIEN
{ char hoten[25];
 int tuoi;
 public:
        int masv;
        char *gethoten(){return hoten;}
 friend void  nhapdiem(SINHVIEN s, DIEM &d);
 friend void  xemdiem(SINHVIEN s, DIEM &d);
 void nhap(void)
  {
  cout <<"\n nhap ho ten "; gets(hoten);
  cout <<"\n nhap ma sv "; cin>>masv;
  cout <<"\n nhap tuoi "; cin>>tuoi; cin.clear();
  }
 void xem(void)
  {
  cout <<"\n ho ten sinh vien: "<<hoten;
  cout <<"\n tuoi la: "<< tuoi << endl;
  }
};
//
class DIEM
{ float diem;
      public:
  float getdiem() {return diem;}
  void  putdiem(float d1) {diem=d1;}
 friend void  nhapdiem(SINHVIEN s, DIEM &d);
 friend  void  xemdiem(SINHVIEN s, DIEM &d);
};
//
void nhapdiem(SINHVIEN s, DIEM &d)
{
 float d2;
 cout <<"\n nhap diem cho sinh vien " <<s.masv <<": ";
 cin >> d2;
 d.putdiem(d2);
}
//
void xemdiem(SINHVIEN s, DIEM &d)
{
 cout <<"\n thong tin sinh vien: " ;
     cout<<"\nma sv: " <<s.masv;
     cout<<"\nho ten: " << s.gethoten();
     cout<<"\ndiem: "<<d.getdiem()<<endl;
}
//
const maxn=50;
void main () {
clrscr();
SINHVIEN s[maxn];
DIEM d[maxn];
int n, i;
cout<<"\nSo sinh vien: "; cin>>n; cin.clear();
for(i=1; i<=n; i++) s[i].nhap();
cout<<"\nThong tin vua nhap la: \n";
for(i=1; i<=n; i++) s[i].xem();
cout<<"\nNhap diem cho tung sinh vien: \n";
for(i=1; i<=n; i++) nhapdiem(s[i],d[i]);
for(i=1; i<=n; i++) xemdiem(s[i],d[i]);
getch();
}

Không có nhận xét nào:

Đăng nhận xét