Hi! I don't understand why my compiler gives me this error when I'm trying to run this code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
class Airplane
{
public:
Airplane();
~Airplane();
protected:
int fuel;
int speed;
int metersOverGround;
int steeringWheel;
private:
void ShowData(const Airplane& ap);
};
Airplane::Airplane () {}
Airplane::~Airplane () {}
class MultiAirplane : public Airplane
{
public:
MultiAirplane(int fuel, int speed, int metersOverGround, int steeringWheel);
~MultiAirplane();
private:
Airplane multiAirplane;
};
MultiAirplane::MultiAirplane(int fuel, int speed, int metersOverGround, int steeringWheel)
{
cout << "MultiAirplane" << endl;
this->multiAirplane.fuel = fuel; // WHY WON'T THIS WORK??
}
MultiAirplane::~MultiAirplane()
{
}
class SingleAirplane : public Airplane
{
public:
SingleAirplane();
~SingleAirplane();
};
void Airplane::ShowData(const Airplane& ap)
{
cout << "Speed: " << ap.speed << endl;
cout << "Fuel: " << ap.fuel << endl;
cout << "M.O.G: " << ap.metersOverGround << endl;
cout << "S.W: " << ap.steeringWheel << endl;
}
int main(int argc, char* argv[])
{
return 0;
}
error: 'int Airplane::fuel' is protected
error: within this context
The variable is protected. Yeah, that's right. But shouldn't a derived class be able to reach it? Or is it only in a function that the derived class is able to reach protected variables and isn't able to reach protected variables in the constructor?
Thanks for help!