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#include<fstream>
#include<iostream>
#include <sstream>
using namespace std;
class Satnav{
private:
int serialNo;
public:
Satnav(int sn) : serialNo(sn){}
string print (){
cout << "With navi# " << serialNo;
}
};
class Vehicle{
protected:
string brand, plate; //Deberia ser Private
public:
Vehicle(string b, string p) : brand (b), plate (p){}
string get_brand(){
return brand;
}
string get_plate(){
return plate;
}
void set_brand (string b1){
brand = b1;
}
virtual void print() = 0;
};
//Task 5
class Scooter : public Vehicle{
public:
Scooter(string b, string p) : Vehicle(brand,plate){}
virtual void print(){
cout << "Scooter " << brand << ", Plate: " << plate << endl;
}
};
//Task 6
class Car : public Vehicle{
protected:
bool aircondition;
Satnav *navigation;
public:
Car(string b, string p, bool ac = 1, Satnav *n=NULL) : Vehicle(brand,plate), aircondition(ac), navigation(n){}
void set_navigation (Satnav *a){
navigation = a;
}
bool get_aircondition(){
return aircondition;
}
void no_aircondition(){
aircondition = 0;
}
void printCarData(){
cout << brand << ", plate" << plate;
if (aircondition == 0){
cout << ", no air condition";
}
else{
cout << ", with air condition";
}
}
};
//Task 7
class Cabrio : public Car{
public:
Cabrio(string b1, string p1, bool a, int *n) : Car(brand, plate, aircondition, navigation){}
virtual void print(){
cout << "Cabrio ";
printCarData();
cout << endl;
}
};
//Task 8
class Hartop : public Car{
private:
int seats;
public:
Hartop(string b1, string p1, bool a, Satnav *n, int s) : Car(brand, plate, aircondition, navigation), seats(s){}
virtual void print(){
cout << "Hartop ";
printCarData();
cout << ", Seats:" << seats << endl;
}
};
int main(){
int n = 5;
Satnav nav1(1);
Satnav nav3(2);
Satnav *nav2 = &nav3;
Vehicle *vehicle[3*n+2];
int i = 0;
for (i=0; i<=n; i++){
ostringstream convert,convert1,convert2;
convert << "DU-CC" << n;
Scooter sc("Honda 110cc",convert.str());
vehicle[i] = static_cast<Vehicle*> (&sc);
convert1 << "DU-Z" << n;
Cabrio ca("BMW Z5",convert1.str(),1,0);
vehicle[i+1] = static_cast<Vehicle *> (&ca);
convert2 << "DU-TA" << n;
Hartop ht ("Toyota Auris",convert2.str(),1,0,5);
vehicle[i+2] = static_cast<Vehicle *> (&ht);
}
Hartop fg1("Ford Galaxy","DU-FG 1",1,&nav1,7);
Hartop fg3("Ford Galaxy","DU-FG 2",1,&nav3,7);
Hartop *fg2 = &fg3;
vehicle[4] = reinterpret_cast<Vehicle*>(&fg1);
vehicle[5] = reinterpret_cast<Vehicle*>(&fg2);
int j=0;
for (int j = 0; j <= 3*n+2; j++)
vehicle[j]->print();
return 0;
}
El programa compila pero parece que se quedara colgando en un bucle que no logro identificar.
Muchas gracias