Private members?

S

Shraddha

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

psp

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 {
.....
}
 
S

sam_cit

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...
 
V

Vallabha

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/
 
S

Salt_Peter

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();
}
 
G

gangs

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,242
Latest member
BarbMott55

Latest Threads

Top