Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Protected access modifier working ???

Reply
Thread Tools

Protected access modifier working ???

 
 
Pradeep Kumar
Guest
Posts: n/a
 
      05-21-2004
/*******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 calcublic 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.
----------------------------------------------------------


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-21-2004

"Pradeep Kumar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> /*******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 calcublic 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'
>


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


 
Reply With Quote
 
 
 
 
Pradeep Kumar
Guest
Posts: n/a
 
      05-21-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Pradeep Kumar" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > /*******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 calcublic 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'
> >

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



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-21-2004

"Pradeep Kumar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>


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


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      05-21-2004
On Fri, 21 May 2004 11:11:44 +0530 in comp.lang.c++, "Pradeep Kumar"
<(E-Mail Removed)> wrote,
>int calc::func(date d){
> d.num = 31;
> return (d.num);
>}


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
diffrence between access modifier ....and access specifier...in c++ dost C++ 5 04-28-2006 01:40 AM
Protected access modifier in Struct Stephen ASP .Net 2 04-07-2006 07:11 PM
Question about Protected Modifier AIK Java 2 08-18-2004 06:49 AM
protected inheritance modifier Wenjie C++ 3 07-21-2003 04:59 PM
Difference between "Protected WithEvents myClassName" And "Protected myClassName" ? Andreas Klemt ASP .Net 2 07-05-2003 12:51 AM



Advertisments