Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > basic classes manipulation

Reply
Thread Tools

basic classes manipulation

 
 
Michael Gray
Guest
Posts: n/a
 
      08-21-2004
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;
}
 
Reply With Quote
 
 
 
 
Rich Grise
Guest
Posts: n/a
 
      08-21-2004
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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-21-2004
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.
 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      08-22-2004
> 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


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      08-24-2004

"Michael Gray" <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TurboTax Basic vs. Taxcut Basic? Sharp Dressed Man Computer Support 1 01-12-2009 12:52 PM
What is the difference between Visual Basic.NET and Visual Basic 6? Jimmy Dean Computer Support 3 07-25-2005 07:05 AM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments