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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:36:47+00:00 2026-06-04T12:36:47+00:00

// inheritence experiment #includestdafx.h #include<iostream> using namespace std; class base { private: int i;

  • 0
// inheritence experiment

#include"stdafx.h"
#include<iostream> 

using namespace std;

class base
{
private:
    int i;
};

class derived: public base
{
private:
    int j;
};

int main()
{
    cout << endl << sizeof(derived) << endl << sizeof(base);
    derived o1;
    base o2;
    cout << endl << sizeof(o1) << endl << sizeof(o2); 
}

I’m getting this output:

8
4
8
4

why’s that so? private data members of a base class aren’t inherited into the derived class, so why I am getting 8 bytes for both, the size of derived and o1 ?

  • 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-04T12:36:48+00:00Added an answer on June 4, 2026 at 12:36 pm
    class base {
    public:
      base(int v) : m_value(v) { }
      int value() { return m_value; }
    private:
      int m_value;
    };
    
    class derived : private base {
      derived(int v) : base(v) { }
      int value2() { return value(); }
    };
    
    int main()
    {
      derived d(2);
      return d.value2();
    }
    

    How do you expect this to work if the private base class doesn’t store the value somewhere?

    Your choice of words “inherited into the derived class” makes me think you are confused about how inheritance works. Members from the base class are not inherited into or copied into the derived class. The derived class contains an instance of the base type, as a sub-object within the complete derived object, so a derived class is (usually) at least as large as the sum of its base classes.

    Consider this code:

    struct A {
    private:
      int i;
    };
    
    struct B {
      int j;
    };
    
    struct derived : A, B {
      int k;
    };
    

    An A object is laid out in memory as an int, and so is a B.
    A derived object could be laid out in memory as an A object, followed by a B object, followed by an int, which means it is a sequence of three ints. Whether the base class members are public or private doesn’t affect that.

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

Sidebar

Related Questions

Consider the following simplified interface inheritence hierarchy: // Starting point: public interface Base {
My group has code with the following structure class Base { public: float some_base_function();
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity
Consider the following inheritance: abstract class Employee { private string empID; private string empName;
Assuming the following hypothetical inheritance hierarchy: public interface IA { int ID { get;
When you draw an inheritance diagram you usually go Base ^ | Derived Derived
I have a funny import error when using Inheritence in Python. In a parent
I need to know something about inheritence in C#. I have a class shown
I'm coming to the end of my intro programming class, we're using java and
I have been using joinedsubclass inheritence mapping for a while and decided to switch

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.