Protected access modifier working ???

Discussion in 'C++' started by Pradeep Kumar, May 21, 2004.

  1. /*******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.
    ----------------------------------------------------------
    Pradeep Kumar, May 21, 2004
    #1
    1. Advertising

  2. "Pradeep Kumar" <> wrote in message
    news:...
    > /*******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
    John Harrison, May 21, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    >
    > "Pradeep Kumar" <> wrote in message
    > news:...
    > > /*******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
    >
    >
    Pradeep Kumar, May 21, 2004
    #3
  4. "Pradeep Kumar" <> wrote in message
    news:...
    > 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
    John Harrison, May 21, 2004
    #4
  5. Pradeep Kumar

    David Harmon Guest

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

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    557
    Andreas Klemt
    Jul 5, 2003
  2. Stephen

    Protected access modifier in Struct

    Stephen, Apr 6, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    398
    Stephen
    Apr 7, 2006
  3. AIK
    Replies:
    2
    Views:
    358
    Rogan Dawes
    Aug 18, 2004
  4. Wenjie
    Replies:
    3
    Views:
    621
    Victor Bazarov
    Jul 21, 2003
  5. dost
    Replies:
    5
    Views:
    1,161
    Phlip
    Apr 28, 2006
Loading...

Share This Page