Protected access modifier working ???

P

Pradeep Kumar

/*******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'
 
J

John Harrison

Pradeep Kumar said:
/*******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'

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
 
P

Pradeep Kumar

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
 
J

John Harrison

Pradeep Kumar said:
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
 
D

David Harmon

On Fri, 21 May 2004 11:11:44 +0530 in comp.lang.c++, "Pradeep Kumar"
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.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top