Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:36:29+00:00 2026-06-10T05:36:29+00:00

Im currently learning multiple inheritance and ran into a problem creating a function that

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:36:30+00:00Added an answer on June 10, 2026 at 5:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple menus that are using .slideToggle (im learning jQuery currently). Using online
I'm currently learning how eclipse plugins work. I want to get a table into
I'm currently reading Oreilly's Learning XNA 4.0 and I've noticed that the author is
Currently I'm learning about neural networks and I'm trying to create an application that
I am trying to learn SQL and currently I'm learning about the COUNT function.
im currently learning python (in the very begining), so I still have some doubts
im currently learning stacks in java and have a quick question. what will the
I am currently learning backbone.js via a screencast tutorial, somewhere along the way, my
I'm currently learning C and am struggling with how to iterate through an array
I am currently learning Javascript, and I'd like to create my own Lorem Ipsum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.