Protected access modifier working ??? 
July 22nd, 2005, 12:23 PM
| | | |
/*******calc.cpp*********************/
# include <iostream.h>
# include"calc.h"
int calc::func(date d){
d.num = 31;
return (d.num);
}
/**********calc.h****************/
# include "date.h"
class calc:public date{
public:
int func(date d);
};
/*********date.h*************/*
class date {
protected :
int num;
};
/****************************/
Compile time error:
C:\Testing\test\calc.cpp(5) : error C2248: 'num' : cannot access protected
member declared in class 'date'
--
Pradeep Kumar
Software Enginner
----------------------------------------------------------
What's right isn't always popular, and what's popular isn't always right.
---------------------------------------------------------- | 
July 22nd, 2005, 12:23 PM
| | | | re: Protected access modifier working ???
"Pradeep Kumar" <pradeep.kumar@vichara.com> wrote in message
news:2h5j4uF97iphU1@uni-berlin.de...[color=blue]
> /*******calc.cpp*********************/
> # include <iostream.h>
> # include"calc.h"
>
> int calc::func(date d){
> d.num = 31;
> return (d.num);
> }
>
>
>
> /**********calc.h****************/
> # include "date.h"
>
> class calc:public date{
> public:
> int func(date d);
> };
>
>
> /*********date.h*************/*
> class date {
> protected :
> int num;
> };
>
>
> /****************************/
> Compile time error:
>
> C:\Testing\test\calc.cpp(5) : error C2248: 'num' : cannot access protected
> member declared in class 'date'
>[/color]
Yes it's working. You aren't allowed to access num from d.
int calc::func(date d){
num = 31; // OK
d.num = 31; // error
return (d.num);
}
Your design looks wrong, why would a class called calc derived from a class
called date? It doesn't make any sense. I suspect that you need to improve
your design rather than use protected access, which clearly doesn't do what
you think it does.
Why not explain what you are trying to do and someone will explain a better
way to do it.
john | 
July 22nd, 2005, 12:23 PM
| | | | re: Protected access modifier working ???
Hi John ..
thanx 4 reply .
i want to access num (protected member) of Date class from one of my
subclass where i am not sure about the exact type of paramter i am getting,
but i am sure that it will be either of date type or one of its sub class
type...
i am pretty new to c++ .. but i am sure that it will work fine in JAVA
Thanx
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2h5jnaF957c5U1@uni-berlin.de...[color=blue]
>
> "Pradeep Kumar" <pradeep.kumar@vichara.com> wrote in message
> news:2h5j4uF97iphU1@uni-berlin.de...[color=green]
> > /*******calc.cpp*********************/
> > # include <iostream.h>
> > # include"calc.h"
> >
> > int calc::func(date d){
> > d.num = 31;
> > return (d.num);
> > }
> >
> >
> >
> > /**********calc.h****************/
> > # include "date.h"
> >
> > class calc:public date{
> > public:
> > int func(date d);
> > };
> >
> >
> > /*********date.h*************/*
> > class date {
> > protected :
> > int num;
> > };
> >
> >
> > /****************************/
> > Compile time error:
> >
> > C:\Testing\test\calc.cpp(5) : error C2248: 'num' : cannot access[/color][/color]
protected[color=blue][color=green]
> > member declared in class 'date'
> >[/color]
>
> Yes it's working. You aren't allowed to access num from d.
>
> int calc::func(date d){
> num = 31; // OK
> d.num = 31; // error
> return (d.num);
> }
>
> Your design looks wrong, why would a class called calc derived from a[/color]
class[color=blue]
> called date? It doesn't make any sense. I suspect that you need to improve
> your design rather than use protected access, which clearly doesn't do[/color]
what[color=blue]
> you think it does.
>
> Why not explain what you are trying to do and someone will explain a[/color]
better[color=blue]
> way to do it.
>
> john
>
>[/color] | 
July 22nd, 2005, 12:23 PM
| | | | re: Protected access modifier working ???
"Pradeep Kumar" <pradeep.kumar@vichara.com> wrote in message
news:2h5kc2F972atU1@uni-berlin.de...[color=blue]
> Hi John ..
>
> thanx 4 reply .
>
> i want to access num (protected member) of Date class from one of my
> subclass where i am not sure about the exact type of paramter i am[/color]
getting,[color=blue]
> but i am sure that it will be either of date type or one of its sub class
> type...
>
> i am pretty new to c++ .. but i am sure that it will work fine in JAVA
>[/color]
The protected rules are different in Java and C++. Don't try and program C++
by comparing it do Java you can end up in big trouble that way. In fact
looking at your code you have another misunderstanding, because I think you
are trying to use inheritance but you are not using pointers or references.
That would be another mistake you are making because you are still thinking
Java when you are programming C++. In Java everything is a reference type
(except the built in types), in C++ nothing is a reference or a pointer
unless you are explicit about it. It's going to take you a while to get
adjusted to C++.
Would you derive a class called calc from a class called date in Java? I
think that's bad programming in any programming language.
john | 
July 22nd, 2005, 12:24 PM
| | | | re: Protected access modifier working ???
On Fri, 21 May 2004 11:11:44 +0530 in comp.lang.c++, "Pradeep Kumar"
<pradeep.kumar@vichara.com> wrote,[color=blue]
>int calc::func(date d){
> d.num = 31;
> return (d.num);
>}[/color]
The argument is type "date", not "calc", so you have no access to
protected members.
Also, setting d.num would be useless anyway, since the copy of the
argument is going to expire immediately after. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,702 network members.
|