Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   basic classes manipulation (http://www.velocityreviews.com/forums/t285150-basic-classes-manipulation.html)

Michael Gray 08-21-2004 09:05 PM

basic classes manipulation
 
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;
}

Rich Grise 08-21-2004 11:04 PM

Re: basic classes manipulation
 
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


Gianni Mariani 08-21-2004 11:04 PM

Re: basic classes manipulation
 
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.

Jonathan Mcdougall 08-22-2004 03:58 AM

Re: basic classes manipulation
 
> 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



Howard 08-24-2004 10:21 PM

Re: basic classes manipulation
 

"Michael Gray" <mikenicgray@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





All times are GMT. The time now is 04:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.