![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0
![]() |
Size of inherited class!
#include<iostream>
using namespace std;
class X
{
int x;
public:
int y;
protected:
int z;
};
class Y : public X
{
int a;
public:
int b;
protected:
int c;
};
int main()
{
cout<<sizeof(Y);
return 0;
}According the output (24 on my machine) , even the variable x which was private in class X is being considered as a part of class Y? Please explain! |
|
|
|
|
|
#2 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5
![]() |
What's to explain? Those private members will still be there, even if they ae not directly accessible. However, this doesn't mean they are not accessible at all; try putting a pair of get/set methods in the base class, and then call them.
The point is that private data members are (generally) intended to help the class keep track of its own internal state. Deriving a new class from this class doesn't mean it no longer has this responsibility, thus, it should have all the original members.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
That's why it's called 'inheritance'
. Those other thangys are access modifiers.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
They are family, it counts.
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| singly-linked list templaste class in C++ w/ example driver | bl00dninja | Show Off Your Open Source Projects | 0 | Sep 11th, 2006 2:05 AM |
| Wierd compile Error. Need help please. | Keiyentai | Java | 7 | Aug 19th, 2006 2:35 AM |
| What is: "Oriented programming (OO)?" | BrinyCode | C++ | 12 | Nov 22nd, 2005 8:40 AM |
| User Input for Number Format | ericelysia1 | Java | 0 | Jul 21st, 2005 4:41 PM |
| MFC/OpenGL: problem with 'Document' class | brenda | C++ | 11 | May 23rd, 2005 9:10 PM |