ch15_6.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:

C#编程

开发平台:

Visual C++

  1. //**********************
  2. //**    ch15_6.cpp    **
  3. //**********************
  4. #include <iostream.h>
  5. #include <string.h>
  6. class Student{
  7. public:
  8.   Student(char* pName);
  9.   ~Student();
  10.   static Student* findname(char* pName);
  11. protected:
  12.   static Student* pFirst;
  13.   Student* pNext;
  14.   char name[40];
  15. };
  16. Student* Student::pFirst = 0;  //静态成员空间分配及初始化
  17. Student::Student(char*pName)
  18. {
  19.   strncpy(name,pName, sizeof(name));
  20.   name[sizeof(name)-1]='';
  21.   pNext=pFirst;
  22.   pFirst=this;
  23. }
  24. Student::~Student()
  25. {
  26.   if(pFirst==this){
  27.     pFirst=pNext;
  28.     return;
  29.   }
  30.   for(Student* pS=pFirst; pS; pS=pS->pNext)
  31.     if(pS->pNext==this){
  32.       pS->pNext=pNext;
  33.       return;
  34.     }
  35. }
  36. Student* Student::findname(char* pName)
  37. {
  38.   for(Student* pS=pFirst; pS; pS=pS->pNext)
  39.     if(strcmp(pS->name,pName)==0)
  40.       return pS;
  41.   return (Student*)0;
  42. }
  43. void main()
  44. {
  45.   Student s1("Randy");
  46.   Student s2("Jenny");
  47.   Student s3("Kinsey");
  48.   Student* pS =Student::findname("Jenny");
  49.   if(pS)
  50.     cout <<"ok." <<endl;
  51.   else
  52.     cout <<"no find." <<endl;
  53. }