Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Dec 11th, 2006, 7:30 PM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
accessibilty class

Hello,
I need to understand the accessibility of inheritance.
I have this code:
#include<iostream>
using namespace std;

class X {
       protected:
                    void fn() { cout << "In X::fn()\n"; } 
       public:
                    X() { cout << "In X::X()\n"; } 
                   virtual void print() { cout << "In X::print()\n"; fn(); } 
                   virtual void fnx() { cout << "In X::fnx()\n"; }
};

class Y : protected X { // Inheritance of Y from X
        public:
                Y() { cout << "In Y::Y()\n"; } 
                virtual void print() { cout << "In Y::print()\n"; fn(); } 
                virtual void fny() { cout << "In Y::fny()\n"; }
};

class Z : public Y { // Inheritance of Z from Y
        public:
                 Z() { cout << "In Z::Z()\n"; } 
                 virtual void print() { cout << "In Z::print()\n"; fn(); } 
};

void foo1(X& obj) { obj.print(); } 
void foo2(Y& obj) { obj.print(); } 
void foo3(Z& obj) { obj.print(); } 
void foo4(X obj) { obj.print(); } 
 
int main() {
       X objx; 
       Y objy; 
       Z objz; 
       foo1(objx); 
       foo4(objx); 
       foo2(objy); 
       foo4(objy); // `X' is an inaccessible base of `Y' 
       foo3(objz); 
       foo4(objz); X' is an inaccessible base of `Z' 

return 0;}
The error is in red on the code.
I want to understand what is happening at those errors points.
I read the documentation of accessibility rules but I am wondering why we dont have an error for
foo3(objz)
or the other since they all calling X::fn()
can someone help me please?

thank you
B
brad sue is offline   Reply With Quote
Old Dec 11th, 2006, 7:38 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Read the definitions of "public", "private", and "protected".
__________________
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 Dec 11th, 2006, 8:22 PM   #3
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
OK relatively to protected inheritance, I quote:
"
- inherited public and protected members become protected members of the derived class.

- all the inherited members are inaccessible to the rest of the program (main & other non-memberfunctions)

- each subsequently derived class has access to the combined set of the protected and public members of the previous base classes.
(this is where I get confused in respect to the code I have.)"

why foo4(objy) would not compile?

When it calls print(),print() calls fn() since all subsequent derived class have access to the previous base class!!
brad sue is offline   Reply With Quote
Old Dec 12th, 2006, 3:16 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,222
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by brad sue View Post
why foo4(objy) would not compile?
Because it is being called from main(), and main() has no access to non-public attributes, base classes, or member functions of class Y.
grumpy 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
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 1:35 AM
URL class Eric the Red Java 5 Jun 24th, 2006 9:01 PM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 7:40 AM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
MFC/OpenGL: problem with 'Document' class brenda C++ 11 May 23rd, 2005 8:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:22 AM.

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