1. 类声明头文件
//TestInstance.h
#ifndef TESTINSTANCE_H_
#define TESTINSTANCE_H_
//structure
typedef struct student
{
char name[15];
float grade;
int biryear;
int birmonth;
int birday;
}student;
//class
class CStudent
{
public:
CStudent();
CStudent(int num, int size);
~CStudent();
void DisplayStudent() const;
void DisplayStudent(int i) const;
bool LocateStudent(student& infor, int i) const;
private:
void CreateData();
void ClearBuffer(); //clear cin buffer
void Inputdata(int i);
private:
student *stu;
int number; //student number
int spaceSize; //space size
};
#endif
2. 类声明实现文件
//TestInstance.cpp
#include "TestInstance.h"
#include <IOMANIP>
#include <IOSTREAM>
#include <exception>
using namespace std;
//student class
CStudent::CStudent()
{
stu = new student;
if (stu == NULL)
exit(-1);
number = 0;
}
CStudent::CStudent(int num, int size)
{
if (num >size)
{
size = num+1;
}
stu = new student[size];
if (stu == NULL)
exit(-1);
number = num;
spaceSize = size;
CreateData();
}
CStudent::~CStudent()
{
delete stu;
}
void CStudent::DisplayStudent() const
{
cout << "//----------------------------------------------------------------------//\n";
cout << setw(15) << "name" << setw(8) << "grade" << setw(15) << "birthday" << endl;;
cout << "//----------------------------------------------------------------------//\n";
for (int i=0; i<number; i++)
{
cout << setw(15) << stu[i].name << setw(8) << stu[i].grade
<< setw(12) << stu[i].biryear << "-" << stu[i].birmonth << "-" << stu[i].birday << endl;
}
cout << "//----------------------------------------------------------------------//\n";
}
void CStudent::DisplayStudent(int i) const
{
cout << "//----------------------------------------------------------------------//\n";
cout << setw(15) << "name" << setw(8) << "grade" << setw(15) << "birthday" << endl;;
cout << "//----------------------------------------------------------------------//\n";
cout << setw(15) << stu[i+1].name << setw(8) << stu[i+1].grade
<< setw(12) << stu[i+1].biryear << "-" << stu[i+1].birmonth << "-" << stu[i+1].birday << endl;
cout << "//----------------------------------------------------------------------//\n";
}
bool CStudent::LocateStudent(student& infor, int i) const
{
if (i < 1 || i > number)
{
cout << "Invalid index!!";
return false;
}
infor.birday = stu[i-1].birday;
infor.birmonth = stu[i-1].birmonth;
infor.biryear = stu[i-1].biryear;
infor.grade = stu[i-1].grade;
strcpy(infor.name, stu[i-1].name);
DisplayStudent(i-1);
return true;
}
void CStudent::Inputdata(int i)
{
cout << "\nPlease Input student " << i+1 << " name: ";
cin.get(stu[i].name, 15);
ClearBuffer();
cout << "Please Input student " << i+1 << " grade: ";
cin >> stu[i].grade;
while (stu[i].grade < 0.00 || stu[i].grade > 100.00)
{
cout << "Invalid data!\n";
cin >> stu[i].grade;
ClearBuffer();
}
cout << "Please Input student " << i+1 << " year: ";
cin >> stu[i].biryear;
while (stu[i].biryear < 1900 || stu[i].biryear > 2006)
{
cout << "Invalid data!\n";
cin >> stu[i].biryear;
ClearBuffer();
}
cout << "Please Input student " << i+1 << " month(1-12): ";
cin >> stu[i].birmonth;
while (stu[i].birmonth < 1 || stu[i].birmonth > 12)
{
cout << "Invalid data!\n";
cin >> stu[i].birmonth;
ClearBuffer();
}
cout << "Please Input student " << i+1 << " day: ";
cin >> stu[i].birday;
switch (stu[i].birmonth)
{
case 2:
while (stu[i].birday < 1 || stu[i].birday > 28)
{
cout << "Invalid data!\n";
cin >> stu[i].birday;
ClearBuffer();
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
while (stu[i].birday < 1 || stu[i].birday > 31)
{
cout << "Invalid data!\n";
cin >> stu[i].birday;
ClearBuffer();
}
break;
case 4:
case 6:
case 9:
case 11:
while (stu[i].birday < 1 || stu[i].birday > 30)
{
cout << "Invalid data!\n";
cin >> stu[i].birday;
ClearBuffer();
}
break;
default:
break;
}
ClearBuffer();
}
void CStudent::CreateData()
{
for (int i=0; i<number; i++)
{
Inputdata(i);
}
}
void CStudent::ClearBuffer()
{
cin.clear();
while (!isspace(cin.get()))
continue;
}
//end of student class
3. main函数
//Test.cpp
//Test some instance
#include "TestInstance.h"
#include <IOSTREAM>
#include <exception>
using namespace std;
int main(int argc, char* argv[])
{
CStudent stu(3, 5);
stu.DisplayStudent();
student stu2;
stu.LocateStudent(stu2, 2);
stu.DisplayStudent();
return 0;
}