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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:11:14+00:00 2026-05-22T01:11:14+00:00

I’m doing a C++ quiz and I need to say whats wrong in the

  • 0

I’m doing a C++ quiz and I need to say whats wrong in the following code:

class Base {
    friend class SubClass;
    int n;
    virtual int getN()
    {
        return n;
    }
};

class SubClass: public Base {
public:
    SubClass() {}
    SubClass(const SubClass& s) {}
};

int main()
{
    SubClass sc;
    if(sc.getN() <= 5)
    {
        int x = sc.getN();
    }
    return 0;
}

Besides that n is uninitilized and maybe the object should be created through a Base class pointer, what else could be wrong?
When I run it I get this error:

 error: 'virtual int Base::getN()' is private
  • 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-22T01:11:14+00:00Added an answer on May 22, 2026 at 1:11 am

    As by default every member of a class1 is private, getN in the base class is declared private.

    Make getN public as:

    class Base {
        friend class SubClass;
        int n;
    public: //<--------------------- you forgot this
        virtual int getN()
        {
            return n;
        }
    };
    

    1. I mean, a class defined with the keyword class. Note that class can be defined with the keyword struct and union as well, according to the C++ Standard.


    EDIT:

    If you think because SubClass is a friend of Base, so it can access private members of Base from outside, then that is wrong. friend means member functions of SubClass can access private members of Base class.

    However, if you make main() friend of Base, then your code would work:

     class Base {
            friend int main(); //make main() friend of Base
            //...
        };
    

    Now from main(), any private members of Base can be accessed!

    See this demo : http://www.ideone.com/UKkCF

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.