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 6814155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:39:20+00:00 2026-05-26T20:39:20+00:00

Hello to all that read I am self learning C++ from a text book:

  • 0

Hello to all that read

I am self learning C++ from a text book:

A Question in the textbook asks me to make a function a friend of a class and therefore the friend function can have access to all of the classes members; which is fine I can do this.
Problem is that the question then goes on to ask that the friend function can only read the class members (private members) but NOT write to them!

**Please note: I have looked at similar question/answers on ‘stackoverflow’ – but I could not find the relevant and simple straight forward answer that I am after.

Here is some relevant code that I have, any help would be appreciated:

Many thanks in advance

#include <iostream>
#include <cstdio>

using namespace std;

class classroom{

  private:
         char name[25];
         int student_id;
         float grades[10];
         float average;
         int num_tests;
         float letter_grade;
         static int last_student_id;
  public:       
         void enter_name_id(void);
         void enter_grade(void);
         void average_grades(void);
         void letter_grades(void);
         void output_name_id_grade(void);
         void last_id(void);
         classroom();


  friend void readers(classroom&);

  };

      int classroom::last_student_id=1;



   void readers(classroom& lee){ 

      cout<<"\n number tests: "<<lee.num_tests;//friend function is reading class member -This is O.K!

      lee.num_tests=15;//friend function is writing to class member - We don't want this to be allowed

      cout<<"\n number tests: "<<lee.num_tests;//Used to test that class members are NOT accessed!             



   } 

and in the main program we have:

int main()
{       

        classroom students[10];
        readers(students[0]);

//and so on...
  • 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-05-26T20:39:20+00:00Added an answer on May 26, 2026 at 8:39 pm

    A function which is a ‘friend of a Class’ that is allowed to have ‘read access’ to its ‘private members’ but NOT ‘write access’?

    Once you declare a function as friend function, the access specifier rules are off, but you can prevent writing to members by passing in a const object to the friend function.

    friend void readers(const classroom&);
                        ^^^^^
    

    Note that access specifiers and constness are two different attributes, do not mix them.

    Note that your friend function can still try to modify the object by casting away the constness of the object by using const_cast but that would result in Undefined Behavior as per the Standard.

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

Sidebar

Related Questions

Hello to all that read I am self learning C++ from a text book.
Hello! I would like to extract all citations from a text. Additionally, the name
This has to be a common question that all programmers have from time to
Hello to all that read I have a small problem(or it could be large!),
Hello all i have a quick question Is there a way i could make
Hello friends i need a book/ tutorials for rails without using scoffold. All the
I fought with the IDE and read around all day enough to learn that
Hello all I need to encrypt text what is the best encryption to use
Hello all! I'm working on a prototype that would require me being able to
When I read litb answer to this question , I learned that passing an

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.