General Question

Mariah's avatar

Is there a smarter way to program this (C++)?

Asked by Mariah (25863points) June 15th, 2012

I’m attempting to teach myself some C++ this summer, and an exercise I came up with is to program a virtual fishtank, trying to make it as realistic as possible. I’m going to try and include some water chemistry properties and the ways in which they interact (e.g. harder water has a more stable pH). And of course, there will be fish.

I’m pretty new to C++ so I don’t know a whole lot about when it is appropriate to represent something as a structure vs. an object, etc. But I’m thinking of making fish a structure. They’ll have a lot of properties, such as the range of temperatures they can withstand.

Right now the best way I can think to represent a range of temperatures is to give each fish an integer parameter “minTemp” and “maxTemp.” Or is there a data type that could do this for me?

Any other pointers?

Oh also, I’m probably gonna want to pick up a book on C++, because the internet tutorials aren’t quite doing it for me….any recommendations?

Observing members: 0 Composing members: 0

13 Answers

Blackberry's avatar

Why can’t I get a woman smart and hot like you?

jrpowell's avatar

I love fish tanks and this is a fantastic way to learn. I learned PHP/Ruby/MySQL by having a idea and doing it. If you can do a fishtank simulator that runs on OS X I will pay you 500 dollars. Hello motivation. And what Blackberry said.

And I am totally serious.

Mariah's avatar

@Blackberry Golly! Thanks!

@johnpowell Oh shit! Well all I had in mind (i.e. all I know how to do) is a text-based sim. I have a lot to learn. But you’ll be the first to know if I actually get anything good working.

PhiNotPi's avatar

Although I must question the entertainment value of a text-based fish tank, I like your idea.

First, I must confess that I do not know much about C++, I have only written a few programs in it, several years ago. So, any incorrect uses of technical terms should be disregarded. This is merely a list of my ideas for what you might decide to include.

The water should have five properties: salinity, temperature, waste level, hardness, and pH. Feel free to eliminate some of these if you feel that it is too detailed. Since there will only be one thing that you will record the salinity of, I might actually make each number a separate object. Why always refer to “propertiesofthefishtankwater.salinity” if you could always simply say “salinity”?

The waste level of the tank will slowly tick upwards, and the user would have to perform some sort of tank-cleaning/water change operation to return it to normal.

I like the idea of giving each fish an integer for each min and max value. It keeps things simple. I suppose that fish will start to die if the water qualities are outside of the right range. Each fish would have a hunger meter, which slowly ticks downward. The user would have some sort of feeding operation.

ratboy's avatar

If you’re thrifty, you may use Bruce Eckel’s books at no cost to you.

I’m not familiar with the latest features of C++ (there was a recent major revision), but I know that learning the Standard Template Library makes everything better.

I prefer to use classes for everything, but others may think differently. Some of these folks for example.

More.

andrew's avatar

Make them an object. Especially if you’re just starting out. Structs are good for containing things like an x,y point, etc. You’re going to start adding more and more functionality to your fish, so just make them an object.

Is there a particular reason you chose C++? Is it your first language? Having been programming in Objective-C for the last few months, I much prefer using a different, more modern language like Python or Ruby.

Mariah's avatar

@PhiNotPi Thanks. Yeah, my main purpose definitely wasn’t to create an entertaining finished product at least, not till @johnpowell popped in here with some good incentive… but to practice programming with an idea I found interesting. I was going to include temperature, hardness, pH, and nitrogen (basically the same as waste), but not salinity, because I was going to focus on freshwater fish. All your ideas are cool, basically what I had in mind!

@ratboy Thanks for the recommendations! I’ll check those out.

@andrew Thanks for the advice! I chose C++ somewhat arbitrarily. First because I wanted to learn an object-oriented language. Also because I already know a small amont of C++ from a high school class. And because I learned some C programming robots last semester. So I’m not dead set on it. Would Python or Ruby be good for a beginning programmer? I’m not brand new, I have taken one college course (we used Racket, which is pretty obscure), but I definitely feel like a newbie.

phaedryx's avatar

@Mariah, if your goal is to “learn an object oriented language” I would recommend Ruby. It is uncompromisingly object oriented. For example:

1. Integers are objects you can call methods on, 1.odd? is valid Ruby
2. false and true are (singleton) instances of FalseClass and TrueClass
3. Everything is done by passing messages (calling methods). For a new object it’s Object.new rather than having a new keyword (like in C++)

I don’t know much Python, but I would recommend it also over C++. C++, in my opinion, is object-oriented principles clumsily and inconsistently bolted onto C.

There is currently a movement in the Ruby community to get more women into programming (iirc, they teach Ruby first, then Ruby on Rails). My local Ruby user group is planning some workshops for later this year, could be the case in your area too. Might be a good start for resources at any rate. I’m also happy to give suggestions and recommendations.

(I know the Python community has something similar, but I don’t know what it is called.)

LostInParadise's avatar

It has been a while since I programmed in C++. I always found header files to be annoying and I was always running into problems with proper use of pointers. I know C++ is popular in scientific and engineering applications so I suppose it is worth knowing.

If you want an additional challenge, you may want to include simulation of schooling. There has been some interesting work showing that flocks of birds and schools of fish can hang together if each member of the group is only concerned about orienting itself to its nearest neighbors.

andrew's avatar

@Mariah Yeah, definitely do Python or Ruby. Since I see you’re interested in physics, you might want to try python.

The nice thing about both those languages is that you can get off the ground and running very quickly, so there’s a high reward-to-work ratio, which is really important when you’re starting out.

inkswinc's avatar

Any other pointers?” sorry, coudln’t resist.

I haven’t worked that much with C or C++ myself since I typically prefer other languages, but I believe the only major difference between a struct and a class is that a class can have methods and functions associated with it. The question you should ask yourself is if you want your fish to do anything themselves as opposed to having other things do things to them. Any time you want to have a fish have its own behavior, it should be in a class, because all a struct does is hold data together.

As for books on C++, I actually do recommend C++ for dummies. I have the 6 in 1 version, but that goes into a lot of concepts largely distinct from C++ such as the whole process of software engineering, so if you just want a book on C++ itself I’d recommend just getting the normal version.

Lastly, good luck on your programming quest, it’s a great and noble one.

phaedryx's avatar

@inkswinc fyi, you can put function pointers in a C struct.

This book on Object Oriented Programming with ANSI-C might be of interest.

I agree with @andrew, Python is a better choice for physics.

“Actually I made up the term ‘object-oriented’, and I can tell you I did not have C++ in mind.”—Alan Kay

phaedryx's avatar

found the link: pyladies

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther