![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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;}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) can someone help me please? thank you B |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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!! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,222
Rep Power: 5
![]() |
|
|
|
|
![]() |
| 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 |
| 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 |