[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Newb Question on Properties of Objects

databinge

12/2/2008 11:51:00 AM

Hi,
Please redirect me if this is the wrong group to post to- but, I'm
starting a cpp project and have some questions about the capabilities
of objects. Ironically, I'm away from my computer this week, so I'm
programming by hand in a notebook. (I'm posting this from a library).
If I had my computer I'd simply try out the things I"m wondering
about- but I cant. Oh well.

So. I'm making a program to track the movements of stars. It's
basically a database program, with a simple renderer built in. I'm
planning to keep track of the stars in a list of star objects. For
each star I'll record 3 variables, like so:

Object::Star
{
int bearing(24); // list to hold a bearing reading for a given star.
One reading per hour of day
int ascension(24); // list to hold an ascension reading for a given
star. One reading per hour.

int brightness;
}

That's pretty much it. Forgive my sloppy syntax. So, in the Main()
program, I want to create a list of these objects, to keep track of a
whole group of stars. Say 256 of them. Like so:

Main()
{
Star all_stars(255); //a list holding 256 star objects
Star current; //a single star object to use in program

int hour = 18;

for (int i = 0; i<=255;i++)
{
current = all_stars(i);
Display_star(current.bearing(hour), current.ascension(hour)); //
function to render stars
}

return 0;
}



So, my question is, will my main() work? Particularly, is it valid
to assign all_stars(i) to "current"? - ( essentially, accessing
"all_stars(i).bearing(hour)" if that were valid). I apologize for the
syntax, but perhaps you can tell what I'm doing, and let me know if it
makes sense.

Thank you very much!

-Jack
9 Answers

Maxim Yegorushkin

12/2/2008 12:45:00 PM

0

On Dec 2, 11:51 am, databi...@gmail.com wrote:

>    Please redirect me if this is the wrong group to post to- but, I'm
> starting a cpp project and have some questions about the capabilities
> of objects. Ironically, I'm away from my computer this week, so I'm
> programming by hand in a notebook. (I'm posting this from a library).
> If I had my computer I'd simply try out the things I"m wondering
> about- but I cant. Oh well.

If you have internet access you can compile it online:
http://www.comeaucomputing.com...

--
Max

Nick Keighley

12/2/2008 12:46:00 PM

0

On 2 Dec, 11:51, databi...@gmail.com wrote:

>    Please redirect me if this is the wrong group to post to-

seems ok

> but, I'm
> starting a cpp project and have some questions about the capabilities
> of objects. Ironically, I'm away from my computer this week, so I'm
> programming by hand in a notebook. (I'm posting this from a library).
> If I had my computer I'd simply try out the things I"m wondering
> about- but I cant. Oh well.
>
>    So. I'm making a program to track the movements of stars. It's
> basically a database program, with a simple renderer built in. I'm
> planning to keep track of the stars in a list of star objects. For
> each star I'll record 3 variables, like so:
>
> Object::Star

as you note later your syntax is sloppy (wrong). You may mean

class Star


> {
>   int bearing(24); // list to hold a bearing reading for a given star.
> One reading per hour of day

the syntax for an array is
int bearing [24];

but consider using a vector


>   int ascension(24); // list to hold an ascension reading for a given
> star. One reading per hour.
>
>   int brightness;
>
> }

are bearing,ascension and brightness *really* integers?
Wouldn't a floating point type like double make more sense?


> That's pretty much it. Forgive my sloppy syntax. So, in the Main()
> program, I want to create a list of these objects, to keep track of a
> whole group of stars. Say 256 of them. Like so:
>
> Main()

int main ()

> {
>   Star all_stars(255); //a list holding 256 star objects
>   Star current; //a single star object to use in program

consider making current a pointer

>   int hour = 18;
>
>   for (int i = 0; i<=255;i++)
>   {
>       current = all_stars(i);
>       Display_star(current.bearing(hour), current.ascension(hour)); //
> function to render stars
>   }
>
> return 0;
>
> }
>
>    So, my question is, will my main() work? Particularly, is it valid
> to assign all_stars(i) to "current"?

yes

> - ( essentially, accessing
> "all_stars(i).bearing(hour)" if that were valid). I apologize for the
> syntax, but perhaps you can tell what I'm doing, and let me know if it
> makes sense.

consider making Display_star a member function of Star.

class Star
{
public:
void display(int hour); // displays the star at the given hour
};


I'd get your C++ book out and look up basic classes.
Find out what member functions are and what they are for.
Look up constructors.


--
Nick Keighley


jt

12/2/2008 12:52:00 PM

0

On Tue, 02 Dec 2008 03:51:26 -0800, databinge wrote:

> Hi,
> Please redirect me if this is the wrong group to post to- but, I'm
> starting a cpp project and have some questions about the capabilities of
> objects. Ironically, I'm away from my computer this week, so I'm
> programming by hand in a notebook. (I'm posting this from a library). If
> I had my computer I'd simply try out the things I"m wondering about- but
> I cant. Oh well.
>
> So. I'm making a program to track the movements of stars. It's
> basically a database program, with a simple renderer built in. I'm
> planning to keep track of the stars in a list of star objects. For each
> star I'll record 3 variables, like so:
>
> Object::Star
> {
> int bearing(24); // list to hold a bearing reading for a given star.
> One reading per hour of day
> int ascension(24); // list to hold an ascension reading for a given
> star. One reading per hour.
>
> int brightness;
> }

Might look something like this:

#include <vector>

class Star
{
const int num_hours;

vector<int> bearing;
vector<int> ascension;
int brightness;

public:

// this may look silly (yes, we know there are 24 hours in a day) but
// it's good style not to litter your code with hard-coded constants

Star() : num_hours(24), bearing(num_hours), ascension(num_hours)
{
// constructor code
}

void Display(int hour) // a star "knows how to display itself"
{
// display code can access bearing[hour], etc.
}
};

Display() need not be a member function of Star. Perhaps if you have a
"renderer" class, then Display might better be a member function of that,
in which case it might look like:

void Display(const Star& star, int hour)
{
// display code can access star.bearing[hour], etc.
}

Your call, really.

> That's pretty much it. Forgive my sloppy syntax. So, in the Main()
> program, I want to create a list of these objects, to keep track of a
> whole group of stars. Say 256 of them. Like so:

Do you really mean "list"? A vector may be more approriate (and easier to
program).

#include<vector>

> Main()

int main()

> {
> Star all_stars(255); //a list holding 256 star objects

// again, define a constant rather then a hard-coded number

const int num_stars = 256; // not 255!

vector<Star> all_stars(num_stars);

> Star current;//a single star object to use in program

Probably not necessary.

> int hour = 18;
>
> for (int i = 0; i<=255;i++)
> {
> current = all_stars(i);
> Display_star(current.bearing(hour), current.ascension(hour)); //
> function to render stars
> }

for (int i = 0; i < num_stars; ++i)
{
all_stars[i].Display(hour); // i-th star displays itself
}

or, if Display is a member of some renderer class;

Renderer renderer; // define a renderer object

for (int i = 0; i < num_stars; ++i)
{
renderer.Display(all_stars[i], hour); // renderer displays i-th star
}

>
> return 0;
> }
>
>
>
> So, my question is, will my main() work?

No way.

> Particularly, is it valid
> to assign all_stars(i) to "current"? - ( essentially, accessing
> "all_stars(i).bearing(hour)" if that were valid).

Who knows? It's wildly invalid code.

> I apologize for the
> syntax, but perhaps you can tell what I'm doing,

Luckily I'm an excellent mind-reader...

> and let me know if it makes sense.

It didn't - hopefully it will, eventually ;-)

--
Lionel B

Triple-DES

12/2/2008 1:29:00 PM

0

On 2 Des, 13:51, Lionel B <m...@privacy.net> wrote:
> Might look something like this:
>
> #include <vector>
>
> class Star
> {
>     const int num_hours;
>
>     vector<int> bearing;
>     vector<int> ascension;
>     int brightness;
>
> public:
>
>     // this may look silly (yes, we know there are 24 hours in a day) but
>     // it's good style not to litter your code with hard-coded constants

Non-static const data members may not be such a good idea either, see
below.

>
>     Star() : num_hours(24), bearing(num_hours), ascension(num_hours)
>     {
>         // constructor code
>     }
>
>     void Display(int hour) // a star "knows how to display itself"
>     {
>         // display code can access bearing[hour], etc.
>     }
>
> };

[snip]

Note that the use of std::vector<Star> later in the code possibly
renders your program ill-formed, because it may cause the implicit
declaration of the implicitly defined copy assigment operator
(std::vector requires Star to be Assignable).

jt

12/2/2008 4:17:00 PM

0

On Tue, 02 Dec 2008 05:29:12 -0800, Triple-DES wrote:

> On 2 Des, 13:51, Lionel B <m...@privacy.net> wrote:
>> Might look something like this:
>>
>> #include <vector>
>>
>> class Star
>> {
>>     const int num_hours;
>>
>>     vector<int> bearing;
>>     vector<int> ascension;
>>     int brightness;
>>
>> public:
>>
>>     // this may look silly (yes, we know there are 24 hours in a day)
>>     but // it's good style not to litter your code with hard-coded
>>     constants
>
> Non-static const data members may not be such a good idea either, see
> below.

Yes, num_hours should be static.

--
Lionel B

James Kanze

12/2/2008 5:21:00 PM

0

On Dec 2, 1:46 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> On 2 Dec, 11:51, databi...@gmail.com wrote:

[...]
> int main ()

> > {
> > Star all_stars(255); //a list holding 256 star objects

His type Star didn't have a constructor taking an int, and it
only represented a single star. What he needs here is:

std::vector< Star > all_stars( 256 ) ;

(Or don't define the dimensions, and just push_back the Star as
you input them.)

> > Star current; //a single star object to use in program

> consider making current a pointer

Why? For the moment, Star seems to have value semantics, which
means that you probably don't want a pointer to it.

> > int hour = 18;

> > for (int i = 0; i<=255;i++)
> > {
> > current = all_stars(i);
> > Display_star(current.bearing(hour), current.ascension(hour)); //
> > function to render stars
> > }

> > return 0;

> > }

> > So, my question is, will my main() work? Particularly, is it
> > valid to assign all_stars(i) to "current"?

> yes

No, since the definition of itself wasn't legal. If he uses a
vector, then
current = all_stars[ i ] ;
is certainly legal. But I'd definitly write the loop:

for ( size_t i = 0 ; i < all_stars.size() ; ++ i ) {
// ...
}

(Or use an iterator.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Puppet_Sock

12/2/2008 5:28:00 PM

0

On Dec 2, 6:51 am, databi...@gmail.com wrote:
[snip]
>    So. I'm making a program to track the movements of stars.
[snip exmaple code]

Posting code snippets is not an efficient way to
get started at C++. What you want is to get one
(or preferably more than one, but one to start)
of the intro texts.

If you don't think you have time to go through
any of these books in detail from start to end,
try the following plan.

First, skim through the text, checking the
headings and reading the first couple pages
of each chapter. Then go through the parts
that seem to apply to what you are doing.

One such text is _Accelerated C++_ by Koenig
and Moo. But there are others. There are even
a few on the net. I *think* (have not looked
lately) that _Thinking in C++_ is still posted
on the web by the author. Google for it, you
should find it quickly.

Once you get up and running in the basics of
the language, then feel free to come back and
ask questions. Particularly of the more difficult
parts where surprising (to you) things happen.
Socks

pjb

12/3/2008 12:55:00 PM

0

James Kanze <james.kanze@gmail.com> writes:

> On Dec 2, 1:46 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
> wrote:
>> On 2 Dec, 11:51, databi...@gmail.com wrote:
>
> [...]
>> int main ()
>
>> > {
>> > Star all_stars(255); //a list holding 256 star objects
>
> His type Star didn't have a constructor taking an int, and it
> only represented a single star. What he needs here is:
>
> std::vector< Star > all_stars( 256 ) ;


No. I think the OP lacks brackets. What he needs here is:

Star all_stars ??( 256 ??) ;



>> > current = all_stars(i);

current = all_stars ??( i ??) ;

(compiled with: gcc -trigraphs ...)


;-)


--
__Pascal Bourguignon__

databinge

12/8/2008 2:48:00 AM

0

Hey Everyone,
Wow. Thanks so much to everyone who responded. My apologies for
taking close to a week to respond back. First, just wanted to make
sure to thank everyone who wrote back with advice. I didn't expect so
much help, let alone people to actually write out code for me. (I've
been living as a quasi-vagrant for about a month now. I wrote my
original post from a public library. After that I was in Manhattan
briefly, followed by an accidental trip to Vermont.)

So. Thanks for clearing things up for me regarding how objects
work, and thanks for muddling through my horrible pseudo code. I'll
definitely be reading up a bit on my c++ basics. So that was probably
the best advice. I took a standard c++ course, and an advanced one
back in college, and even did really well. It's occured to me though,
that that was eight years ago, and my memory is kind of poor.

I'm kind of psyched about the star program though. At the moment
I'm really focused on the rendering aspect of the program. I wrote a
formula for displaying the Ascension/Bearing variables on a
rectangular screen. I'm pretty proud of it. Likely there's a better
algorithm online somewhere, but I'm doing this for fun. If anyone has
interest in the equation I'd be happy to post it.

Otherwise, I'll take some time to re-educate myself on the basics
of OOP, and be sure to post here if I run into any more snags.

Thanks again,

Jack

On Dec 2, 6:51 am, databi...@gmail.com wrote:
> Hi,
>    Please redirect me if this is the wrong group to post to- but, I'm
> starting a cpp project and have some questions about the capabilities
> of objects. Ironically, I'm away from my computer this week, so I'm
> programming by hand in a notebook. (I'm posting this from a library).
> If I had my computer I'd simply try out the things I"m wondering
> about- but I cant. Oh well.
>
>    So. I'm making a program to track the movements of stars. It's
> basically a database program, with a simple renderer built in. I'm
> planning to keep track of the stars in a list of star objects. For
> each star I'll record 3 variables, like so:
>
> Object::Star
> {
>   int bearing(24); // list to hold a bearing reading for a given star.
> One reading per hour of day
>   int ascension(24); // list to hold an ascension reading for a given
> star. One reading per hour.
>
>   int brightness;
>
> }
>
> That's pretty much it. Forgive my sloppy syntax. So, in the Main()
> program, I want to create a list of these objects, to keep track of a
> whole group of stars. Say 256 of them. Like so:
>
> Main()
> {
>   Star all_stars(255); //a list holding 256 star objects
>   Star current; //a single star object to use in program
>
>   int hour = 18;
>
>   for (int i = 0; i<=255;i++)
>   {
>       current = all_stars(i);
>       Display_star(current.bearing(hour), current.ascension(hour)); //
> function to render stars
>   }
>
> return 0;
>
> }
>
>    So, my question is, will my main() work? Particularly, is it valid
> to assign all_stars(i) to "current"? - ( essentially, accessing
> "all_stars(i).bearing(hour)" if that were valid). I apologize for the
> syntax, but perhaps you can tell what I'm doing, and let me know if it
> makes sense.
>
>    Thank you very much!
>
> -Jack