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

acces base class member from derive class

Please go though following program:
#include <iostream.h>

class base {
public:
int basepublic;
protected:
int baseprotected;
};

class derived : public base {
public:
void compare(const derived *pb){
int a=pb->basepublic;
a=pb->baseprotected;
}
};

int main(){

return 0;
}

Now when i compile and run this program, it got all through.
MY question is how it is possible to access protected variable of a
different derive object in derive class's member function?
if i change argument of compare function from
const derived *pb to
const base *pb,
i get compilation error
" "1.cpp", line 14: Error: baseprotected is not accessible from
derived::compare(const base*)."
which is expected.

Aug 22 '06 #1
3 1642
gyan wrote:
Please go though following program:
#include <iostream.h>

class base {
public:
int basepublic;
protected:
int baseprotected;
};

class derived : public base {
public:
void compare(const derived *pb){
int a=pb->basepublic;
a=pb->baseprotected;
}
};

int main(){

return 0;
}

Now when i compile and run this program, it got all through.
MY question is how it is possible to access protected variable of a
different derive object in derive class's member function?
Because *pb is a derived and compare is a method of derived. Forget
base members and just consider derived ones, you would expect to be able
to see those, wouldn't you?

--
Ian Collins.
Aug 22 '06 #2
But Pb has been defined in main, outside the scope of derived class, so how
can we access protected member of pb object.member function of a class
should have access only to data member of its own object i.e. "this" and
not other object.

Aug 23 '06 #3
gyan wrote:
But Pb has been defined in main, outside the scope of derived class, so how
can we access protected member of pb object.member function of a class
should have access only to data member of its own object i.e. "this" and
not other object.
Please quote context. I didn't realise you were replying to me.

In your example that you snipped, main was empty:

#include <iostream.h>
>
class base {
public:
int basepublic;
protected:
int baseprotected;
};

class derived : public base {
public:
void compare(const derived *pb){
int a=pb->basepublic;
a=pb->baseprotected;
}
};

int main(){

return 0;
}
pb is a pointer to a derived, so any other instance of a derived can
fiddle with its private parts.

--
Ian Collins.
Aug 23 '06 #4

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

Similar topics

9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
5
by: eiji | last post by:
Hi folks, I hope this is not "off topic"! :-) Consider the next code: /* Declarations of types that could become platform-dependent */ #define MyChar char #define MyInt int
9
by: olanglois | last post by:
Hi, I am not sure if I have found a compiler bug (I am using VC++.NET2003) or if this is the correct behavior defined by the language but I am sure someone can clear up my confusion. Suppose the...
5
by: Dimitry | last post by:
I am trying to make the following: struct Base { char param; }; class Derived1 : public Base { public:
6
by: noel.hunt | last post by:
I have a base class, PadRcv, with virtual functions. User code will derive from this class and possibly supply it's own functions to override the base class virtual functions. How can I test that...
8
by: ManicQin | last post by:
Hi lets say I have the next situation class base { pubic: ;) base() {} virtual ~base(){} virtual init() {//do init } };
3
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other....
3
by: Rahul | last post by:
While reading Inside the C++ object model I came through the following paragraph Point3d origin, *ptr = &origin; A) origin.x = 0.0; B) ptr->x = 0.0; The book says "A & B statements performs...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.