1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <iostream>
using namespace std;
struct Person {
public:
enum Kind {FACULTY, STUDENT};
protected:
Kind kind;
char id[10];
char *name;
public:
Person (const char id[], const char nm[], Kind type)
{
strcpy(this->id,id);
name = new char [strlen(nm)+1];
if (name == 0)
{
cout<<"out of memory"<<endl;
exit(1);
}
strcpy(name,nm);
kind = type;
}
Kind getKind() const { return kind; }
~Person()
{ delete [] name; }
};
struct Faculty : public Person {
private:
char*rank;
public:
Faculty (const char id[], const char nm[], const char r[]):Person(id,nm, FACULTY)
{
rank = new char [strlen(r)+1];
if (rank == 0)
{
cout<<"out of memory"<<endl;
exit(1);
}
strcpy (rank,r);
}
void write () const
{
cout<<"id: "<<id<<endl;
cout<<"name: "<<name<<endl;
cout<<"rank: "<<rank<<endl<<endl;
}
~Faculty ()
{ delete [] rank; }
};
struct Student: public Person {
private:
char* major;
public:
Student (const char id[], const char nm[], const char m[]): Person(id, nm, STUDENT)
{
major = new char [strlen(m)+1];
if (major == 0)
{
cout<<"out of memory"<<endl;
exit(1);
}
strcpy(major,m);
}
void write () const
{
cout<<"id: "<<id<<endl;
cout<<"name: "<<name<<endl;
cout<<"major: "<<major<<endl<<endl;
}
~Student ()
{
delete []major;
}
};
void write (const Person *p)
{
switch (p->getKind())
{
case Person::FACULTY:((Faculty*)p)->write();
break;
case Person::STUDENT:((Student*)p)->write();
break;
}
}
int main ()
{
Person* data[20]; int cnt=0;
int choice =1;
for (int i=0; i<2; i++)
{
cout<<"1:Creation of new student"<<endl;
cout<<"2:Creation of new faculty"<<endl;
cout<<"3:Quit"<<endl;
cin>>choice;
if (choice == 1)
data[cnt] =new Student ("123","one","software");
else if (choice == 2)
data[cnt] = new Faculty("234","two","professor");
else break;
cnt++;
}
cout<<"Total records read: "<<cnt<<endl<<endl;
for (int i=0; i<cnt; i++)
{
write (data[i]);
}
for (int j=0; j<cnt; j++)
{
delete data[j];
}
}
|