Cách sử dụng mảng đối tượng
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
//Tao lop person
class Person
{
public:
int Id;
string name;
virtual void input()
{
cout << "ID:";cin >> Id;
cin.ignore();
cout << "Name:";getline(cin, name);
}
virtual void output()
{
cout << "ID:" << Id << endl;
cout << "Name:" << name << endl;
}
virtual double calculateBonus() = 0;
};
//Tao student thua ke Person
class Student :public Person
{
public:
int studId;
float avgMark;
void input()
{
Person::input();
cout << "studId:";cin >> studId;
cout << "avgMark:";cin >> avgMark;
}
void output()
{
Person::output();
cout << "studId:" << studId << endl;
cout << "avgMark" << avgMark << endl;
}
double calculateBonus()
{
if (avgMark > 8.0)
return 750000;
else
return 0;
}
};
class Lecturer : public Person
{
public:
int empID;
int numberOfPaper;
void input()
{
Person::input();
cout << "empID:";cin >> empID;
cout << "numberOfPaper";cin >> numberOfPaper;
}
void output()
{
Person::output();
cout << "empID" << empID << endl;
cout << "numberOfPaper" << numberOfPaper << endl;
}
double calculateBonus()
{
if (numberOfPaper > 3.0)
return 1500000;
else
return 0;
}
};
class listOfPerson
{
public:
Person *ds[100];
int k;
void inputPerson()
{
//int k=0,
int chon, i;
k = 0;
while (1)
{
cout << "\n*Lecturer/student/stop (1,2,3):"; cin >> chon; cin.get();
if (chon == 3) break;
if (chon == 1) ds[k] = new Lecturer();
if (chon == 2) ds[k] = new Student();
ds[k]->input(); k++;
}
}
void outputPerson()
{
for (int i = 0; i < k; i++)
{
ds[i]->output();
printf("\n%.2f", ds[i]->calculateBonus());
//cout << "Bonus:" <
}
}
void sortDESC()
{
for (int i = 0; i < k-1; i++)
for (int j = i + 1; j < k; j++)
{
if (ds[i]->calculateBonus()
{
Person *tmp;
tmp = ds[i];
ds[i] = ds[j];
ds[j] = tmp;
}
}
}
};
int main()
{
listOfPerson objLOP;
objLOP.inputPerson();
objLOP.outputPerson();
objLOP.sortDESC();
cout << "Sau khi sort:" << endl;
objLOP.outputPerson();
return 0;
}
Trương Đình Huy
» Tin mới nhất:
» Các tin khác: