0

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
0

2 Answers 2

1

You don't define the function showRoomServiceMeal as a part of the RoomServiceMeal class:

void showRoomServiceMeal()
{
    ...
}

Change to

void RoomServiceMeal::showRoomServiceMeal()
{
    ...
}

Also, in the showRoomServiceMeal method you don't have to use the class prefix when accessing the parent classes members. Instead of using e.g. RestaurantMeal::price you can just use price. This is because the variables and functions are unique in each class.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks mate. but even if they are unique isnt better to use the class prefix to clear ambiguity or is it just coding style?
@SexyBeastFarEast It's more of a coding-style thing, and general laziness in some cases. :)
0

you forgot RoomServiceMeal:: before showRoomServiceMeal() (making your compiler think that function is static and not class related)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.