Inheritance

2 views
Skip to first unread message

Nilu

unread,
Sep 16, 2010, 11:52:44 AM9/16/10
to nextgen_engg
Code snippet in C++ :
#include <iostream>

class Base
{ public :
Base(){}
virtual ~Base(){}

virtual void someMethod()
{ std::cout << "Base : someMethod() was called."; }
};

class Derived : public Base
{ public :
Derived(){}
~Derived(){}

private :
virtual void someMethod()
{ std::cout << "Derived : someMethod() was called."; }
};

int main(void)
{ Base* b = new Derived();
b->someMethod();
return 0;
}

(Same)Code snippet in Java :
class Base {
public void someMethod() {
System.out.println("Base : someMethod() was called.");
}
}

class Derived extends Base {
private void someMethod() {
System.out.println("Derived : someMethod() was called.");
}
}

class InheritanceThingy {
public static void main(String args[]) {
Base b = new Derived();
b.someMethod();
}
}

Question for both the code snippets :
1. Will the code compile?
2. If yes, will it run? If yes, please explain.
Message has been deleted

Mickey

unread,
Sep 16, 2010, 10:07:28 PM9/16/10
to nextgen_engg
It compiles:
c:\c\bin\g++.exe -pedantic -Wall -c test.cpp -o test.o

>Exit code: 0

It builds:

>c:\c\bin\g++.exe -pedantic -Wall test.cpp -o test.exe
>Exit code: 0

It runs:
>test.exe

Derived : someMethod() was called.

Hee hee, C++ is like that. This is a known 'feature' if you ask me, at
first glance it does look like a bug but that is how it is. Probably
because how virtual table are stored in memory. I wonder if it works
the other way: you can access a private member of a base class by
deriving a class from it and doing cunning coding... surprisingly
there is a way to do that also! So my question-- will that be a
security breach? I can tell you the answer, it is "no", it is the
justification that is the interesting part...

I haven't explained the behavior asked by Nilesh... instead asked
another one... There can be more than one way to answer these...

Regards,
Jyoti

sachithanandam karthikselvan

unread,
Sep 19, 2010, 2:13:18 AM9/19/10
to nextge...@googlegroups.com
> Hee hee, C++ is like that. This is a known 'feature' if you ask me,
> at first glance it does look like a bug but that is how it is.
> Probably
> because how virtual table are stored in memory. I wonder
> if it works the other way: you can access a private member of a base
> class by deriving a class from it and doing cunning coding...
> surprisingly
> there is a way to do that also! So my question-- will that be a
> security
> breach? I can tell you the answer, it is "no",
<karthikselvan> Your answer is 100% correct. You can't call this as a
security breach. This works as per the design. Here is the context
from section 15.3 Access control (page no 402) from "The C++
programming Language special edition by Bjarne Stroustrup". I
consider this book as a bible for me for C++.

"
A member of a class can be private,protected, or public:

- If it is private, ...
- If it is protected,...
- If it is public, its name can be used by any function.

This refelcts the view that there are three kinds of functions
accessing a class: functions implementing the class (its friends and
members), function implementing a derived class (The derived class
friends and members) and other functions."

You shall not have (Read RFC (Request For Comments) 2119 to interpret
the menaing of "SHALL") private virtual functions and protected
virtual functions in the base class. That means you shall have only
public virtual functions in the base class. This public virtual
functions in the base class can be overridden by and virtual function
which has the same signature in the derived class. In this case , the
derived class has the same signature in the private section but
private section doesn't matter for public virtual sections in the
base class. Using the dynamic binding concept, the virtual pointer
table locates the derived class virtual function signature and
executes it by calling it. So this can not be considered as a security
breach. It works as per the design.

I don't know Java so I can't make a comment on that.

Thanks
S.Karthikselvan
(P.S. You guys start reading or writing RFCs to understand networking
concepts from the source. I mean original authors of the protocol.)

Nilu

unread,
Sep 20, 2010, 5:28:36 AM9/20/10
to nextgen_engg
You both are right.

On Sep 16, 8:52 pm, Nilu <niluk...@gmail.com> wrote:

Akhil Bhiwal

unread,
Oct 19, 2010, 12:56:46 AM10/19/10
to nextgen_engg
We can still access private variables of a class using pointers.

class Demo
{
private:
int a;
int b;
public:
void setValue(int aa, int bb)
{
a = aa;
b = bb;
}
};

int main()
{
Demo obj;
obj.setValue(1,2);
int *ptr = (int *)&obj;
cout << *ptr;
ptr ++;
cout << *ptr;
return 0;
}

Now, if we want to access the function, then we can cast the object to
a function pointer and then access it.


Regards,
Akhil Bhiwal
Reply all
Reply to author
Forward
0 new messages