Re: Newb Question on Properties of Objects
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