473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

polymiorphism and access privilege ?

I recently come to this thought, and did a test:

class B1
{
public:
virtual void Func()
{
std::cout << "Here is B1" << "\n";
}
};

class B2: public B1
{
private:
virtual void Func()
{
std::cout << "Here is B2" << "\n";
}
};

class B3: public B2
{
public:
virtual void Func()
{
std::cout << "Here is B3" << "\n";
}
};

int main()
{
B1* pB2 = new B2();
B1* pB3 = new B3();
pB2->Func();
pB3->Func();

delete pB2;
delete pB3;

return 0;
}

---
Func() in class B2 is under private tag, but seems it has no problem
to be called in main(), why? Is that means when polymiorphism, the
access privilege is just up to base class?

Sep 21 '07 #1
2 1308
sun1991 wrote:
I recently come to this thought, and did a test:

class B1
{
public:
virtual void Func()
{
std::cout << "Here is B1" << "\n";
}
};

class B2: public B1
{
private:
virtual void Func()
{
std::cout << "Here is B2" << "\n";
}
};

class B3: public B2
{
public:
virtual void Func()
{
std::cout << "Here is B3" << "\n";
}
};

int main()
{
B1* pB2 = new B2();
B1* pB3 = new B3();
pB2->Func();
pB3->Func();

delete pB2;
delete pB3;

return 0;
}

---
Func() in class B2 is under private tag, but seems it has no problem
to be called in main(), why? Is that means when polymiorphism, the
access privilege is just up to base class?
Access control has nothing to do with polymorphism,
Your code compiles, because all the function you call (Func) is a member
of B1 , which is *public*; and pB2 and pB3 are both pointers to B1.
Which Func to called is to be decided during *run-time*.

C++ is more flexible on the derived class with its choosing access
control for overridding functions

In C# and Java,
You can't override a public function with protected/private access.
--
Thanks
Barry
Sep 21 '07 #2
sun1991 wrote:
I recently come to this thought, and did a test:

class B1
{
public:
virtual void Func()
{
std::cout << "Here is B1" << "\n";
}
};

class B2: public B1
{
private:
virtual void Func()
{
std::cout << "Here is B2" << "\n";
}
};

class B3: public B2
{
public:
virtual void Func()
{
std::cout << "Here is B3" << "\n";
}
};

int main()
{
B1* pB2 = new B2();
B1* pB3 = new B3();
pB2->Func();
pB3->Func();

delete pB2;
delete pB3;

return 0;
}

---
Func() in class B2 is under private tag, but seems it has no problem
to be called in main(), why?
Because access specifiers and overriding are orthogonal.
Is that means when polymiorphism, the
access privilege is just up to base class?
It's up to the class through which you're calling it. In your case
'pB2' and 'pB3' are both pointers to 'B1', where the function is
declared public. If you make them pointers to 'B2', you won't be
able to call 'Func'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pete Fong | last post by:
Dear all, I am a beginner with Python. I want to write a program as "runas" in Windows XP. But I have got the following error: File...
1
by: - | last post by:
how does one prevent someone from directly inserting into a table such that any inserts should be passed to a stored procedure e.g insert_data(....) i have read the manual on revoke, grant but...
9
by: | last post by:
Hi All, I have allready tried to ask a similar question , but got no answer until now. In the meantime, I found, that I cannot understand some thread-settings for the Main() function . If I use...
2
by: Desmond | last post by:
Hi, I would really appreciate if somebody could give some advise on this. I've a ASP.NET application that is supposed to send emails and it is tested to be working well on IIS 5.0 and tested...
3
by: Jerry J | last post by:
Hi, My asp.net application must access a file that can be anywhere on the server's LAN. I am having a problem because the server, doesn't have access to files that are not on the server's local...
5
by: Simon Hart | last post by:
Does anyone know the standard code access permission to be able to manipulate a process using the Process class? Thanks Simon.
8
by: sun1991 | last post by:
Hi All, I tried the following code, but it did not work as I think: --- using namespace std; namespace { class Fraction { public:
3
by: S.M.Deshpande | last post by:
Hi, I have installed DB2 version 8.1 and created two databases, TEST1 and TEST2. Two operating system users test1 and test2 are mapped to two database users in TEST1 and TEST2 databases...
8
by: Ping | last post by:
Hello, Have a wonderful new year! I have oracle 9.2 on XP. There is a table T1 created by user U1 in U1's schema, a synonym S1 created also by U1 to point to T1 in U1's schema, U1 granted...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.