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

basic classes manipulation

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


P: n/a
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

Jul 22 '05 #2

P: n/a
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.
Jul 22 '05 #3

P: n/a
> 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
Jul 22 '05 #4

P: n/a

"Michael Gray" <mi*********@netzero.com> 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

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.