Advertisement
SabirSazzad

Static using object count

Feb 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. class Student{
  8. private:
  9.     static int countStd;
  10.     char* idStr;
  11.     int id;
  12.     string dept;
  13. public:
  14.     Student(int id, string dept);
  15.     string getId();
  16.     int getObjectCount();
  17.     ~Student();
  18.  
  19. };
  20.  
  21. int Student::countStd = 0;
  22.  
  23.  
  24. Student::Student(int id, string dept){
  25. this->id = id;
  26. this->dept = dept;
  27. idStr = new char[10];
  28. cout<<"\ninside constructor - "<< ++countStd<<" th Object\n";
  29.  
  30. }
  31. int Student::getObjectCount(){
  32. return countStd;
  33. }
  34.  
  35. string Student::getId(){
  36.     itoa(this->id, idStr, 10);
  37. return this->dept+"-" +string(idStr);
  38. }
  39.  
  40. Student::~Student(){
  41. cout<<"\ninside destructor\n";
  42. countStd--;
  43. delete [] idStr;
  44. }
  45.  
  46. int main(){
  47.  
  48. Student student(1, "s1");
  49. Student *s = new Student(2,"s2");
  50. delete s;
  51.  
  52. cout<<"\nCurrent number of objects available: "<< s->getObjectCount();
  53.  
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement