473,498 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private members?

Is it is possible to derive pivate class members of base class in it's
derive class?

May 3 '07 #1
5 1775
psp
On May 2, 9:25 pm, Shraddha <shraddhajosh...@gmail.comwrote:
Is it is possible to derive pivate class members of base class in it's
derive class?
Yes, instead of deriving the public interface you can derive private
by using the keyword private:

class derived : private base {
.....
}

May 3 '07 #2
On May 3, 9:25 am, Shraddha <shraddhajosh...@gmail.comwrote:
Is it is possible to derive pivate class members of base class in it's
derive class?
I think you want to access private members of the base class in the
member functions of derived class...
If that is the case, it is not possible, however you could declare
them protected in the base class and have the member functions of the
derived class to access the same...

May 3 '07 #3
On May 3, 9:25 am, Shraddha <shraddhajosh...@gmail.comwrote:
Is it is possible to derive pivate class members of base class in it's
derive class?
See if below technique works for you:

#include <iostream>

using namespace std;

class base {
private:
int a;
public:
base() { a= 100; }
friend class derived;
};

class derived :public base{
public:
void print() { cout << "The value of a is " << this->a <<
endl; }
};

int main ()
{
derived o2;
o2.print();
return 0;
}

Output:
% ./a.out
The value of a is 100

Cheers
-Vallabha
S7 Software Solutions
http://www.s7solutions.com/

May 3 '07 #4
On May 3, 2:07 am, Vallabha <vsnadago...@gmail.comwrote:
On May 3, 9:25 am, Shraddha <shraddhajosh...@gmail.comwrote:
Is it is possible to derive pivate class members of base class in it's
derive class?

See if below technique works for you:

#include <iostream>

using namespace std;

class base {
private:
int a;
public:
base() { a= 100; }
friend class derived;

};

class derived :public base{
public:
void print() { cout << "The value of a is " << this->a <<
endl; }

};

int main ()
{
derived o2;
o2.print();
return 0;

}

Output:
% ./a.out
The value of a is 100

Cheers
-Vallabha
S7 Software Solutionshttp://www.s7solutions.com/
The above is an example of the use of friend destroying the interface
the base class provides.
If a private member is to be reacheable, a const-correct interface is
called for, not friend.

#include <iostream>

class base {
int a;
public:
base() : a( 100 ) { }
int get() const { return a; } // could be protected
};

class derived : public base {
public:
void print() const { std::cout << get() << std::endl; }
};

int main()
{
derived instance;
instance.print();
}
May 3 '07 #5
On May 3, 9:25 am, Shraddha <shraddhajosh...@gmail.comwrote:
Is it is possible to derive pivate class members of base class in it's
derive class?
No, according to the c++ design there is no explicit mechanism to
derive a private member of a base class.
But (yes the inevitable but... :) ) if you want to access the private
data in a derived class there are whole lot of methods to do that.
Remember access specifiers are a design decision and not a security
constraint.

one of the method would be :

class base {
private:
int b;
int a;
public:
base()
{
b=100;
a= 200;
}
};

class derived :public base{
public:
void print()
{
printf("%d\n", *this);
printf("%d\n", *(reinterpret_cast<int*>(this)+1));
}
};

int main ()
{
derived o2;
o2.print();
return 0;
}

Gangs.

May 3 '07 #6

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

Similar topics

7
9735
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
12
2627
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate =...
8
3930
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
5
2394
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
4
6098
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
8
7815
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
10
25711
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
6
4773
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
86
4549
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
11
6420
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
0
7126
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
7005
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
7210
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
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5465
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
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.