Ver Mensaje Individual
  #3 (permalink)  
Antiguo 03/02/2014, 05:18
erikat
 
Fecha de Ingreso: mayo-2010
Mensajes: 12
Antigüedad: 13 años, 11 meses
Puntos: 0
Respuesta: Error C++ al intentar pasar objetos de clase deriavada a clase base

Vale, muchas gracias.. funciono declarar la clase base como privada y ahora no me un error. Sin embargo no logro luego imprimir los datos.

Código C++:
Ver original
  1. #include<fstream>
  2. #include<iostream>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. class Satnav{
  7. private:
  8.    int serialNo;
  9. public:
  10.     Satnav(int sn) : serialNo(sn){}
  11.     string print (){
  12.         cout << "With navi# " << serialNo;
  13.     }
  14. };
  15. class Vehicle{
  16.  protected:
  17.     string brand, plate; //Deberia ser Private
  18.  public:
  19.     Vehicle(string b, string p) : brand (b), plate (p){}
  20.     string get_brand(){
  21.         return brand;
  22.     }
  23.     string get_plate(){
  24.         return plate;
  25.     }
  26.     void set_brand (string b1){
  27.         brand = b1;
  28.     }
  29.     virtual void print() = 0;
  30. };
  31.  
  32. //Task 5
  33.  
  34. class Scooter : public Vehicle{
  35. public:
  36.      Scooter(string b, string p) : Vehicle(brand,plate){}
  37.      virtual void print(){
  38.      cout << "Scooter " << brand << ", Plate: " << plate << endl;
  39.      }
  40. };
  41.  
  42. //Task 6
  43. class Car : public Vehicle{
  44. protected:
  45.     bool aircondition;
  46.     Satnav *navigation;
  47.  public:
  48.     Car(string b, string p, bool ac = 1, Satnav *n=NULL) : Vehicle(brand,plate), aircondition(ac), navigation(n){}
  49.     void set_navigation (Satnav *a){
  50.         navigation = a;
  51.     }
  52.     bool get_aircondition(){
  53.         return aircondition;
  54.     }
  55.     void no_aircondition(){
  56.         aircondition = 0;
  57.     }
  58.     void printCarData(){
  59.         cout << brand << ", plate" << plate;
  60.         if (aircondition == 0){
  61.             cout << ", no air condition";
  62.         }
  63.         else{
  64.             cout << ", with air condition";
  65.         }
  66.     }
  67. };
  68. //Task 7
  69. class Cabrio : public Car{
  70. public:
  71.     Cabrio(string b1, string p1, bool a, int *n) : Car(brand, plate, aircondition, navigation){}
  72.     virtual void print(){
  73.         cout << "Cabrio ";
  74.         printCarData();
  75.         cout << endl;
  76.     }
  77. };
  78. //Task 8
  79. class Hartop : public Car{
  80. private:
  81.     int seats;
  82. public:
  83.     Hartop(string b1, string p1, bool a, Satnav *n, int s) : Car(brand, plate, aircondition, navigation), seats(s){}
  84.     virtual void print(){
  85.     cout << "Hartop ";
  86.     printCarData();
  87.     cout << ", Seats:" << seats << endl;
  88.     }
  89. };
  90.  
  91. int main(){
  92.     int n = 5;
  93.  
  94.     Satnav nav1(1);
  95.     Satnav nav3(2);
  96.     Satnav *nav2 = &nav3;
  97.  
  98.     Vehicle *vehicle[3*n+2];
  99.     int i = 0;
  100.  
  101.     for (i=0; i<=n; i++){
  102.  
  103.         ostringstream convert,convert1,convert2;
  104.         convert << "DU-CC" << n;
  105.         Scooter sc("Honda 110cc",convert.str());
  106.         vehicle[i] = static_cast<Vehicle*> (&sc);
  107.  
  108.         convert1 << "DU-Z" << n;
  109.         Cabrio ca("BMW Z5",convert1.str(),1,0);
  110.         vehicle[i+1] = static_cast<Vehicle *> (&ca);
  111.  
  112.         convert2 << "DU-TA" << n;
  113.         Hartop ht ("Toyota Auris",convert2.str(),1,0,5);
  114.         vehicle[i+2] = static_cast<Vehicle *> (&ht);
  115.     }
  116.  
  117.     Hartop fg1("Ford Galaxy","DU-FG 1",1,&nav1,7);
  118.     Hartop fg3("Ford Galaxy","DU-FG 2",1,&nav3,7);
  119.     Hartop *fg2 = &fg3;
  120.  
  121.     vehicle[4] = reinterpret_cast<Vehicle*>(&fg1);
  122.     vehicle[5] = reinterpret_cast<Vehicle*>(&fg2);
  123.  
  124.     int j=0;
  125.     for (int j = 0; j <= 3*n+2; j++)
  126.       vehicle[j]->print();
  127.  
  128.    return 0;
  129. }

El programa compila pero parece que se quedara colgando en un bucle que no logro identificar.

Muchas gracias

Última edición por erikat; 03/02/2014 a las 05:33