bài taapj của mình có 3 lớp: 1 lớp mẹ (NhanVien ) và 2 lớp con (CongNhan, CanBo)
sau khi sắp xếp lại theo thứ tự lương giảm dần. Mình muốn tìm Công nhân có lương thấp nhất thì làm thế nào???
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class NV
{
private:
string ten, gt;
int tuoi;
static int dem;
public:
virtual void nhap()
{
cin.ignore();
cout<<"\nten= ";
getline(cin, ten);
cout<<"tuoi= ";
cin>>tuoi;
cin.ignore();
cout<<"gioi tinh= ";
getline(cin, gt);
}
virtual void hienthi()
{
cout<<setw(10)<<dem++<<setw(20)<<ten<<setw(10)<<tuoi<<setw(10)<<gt;
}
virtual float luong()=0;
};
int NV::dem=0;
class CN:public NV
{
private:
int songay, luongngay;
public:
void nhap()
{
NV::nhap();
cout<<"luong ngay= ";cin>>luongngay;
cout<<"so ngay= ";cin>>songay;
}
void hienthi()
{
NV::hienthi();
cout<<setw(10)<<luongngay<<setw(10)<<songay<<setw(30)<<luong()<<endl;
}
float luong()
{
return luongngay*songay;
}
};
class CB:public NV
{
float hsl, phucap;
public:
void nhap()
{
NV::nhap();
cout<<"hsl= ";cin>>hsl;
cout<<"phu cap= ";cin>>phucap;
}
void hienthi()
{
NV::hienthi();
cout<<setw(30)<<hsl<<setw(10)<<phucap<<setw(10)<<luong()<<endl;
}
float luong()
{
return hsl*1150000+phucap;
}
};
int main()
{
NV *a[100];//mang con tro de -->tro den can bo or cong nhan
int M,N;
cout<<"M,N= ";cin>>M>>N;
//nhap thong tin cho nhan vien
cout<<"\n1.nhap thong tin cho cong nhan:"<<endl;
for (int i=0; i<M;i++)
{
a[i]= new CN();//vi la con tro nen khong co bo nho nen dung new de tao khong gian luu tru thong tin cho no
a[i]->nhap();
}
cout<<"\n2.nhap thong tin cho can bo:"<<endl;
//nhap thong tin cho can bo
for (int i=M; i<M+N;i++)
{
a[i]=new CB();//vi la con tro nen khong co bo nho nen dung new de tao khong gian bo nho cho no
a[i]->nhap();
}
//xuat
cout<<"==>DS vua nhap:"<<endl;
cout<<setw(10)<<"STT"<<setw(20)<<"ten"<<setw(10)<<"tuoi"<<setw(10)<<"gt"<<setw(10)<<"luongngay"<<setw(10)<<"songay"
<<setw(10)<<"hsl"<<setw(10)<<"phucap"<<setw(10)<<"luong"<<endl;
for (int i=0; i<M+N;i++)
a[i]->hienthi();
for (int i=0; i<M+N-1;i++)
for (int j=i+1;j<M+N;j++)
if(a[i]->luong()<a[j]->luong())
{
NV* tg;
tg=a[i];
a[i]=a[j];
a[j]=tg;
}
cout<<"\n==> Sap xep theo chieu giam dan:"<<endl;
for (int i=0; i<M+N;i++)
a[i]->hienthi();
//vấn đề xuất hiện ở đây:
cout<<"\n==> cong nhan co luong thap nhat la:"<<endl;
float min = a[0]->luong();
for (int i=0; i<M; i++)
if (a[i]->luong()<min) min=a[i]->luong();
for (int i=0; i<M; i++)
if (a[i]->luong()==min)
a[i]->hienthi();
//vấn đề là cái mảng đã được sắp xếp lại nên từ a[0] -> a[m] không phải là thông tin riêng của công nhân nữa.
return 1;
}


83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?