domingo, 22 de febrero de 2015

Miembros Estaticos Visual C++



#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Student
{
public:

 Student( const char *pN = "no name" ) : sName( pN )
 {
  noOfStudents++; 
 }

 ~Student()
 {
  noOfStudents--;
 }

 const string &name()
 {
  return sName;
 }

 static int number()
 {
  return noOfStudents;
 }

protected:

 string sName;
 static int noOfStudents;
};

int Student :: noOfStudents = 0;

int main( int argc , char *argv[] )
{
 Student s1("Kenny");
 Student *pS2 = new Student("Nicole");

 cout << "Creado: " << s1.name() 
  << " y " << pS2 -> name() << endl;

 cout << "Numero de estudiantes: " << s1.number() << endl;


 cout << "Eliminando: " << pS2 -> name() << endl;
 delete pS2;
 cout << "Numero de estudiantes: " << Student :: number() << endl;

 system("pause");
 return 0;
}

No hay comentarios:

Publicar un comentario