Ver Mensaje Individual
  #2 (permalink)  
Antiguo 08/02/2014, 07:49
erikat
 
Fecha de Ingreso: mayo-2010
Mensajes: 12
Antigüedad: 14 años
Puntos: 0
Respuesta: Autoincrementar el atribuno de una clase cada vez que se genara un objeto

Dejo la Solucion por si alguien la necesita:

Código C++:
Ver original
  1. class Rent{
  2. protected:
  3.     int rentNo;
  4.     timepoint from, to;
  5.     Vehicle *vehicle;
  6.     Driver *driver;
  7.     Rent *next;
  8.     static int lastRent;
  9. public:
  10.     Rent(timepoint fr, timepoint t, Vehicle *v, Driver *d, Rent *n) : from(fr), to(t), vehicle(v), driver(d), next(n){
  11.     Rent::lastRent = 0;
  12.     rentNo = lastRent+1;
  13.     }
  14.     timepoint get_from(){
  15.         return from;
  16.     }
  17.     timepoint get_to(){
  18.         return to;
  19.     }
  20.     Rent get_next(){
  21.         return *next;
  22.     }
  23.     void set_from (timepoint c){
  24.         from = c;
  25.     }
  26.     void set_to (timepoint d){
  27.         to = d;
  28.     }
  29.     virtual void print(){
  30.     rentNo = 1+lastRent++;
  31.     cout << "VEHICLE RENT" << endl << "RentNo: " << rentNo << endl << "Vehicle: ";
  32.     vehicle->print();
  33.     cout << endl << "From: " << from << endl << "To: " << to << endl << "Driver: " << *driver << endl << endl;
  34.     }
  35. };
  36. int Rent::lastRent = 0;

El main es el mismo.

:)