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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:17:45+00:00 2026-06-17T08:17:45+00:00

I write a virtual function with protected inherientence #include<iostream> using namespace std; class D{

  • 0

I write a virtual function with protected inherientence

#include<iostream>
using namespace std;
class D{
private:
    int a;
protected:
    int b;
public:
    D(){a=b=c=0;}
    virtual void f(){
    a=2;
    cout <<"D::f"<<a<<endl;
    }
    void g(){cout<<"D::g"<<a<<endl;}
    int c;
};
class E:protected D{
    private:
        int a,b,c;
    public:
        E(){a=b=c;}
        void f(){
            a=3;
            cout<<"E::f"<<a<<endl;
        }
        void g(){cout<<"E::g"<<a<<endl;}
};
int main(){
    D *d = new E;
    d->f();
    d->g();
    return 0;
}

but if I use it, it turn out inaccessible base.

If I change it to public inherience , it can run.

I wonder that why I cannot use D *d = new E; with private and protected inherience?

Thx in advance.

  • 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-17T08:17:46+00:00Added an answer on June 17, 2026 at 8:17 am

    private or protected base means the base is inaccessible to the world. So when you write an expression which requires conversion from the derived to the inaccessible base, that is forbidden due to accessibility rules because the conversion needs to take place at the call-site which is a part of the world.

    In object-oriented terminology, private or protected base doesn’t define is-a relationship. It is actually an implemented-in-terms-of relationship, which is composition in simpler term.

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

Sidebar

Related Questions

If we write virtual function it adds a vtable in object of that class.
I wrote a simple example, which estimates average time of calling virtual function, using
I'm trying to write an abstract class with some pure virtual binary operators, which
Problematic code: #include <array> #include <iostream> #include <cstring> int main(int argc, char *argv[]) {
I'm learning Java and trying to understand how private , protected and public modifiers
Possible Duplicate: Overriding parent class’s function I'm struggling with calling a virtual function in
So I'm trying to write a base class to handle classes that wrap std::vector,
I want to write a template function that checks some Timestamp property (class inherits
I have a purely virtual class defined as such: class BaseClass { protected: const
I'm wanting to write a virtual keyboard, like windows onscreen keyboard for touchscreen pcs.

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.