home *** CD-ROM | disk | FTP | other *** search
- // Chap16_2.cpp
- #include <iostream.h>
- #include <string.h>
- class Student
- {
- public:
- //same constructor and destructor as earlier
- Student(char *pName = "no name")
- {
- strcpy(name, pName);
- noOfStudents++; // count us
- pNext = pFirst; // add us to front of list
- pFirst= this;
- }
- ~Student()
- {
- noOfStudents--;
-
- Student* pS; // remove us from list
- if (pFirst == this)
- {
- pFirst = pNext;
- }
- else
- {
- // look for the guy in front of us...
- for (pS = pFirst; pS; pS = pS->pNext)
- {
- if (pS->pNext == this)
- {
- // ...change his next pointer to
- // "go around" us
- pS->pNext = pNext;
- break;
- }
- }
- }
- }
-
- //findName - return student w/specified name
- static Student *findName(char *pName);
- protected:
- static Student *pFirst;
- Student *pNext;
- char name[40];
- static int noOfStudents;
- };
- Student* Student::pFirst = 0;
- int Student::noOfStudents = 0;
- //findName - return the Student with the specified name.
- // Return zero if no match.
- Student* Student::findName(char *pName)
- {
- //loop thru the linked list...
- for (Student *pS = pFirst; pS; pS = pS->pNext)
- {
- //...if we find the specified name...
- if (strcmp(pS->name, pName) == 0)
- {
- //...then return the objectÆs address
- return pS;
- }
- }
- //...otherwise, return a zero (item not found)
- return (Student*)0;
- }
-
- int main()
- {
- Student s1("Randy");
- Student s2("Jenny");
- Student s3("Kinsey");
- Student *pS = Student::findName("Jenny");
- return 0;
- }
-