2010-02-13 21:15:53 UTC
the book says that derived classes cant access private functions, but if you want derived classes to access member functions, and prevent other users from accessing them, you use "protected:"
however, i still get a compiler error (compiler is MinGW GCC; IDE is Code::Blocks)
here is the code i wrote to test it and i cannot see what i did wrong
#include
using namespace std;
class Vehicle
{
public:
int RandomPublicVariable;
protected:
void Drive()
{
cout << "vroom vroom!" << endl;
}
};
class DodgeViper : public Vehicle
{
public:
void CheckOutMyViper()
{
cout << "Yo check out my hot viper!" << endl;
}
};
int main()
{
DodgeViper MyCar;
MyCar.CheckOutMyViper();
MyCar.Drive();
return 0;
}
when i compile and run, i get the error "void Vehicle::Drive() is protected". i might have typed that error wrong, but you get the jist: it says Vehicle() is protected, and the book says derived classes can inherit protected functions. WTF?