By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Protected access modifier working ???

P: n/a
/*******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.
----------------------------------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Pradeep Kumar" <pr***********@vichara.com> wrote in message
news:2h************@uni-berlin.de...
/*******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
Jul 22 '05 #2

P: n/a
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" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

"Pradeep Kumar" <pr***********@vichara.com> wrote in message
news:2h************@uni-berlin.de...
/*******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

Jul 22 '05 #3

P: n/a

"Pradeep Kumar" <pr***********@vichara.com> wrote in message
news:2h************@uni-berlin.de...
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
Jul 22 '05 #4

P: n/a
On Fri, 21 May 2004 11:11:44 +0530 in comp.lang.c++, "Pradeep Kumar"
<pr***********@vichara.com> 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.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.