30/09/2018, 18:28
Sao code dưới đây không xuất ra được danh sách thí sinh A,B?
#pragma once
#include<iostream>
#include <string>
using namespace std;
class Bngay
{
private:
int ngay, thang,nam;
public:
friend istream&operator>>(istream&is,Bngay &a);
friend ostream&operator<<(ostream&os,Bngay a);
};
class thisinh
{
protected:
string SBD;
string hoten;
Bngay ngaysinh;
float tongdiem;
public:
friend istream&operator>>(istream&is,thisinh &d);
friend ostream&operator<<(ostream&os,thisinh d);
};
class thsinhB: public thisinh
{
private:
float van,su ,dia;
public:
friend istream&operator>>(istream&is,thsinhB &s);
friend ostream&operator<<(ostream&os,thsinhB s);
};
class thsinhA: public thisinh
{
private:
float toan,ly ,hoa;
public:
friend istream&operator>>(istream&is,thsinhA &p);
friend ostream&operator<<(ostream&os,thsinhA p);
};
#include "Bngay.h"
istream&operator>>(istream&is,Bngay &a)
{
cout<<"
nhap ngay ";
is>>a.ngay;
cout<<"
nhap nam ";
is>>a.thang;
cout<<"
nhap nam ";
is>>a.nam;
return is;
}
ostream&operator<<(ostream&os,Bngay a)
{
os<<"
"<<a.ngay<<"/"<<a.thang<<"/"<<a.nam;
return os;
}
#include "Bngay.h"
istream&operator>>(istream&is,Bngay &a)
{
cout<<"
nhap ngay ";
is>>a.ngay;
cout<<"
nhap nam ";
is>>a.thang;
cout<<"
nhap nam ";
is>>a.nam;
return is;
}
ostream&operator<<(ostream&os,Bngay a)
{
os<<"
"<<a.ngay<<"/"<<a.thang<<"/"<<a.nam;
return os;
}
#include "thsinhA.h"
istream&operator>>(istream&is,thsinhA &p)
{
thisinh*tam=static_cast<thisinh*>(&p);
is>>*tam;
cout<<"
nhap diem toan: ";
is>>p.toan;
cout<<"
nhap diem ly";
is>>p.ly;
cout<<"
nhap diem hoa:";
is>>p.hoa;
p.tongdiem=p.toan+p.ly+p.hoa;
return is;
}
ostream&operator<<(ostream&os,thsinhA p)
{
thisinh tam=static_cast<thisinh>(p);
os<<tam;
os<<"
diemtoan: "<<p.toan<<"
diem ly: "<<p.ly<<"
diem hoa: "<<p.hoa;
return os;
}
#include "thsinhB.h"
istream&operator>>(istream&is,thsinhB &p)
{
thisinh*tam=static_cast<thisinh*>(&p);
is>>*tam;
cout<<"
nhap diem toan: ";
is>>p.van;
cout<<"
nhap diem ly";
is>>p.su;
cout<<"
nhap diem hoa:";
is>>p.dia;
p.tongdiem=p.van+p.su+p.dia;
return is;
}
ostream&operator<<(ostream&os,thsinhB p)
{
thisinh tam=static_cast<thisinh>(p);
os<<tam;
os<<"
diemvan: "<<p.van<<"
diem su: "<<p.su<<"
diem dia: "<<p.dia;
return os;
}
và main
#include "Bngay.h"
#include "thisinh.h"
#include "thsinhA.h"
#include "thsinB.h"
#include "vector"
using namespace std;
void main()
{
int n1,n2,tuychon,i,j;
thsinhA tsa;
thsinhB tsc;
vector <thsinhA> v1;
vector <thsinhB> v2;
do
{
cout<<"
--------MENU--------";
cout<<"
1.NHAP THI SINH KHOI A";
cout<<"
2.NHAP THI SINH KHOI B";
cout<<"
0. THOAT CHUONG TRINH";
do
{
cout<<"
NHAP TUY CHON 0,1,2";
cin>>tuychon;
if (tuychon<0 || tuychon>2)
cout<<"
Nhap Tuy chon sai nhap lai nha......";
}
while( tuychon<0 || tuychon>2);
if (tuychon == 1 )
{
cout <<" Nhap so thi sinh khoi A:";
cout<<n1;
for (i=0;i<n1;i++)
{
cin>>tsa;
v1.push_back(tsa);
}
}
if (tuychon == 2)
{
cout<<"Nhap so thi sinh khoi B
:";
cin>>n2;
for(j=0;j<n2;j++)
{
cin>>tsc;
v2.push_back(tsc);
}
}
}
while(tuychon!=0);
if(n1>0)
{
cout<<"
DANH SACH THI SINH THI KHOI A:";
for(i=0;i<v1.size();i++)
cout<<v1[i];
}
if(n2>0)
{
cout<<"
DANH SACH THI SINH THI KHOI B:";
for(i=0;i<v2.size();i++)
cout<<v2[i];
}
system("PAUSE");
}
Bài liên quan