Im currently learning multiple inheritance and ran into a problem creating a function that inherits its previous ancestors variables and functions. The problem occurs in the showRoomServiceMeal() functions where i do the multiple inheriting. When i compile i get errors stating that the corresponding inherited variables cannot be inherited because they are protected and that the inherited functions are used without objects.
I thought that the protected assessors allows the variables to be used by its children and to access the corresponding functions and protected variables the scope resolution operator (::) is to be used? Can anyone help explain why im getting these errors?
#include<iostream>
#include<string>
using namespace std;
class RestaurantMeal
{
protected:
string entree;
int price;
public:
RestaurantMeal(string , int );
void showRestaurantMeal();
};
RestaurantMeal::RestaurantMeal(string meal, int pr)
{
entree = meal;
price = pr;
}
void RestaurantMeal::showRestaurantMeal()
{
cout<<entree<<" $"<<price<<endl;
}
class HotelService
{
protected:
string service;
double serviceFee;
int roomNumber;
public:
HotelService(string, double, int);
void showHotelService();
};
HotelService::HotelService(string serv, double fee, int rm)
{
service = serv;
serviceFee = fee;
roomNumber = rm;
}
void HotelService::showHotelService()
{
cout<<service<<" service fee $"<<serviceFee<<
" to room #"<<roomNumber<<endl;
}
class RoomServiceMeal : public RestaurantMeal, public HotelService
{
public:
RoomServiceMeal(string , double , int );
void showRoomServiceMeal();
};
RoomServiceMeal::RoomServiceMeal(string entree, double price, int roomNum) :
RestaurantMeal(entree, price), HotelService("room service", 4.00, roomNum)
{
}
void showRoomServiceMeal()
{
double total = RestaurantMeal::price + HotelService::serviceFee;
RestaurantMeal::showRestaurantMeal();
HotelService::showHotelService();
cout<<"Total is $"<<total<<endl;
}
int main()
{
RoomServiceMeal rs("steak dinner",199.99, 1202);
cout<<"Room service ordering now:"<<endl;
rs.showRoomServiceMeal();
return 0;
}
using g++ i get this error:
RoomService.cpp: In function ‘void showRoomServiceMeal()’:
RoomService.cpp:18: error: ‘int RestaurantMeal::price’ is protected
RoomService.cpp:73: error: within this context
RoomService.cpp:18: error: invalid use of non-static data member ‘RestaurantMeal::price’
RoomService.cpp:73: error: from this location
RoomService.cpp:39: error: ‘double HotelService::serviceFee’ is protected
RoomService.cpp:73: error: within this context
RoomService.cpp:39: error: invalid use of non-static data member ‘HotelService::serviceFee’
RoomService.cpp:73: error: from this location
RoomService.cpp:74: error: cannot call member function ‘void RestaurantMeal::showRestaurantMeal()’ without object
RoomService.cpp:75: error: cannot call member function ‘void HotelService::showHotelService()’ without object