can someone help me with this code?

This code follows the concept of dynamic binding but I cannot see how. I also cannot see how function write() is a virtual function. This is probably because of the "enum". What does it exactly do here?


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];
	}
}

Last edited on
The code emulates the functionality of virtual functions to show you how much easier it is to just use the virtual keyword.
The write function checks which type the object has using the getKind() function, then uses that information to call the write member function of the appropriate derived type.
Polymorphism mens that type information is marked somewhere and can be used in run time instead of the actual type. The thing Person* points to needs to know whether it is a Student of a Faulty so that it can call the appropriate method. The enum is used to store that information. It could easily be replaced with any other type of variable (if it's not a basic type you won't be able to use a swich, but you can use if then). Function write is used to check that information and call the appropriate method write.
Oh I see now. Thank you both very much :)
Topic archived. No new replies allowed.