Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 4th, 2007, 12:23 PM   #1
sharadpro
Newbie
 
Join Date: Feb 2006
Location: India
Posts: 25
Rep Power: 0 sharadpro is on a distinguished road
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!
sharadpro is offline   Reply With Quote
Old Feb 4th, 2007, 12:33 PM   #2
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is offline   Reply With Quote
Old Feb 4th, 2007, 1:14 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 4th, 2007, 2:43 PM   #4
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3 pegasus001 is on a distinguished road
Quote:
Originally Posted by sharadpro View Post
even the variable x which was private in class X is being considered as a part of class Y?
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.
pegasus001 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC