domingo, 22 de febrero de 2015

Constructores y Destructores Visual C++



#include <cstdlib>
#include <iostream>

using namespace std;

class Course
{
public:

 Course()
 {
  cout << "Construyendo Course" << endl;
 }

 ~Course()
 {
  cout << "Destruyendo Course" << endl;
 }
};

class Student
{
public:

 Student()
 {
  cout << "Construyendo Student" << endl;
 }

 ~Student()
 {
  cout << "Destruyendo Student" << endl;
 }
};

class Teacher
{
public:

 Teacher()
 {
  cout << "Construyendo Teacher" << endl;
  pC = new Course;
 }

 ~Teacher()
 {
  cout << "Destruyendo Teacher" << endl;
  delete pC;
 }

protected:
 Course *pC;
};

class TutorPair
{
public:

 TutorPair()
 {
  cout << "Construyendo TutorPair" << endl;
 }

 ~TutorPair()
 {
  cout << "Destruyendo TutorPair" << endl;
 }

protected:
 Student s;
 Teacher t;
};

TutorPair *fn()
{
 cout << "Creando un objeto en la funcion fn" << endl;
 TutorPair tp;

 cout << "Asignando tp fuera de la heap" << endl;
 TutorPair *pTP = new TutorPair;

 cout << "Regrezando de fn()" << endl;
 return pTP;
}


int main( int argc , char *argv[] )
{
 TutorPair *ptReturned = fn();

 cout << "Regrezando el objeto de heap para la heap" << endl; 
 delete ptReturned;

 system("pause");
 return 0;
}




No hay comentarios:

Publicar un comentario