473,396 Members | 1,784 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,396 software developers and data experts.

Access specifier and inheritance

Hi,

I have a class x with two public methods xmethod1 and xmethod2. I want
to derive a class y such that it can access only xmethod2. I have done
the following and it compiles please let me know if it is valid to do
so.

class y : public x
{
private:
void xmethod1()
{
// some dummy implementation
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?

Thanks and Regards,
M Shetty

PS: This still allows:
y *y1 = new y;
x *x2 = y1;
x2->xmethod1();
// calls derived private method y::xmethod1
Jul 22 '05 #1
3 2653
"mshetty" <ms*****@mail.com> wrote
Hi,

I have a class x with two public methods xmethod1 and xmethod2. I want
to derive a class y such that it can access only xmethod2. I have done
the following and it compiles please let me know if it is valid to do
so.

class y : public x
{
private:
void xmethod1()
{
// some dummy implementation
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?

Thanks and Regards,
M Shetty

PS: This still allows:
y *y1 = new y;
x *x2 = y1;
x2->xmethod1();
// calls derived private method y::xmethod1


What you're trying to do violates one of the most basic rules of object-oriented
programming -- the Liskov Substitutability Principle. This says that instances
(or references or pointers) of derived classes MUST be substitutable for
instances (or references or pointers) of the base class. A corrolary to this is
that you can make a derived class do more than its base, but you can't make it
do less. This is true for OOP in any language, not only C++.

In other words, if baseInst.xyz() is legal but derivedInst.xyz() is illegal,
there's a fundamental flaw in the class hierarchy that should be fixed, not
circumvented.

Claudio Puviani
Jul 22 '05 #2
"mshetty" <ms*****@mail.com> wrote...
I have a class x with two public methods xmethod1 and xmethod2.
Are those member functions declared 'virtual' or not?
I want
to derive a class y such that it can access only xmethod2.
In order to grant access only to member function 'xmethod2' in 'x', you
should declare it either public or protected. If it's public, everybody
will have access to it. If it's protected, only derived classes and
friends will have access. If it's private, non-friends cannot access it.
I have done
the following and it compiles please let me know if it is valid to do
so.
The posted code does not contain enough information to judge that or you
failed to correctly state the requirements you impose on the classes.

class y : public x
{
private:
void xmethod1()
{
// some dummy implementation
No need for any implementation, even for a body. If nobody is allowed
to access it, any attempt to use 'xmethod1' should be an error, even
a "legal" one, from the same class. Just don't provide the body at all.
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?

Thanks and Regards,
M Shetty

PS: This still allows:
y *y1 = new y;
x *x2 = y1;
x2->xmethod1();
// calls derived private method y::xmethod1


No, it most likely calls the base class member function. Without
seeing how you defined 'x', it is impossible to tell for sure.

Victor
Jul 22 '05 #3
mshetty wrote:
Hi,

I have a class x with two public methods xmethod1 and xmethod2. I want
to derive a class y such that it can access only xmethod2. I have done
the following and it compiles please let me know if it is valid to do
so.

class y : public x
{
private:
void xmethod1()
{
// some dummy implementation
// trying to override xmethod1 with a private access
// specifier so that whenever xmethod1 is invoked using an
// object of y an error is thrown by the compiler
}
}

Is this valid? I want the compiler to throw an error.
Is there a better way of doing this?


Nope. You can't make publically inherited methods "more private." Use
protected inheritance instead, and put a "using x::xmethod2" declaration
in the "public" section of y's definition. This technique is useful
when x is being made a base of y only to ease y's implementation.

Jul 22 '05 #4

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

Similar topics

1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
8
by: Stanislav Simicek | last post by:
Hello, I've several C++ classes that must be ported to C#. Is there any construction in C# similar to C++: class A : protected B { ... }
13
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
5
by: dost | last post by:
can u tell me the diffrence between...... private public and protected .........in........these.......which one...is access modifier....and which one..is specifier....... and....the diffrence...
18
by: Money | last post by:
Here in this thread http://groups.google.co.in/group/comp.lang.c/browse_frm/thread/c16d280238a95c9/e3b0dbf76f3e02e3?q=finding+endianness&rnum=1#e3b0dbf76f3e02e3 Tydr Schnubbis in 3rd reply used...
2
by: sarathy | last post by:
Hi, What is the difference b/w a "storage class" and "storage class specifier"? It is said that there, Storage Class --automatic static ( K&R ) Storage Class Specifier --auto extern register...
3
by: Rahul | last post by:
Hi Everyone, The following code works fine, class A { private: friend void sample(A& obj) { printf("in friend...\n"); printf("%d",obj.i);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.