basic classes manipulation

Discussion in 'C++' started by Michael Gray, Aug 21, 2004.

  1. Michael Gray

    Michael Gray Guest

    OK, I'm a newbie.

    Can anyone tell me why the following code gives an error that the
    lightsaber class member can't be accessed by the jedi class EVEN
    THOUGH the jedi class contains a lightsaber???//basic class
    manipulation
    #include <iostream.h>

    class lightsaber
    {
    public:
    unsigned int getState ();
    void ignite ();
    void shutDown ();
    private:
    unsigned int state;
    };

    unsigned int lightsaber::getState()
    {
    return state;
    }

    void lightsaber::ignite()
    {
    state = 1;
    }

    void lightsaber::shutDown()
    {
    state = 0;
    }

    //------------------------------------------------------------------------------
    class jedi
    {
    public:
    unsigned int getRank ();
    void setRank (int);


    private:
    unsigned int itsRank;
    lightsaber itsSaber;

    };



    unsigned int jedi::getRank()
    {
    return itsRank;
    }

    void jedi::setRank(int rank)
    { itsRank = rank;

    }



    int main()
    { lightsaber saber; //make a lightsaber object

    saber.shutDown();
    cout <<saber.getState() <<"\n";
    if (saber.getState() == 0)
    cout <<"saber is shut down \n";
    saber.ignite();
    cout <<saber.getState() <<"\n";
    if (saber.getState() == 1)
    cout <<"saber is ignited \n";



    jedi Anakin;
    Anakin.setRank(9);
    cout <<"Anakin's rank is "<<Anakin.getRank() <<"\n";

    Anakin.itsSaber.ignite();


    return 0;
    }
     
    Michael Gray, Aug 21, 2004
    #1
    1. Advertising

  2. Michael Gray

    Rich Grise Guest

    Michael Gray wrote:

    > OK, I'm a newbie.
    >

    OK, me too, so I'm guessing.

    > Can anyone tell me why the following code gives an error that the
    > lightsaber class member can't be accessed by the jedi class EVEN
    > THOUGH the jedi class contains a lightsaber???//basic class
    > manipulation


    (some snippage should reveal the motivation for my guess)

    > class lightsaber
    > private:
    > unsigned int state;
    > };

    ## (does the missing '{' mean anything?)
    > unsigned int lightsaber::getState()
    > void lightsaber::ignite()
    > void lightsaber::shutDown()
    > //------------------------------------------------------------------------------
    > class jedi
    > private:
    > lightsaber itsSaber;

    -------------
    > int main()
    > Anakin.itsSaber.ignite();


    Isn't Anakin.itsSaber private? Or do I misunderstand too?

    Thanks,
    Rich
     
    Rich Grise, Aug 22, 2004
    #2
    1. Advertising

  3. Michael Gray wrote:
    > OK, I'm a newbie.
    >
    > Can anyone tell me why the following code gives an error that the
    > lightsaber class member can't be accessed by the jedi class EVEN
    > THOUGH the jedi class contains a lightsaber???//basic class
    > manipulation


    You seem to imply that because a class contains a member of another
    class it should be able to access the private members of the class. This
    is contrary to the standard.

    A private member variable (or inherited class) can obly be accessed by
    member functions of the class containing (or inheriting) the variable
    (or inherited class) as well as it's friends.

    In your example, main() is not a member function of Jedi and hence can't
    access it's private members.

    The compiler is quite specific about it!

    xx_jedi.cpp:38: error: `lightsaber jedi::itsSaber' is private
    xx_jedi.cpp:74: error: within this context


    I suggest you either make lightsaber public or add a public access
    method to Jedi.
     
    Gianni Mariani, Aug 22, 2004
    #3
  4. > Can anyone tell me why the following code gives an error that the
    > lightsaber class member can't be accessed by the jedi class EVEN
    > THOUGH the jedi class contains a lightsaber???


    You'll need to read more about encapsulation and data protection.
    Learn about the differences about public and private (and
    eventually protected). For example,

    class C
    {
    private:
    int a;
    };

    int main()
    {
    C c;

    c.a = 10; // error
    }

    Since 'a' is declared in the private section, it is an error to access
    it from outside the class Only a member function (or a friend) can
    use it.

    > #include <iostream.h>


    Non standard, use

    # include <iostream>

    Read about standard headers and namespace std.

    [ lots of code snipped, please only post code which is relevant to your
    problem ]

    > class jedi
    > {
    > private:
    > lightsaber itsSaber;
    >
    > };
    >
    > int main()
    > {
    > jedi Anakin;
    >
    > Anakin.itsSaber.ignite();


    Here, since 'itsSaver' is private, you cannot access it. Either make it
    public (which would be a bad idea) or use a pubilc function to access it.

    >
    > return 0;
    > }


    What's more, when you post, try to indent your code and to give only the
    code which is pertinent to your question.

    If you don't understand the code you wrote, which I think is the case, stop
    right now, buy a better book and start learning. Know what you write and
    write what you know.


    Jonathan
     
    Jonathan Mcdougall, Aug 22, 2004
    #4
  5. Michael Gray

    Howard Guest

    "Michael Gray" <> wrote in message

    >

    //--------------------------------------------------------------------------
    ----
    > class jedi
    > {
    > public:
    > unsigned int getRank ();
    > void setRank (int);
    >
    >
    > private:
    > unsigned int itsRank;
    > lightsaber itsSaber;
    >
    > };
    >
    >


    > int main()
    > { lightsaber saber; //make a lightsaber object
    >
    > saber.shutDown();
    > cout <<saber.getState() <<"\n";
    > if (saber.getState() == 0)
    > cout <<"saber is shut down \n";
    > saber.ignite();
    > cout <<saber.getState() <<"\n";
    > if (saber.getState() == 1)
    > cout <<"saber is ignited \n";
    >
    >
    >
    > jedi Anakin;
    > Anakin.setRank(9);
    > cout <<"Anakin's rank is "<<Anakin.getRank() <<"\n";
    >
    > Anakin.itsSaber.ignite();
    >


    The member itsSaber is private. You can 1) make it public (not the best
    choice, usually); or, 2) add a function GetSaber that returns a reference
    (or pointer) to the lightsaber member; or, 3) add an ignite() function to
    the jedi class that simply calls the ignite function for the itsSaber
    member. I'd do the third option probably, unless there were other cases
    where I wanted a reference (or pointer) to the lightsaber owned by the jedi,
    in which case I'd choose option 2.

    -Howard
     
    Howard, Aug 24, 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. jinho
    Replies:
    2
    Views:
    382
    Joseph Millar
    Aug 6, 2003
  2. Engineer
    Replies:
    6
    Views:
    643
    Jeremy Bowers
    May 1, 2005
  3. Replies:
    0
    Views:
    452
  4. Replies:
    1
    Views:
    299
    Laurent Bugnion
    Aug 14, 2006
  5. Replies:
    2
    Views:
    310
Loading...

Share This Page