Is it possible to assign objects of different class types to one another? I'm trying to assign a member of the Ball class representing the 'ball' to an element on the grid which is an array in the Grid class. I'm trying to do this by overloading the assignment operator but it's not compiling.
The program I'm trying to write is a game of Pong. Here's the code so far:
You wrote an assignment operator for Ball objects, but you are trying to assign a Ball to a *(grid + 5). Dereferencing with * means value pointed by. First C++ would need to know how to add a Grid with an integer, then it would need to know how to dereference that sum. Since derefencing is just for pointers, it probably would not compile. Even if it could be dereferenced, you would need an assignment operator in Grid that could accept a right hand side Ball.
In this situation I would choose for the Grid class to own a private member of type Ball. Of course, additional methods would be required to move the ball around in Grid's private character array. But if I really stop to think about Ball, why would I want to make a class for something that is really just a simple character? In the end, I would decide to accept a char in Grid's constructor and also have a default ball character in case they do not pass a ball character or they pass a character that is already used in the game.
In object oriented programming, the first thing you want to do is plan out how your classes are going to interact with each other and determine their relationships. When the program is complex enough UML diagrams can be a tremendous help, but even for simple programs they can still shed light. http://agilemodeling.com/artifacts/interactionOverviewDiagram.htm
Thanks kevin. I tried what you suggested and simplified things, so that the Grid class now contains chars to hold the ball and paddles.
When creating the grid, I've set the ball and paddles to certain positions on the grid. For the grid as it is now, it's fine, but if the dimensions are changed, the positions of the ball and paddles become skewed. This is because the positions they are set to are hard coded. Do you know how I might set the positions without hard coding them, so that if the grid dimensions are altered the ball and paddles are set to a new appropriate position?
Grid::row and Grid::col sound too much like an individual row or column. I would consider renaming them to Grid::nrows and Grid::ncols or something similar to reflect that they are counts and not specific indices.
For the grid as it is now, it's fine, but if the dimensions are changed
This is just basic pong, right? Why would the grid size need to be altered in the middle of a game? If you are trying to make some sort of Pong++ (my lack of creativity is indeed real), what would you like to happen to the game grid (including balls and paddles)? If you lack a vision of what you want the finished code to do, it is not possible for me to help you until you plan out your vision. All I can do is make guesses at what you want to happen when the gird resizes.
No, no, this is just basic pong. I don't want the grids dimensions to be altered during the game. I'm just looking for a way to set the ball and paddles offsets without hard coding those values, so that if the grid just so happens to be of a different size to what I have now, the ball and paddles won't end up being randomly (probably incorrectly) placed on the grid. My 'vision', as it were, is to make a bog standard game of pong in the console. Can you think of a way to place the ball and paddles without hard coding their positions?
Learning a GUI library would be the best way to avoid flickering. There are some libraries that specifically target the console such as ncurses. You are already on the right track with "hard coding" the ball and paddles by using math formulas. Try using different grid sizes and tweaking your formulas until they suit your fancy.
I was hoping there might be some "one size fits all" formula, but oh well. If using a graphical interface is the best way to avoid flickering, maybe I should go one step further than ncurses and learn SFML. What do you think, would SFML be appropriate for this as well?
The first thing I did when I started learning SFML was make a pong game. Then two parts I found difficult was learning a new library and coding a pong game. You already have a pong game, so you could easily learn SFML.