Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Student
  5. {
  6. private:
  7.     int age;
  8.     char *name;
  9. public:
  10.     static int count;
  11.     Student(int m,char *n);
  12.     Student()
  13.     {
  14.         count++;
  15.         age = 0;
  16.         name = new char[strlen("NoName")+1];
  17.         strcpy(name,"NoName");
  18.     }
  19.     ~Student()
  20.     {
  21.         count--;
  22.         delete[] name;
  23.     }
  24.     void Print() const
  25.     {
  26.         cout << count << endl;
  27.         cout << "Name=" << name <<" Age=" << age <<endl;
  28.     }
  29. };
  30. int Student::count = 0;
  31. Student::Student(int m,char *n)
  32. {
  33.     count++;
  34.     age = m;
  35.     name = new char[strlen(n) + 1];
  36.     strcpy(name,n);
  37. }
  38. int main()
  39. {
  40.     cout << "count="<<Student::count<<endl;
  41.     Student s1, *p = new Student(23,"ZhangHong");
  42.     s1.Print();
  43.     p->Print();
  44.     delete p;
  45.     s1.Print();
  46.     Student Stu[4];
  47.     cout <<"count="<<Student::count<< endl;
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement