Jump to content
  • Advertisement
Sign in to follow this  
Hop chewer

classes

This topic is 3324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am trying to pass Rectangle class to Area class so I can use the data types from Rectangle in Area.
[source lang = "cpp"]
#include <iostream>

using namespace std;

class Rectangle
{
	private:
	int x, y, w, h;
	
	public:
	Rectangle();
	void set_data(int, int, int, int);
};

class Area
{
	private:
	int area;
	Rectangle *myRectangle;
	
	public:
	Area(Rectangle *myRectangle);
	void get_area();
};

Area::Area(Rectangle *myRectangle)
{
	area = 0;
}

void Area::get_area()
{
	myRectangle->set_data(0, 0, 5, 10);
	area = myRectangle->w * myRectangle->h;
}

Rectangle::Rectangle()
{
	x = 0;
	y = 0;
	w = 0;
	h = 0;
}

void Rectangle::set_data(int a, int b, int c, int d)
{
	x = a;
	y = b;
	w = c;
	h = d;
}

int main()
{
	Rectangle myRectangle;
	Area myArea(&myRectangle);

	return 0;
}
This is how I am handling the width and height of the rectangle while calculating the area. area = myRectangle->w * myRectangle->h; I am getting the following.
class4.cpp: In member function ‘void Area::get_area()’:
class4.cpp:8: error: ‘int Rectangle::w’ is private
class4.cpp:34: error: within this context
class4.cpp:8: error: ‘int Rectangle::h’ is private
class4.cpp:34: error: within this context
How do I calculate the area using w and h from Rectangle? How would I access x and y from class Rectangle inside class Area to print the coordinates and area of the rectangle?

Share this post


Link to post
Share on other sites
Advertisement
Why do you want a separate Area class? It would be much cleaner to have the Rectangle know how to calculate its own area, then you don't have to violate encapsulation by accessing private members in another class. Also, you should use the constructor to set up class data instead of a separate function:

class Rectangle
{
private:
int x_, y_, width_, height_;

public:
Rectangle(int x, int y, int width, int height)
: x_(x), y_(y), width_(width), height_(height)
{
}


int Area() const
{
return width_ * height_;
}
};

Share this post


Link to post
Share on other sites
I am learning about classes and doing this is a learning exercise. The goal is to learn how to pass one class to another to use that data in the second class. The end goal is to be able to pass the class of an object to the class of another object for collision detection.

Share this post


Link to post
Share on other sites
What you are asking to do is textbook bad design.

If one class has information required to perform a calculation, then that class should be responsible for that calculation. Splitting up data storage from logic that uses that data is entirely against the point of objects/classes in the first place.

Share this post


Link to post
Share on other sites
So if I have a pong game, and the Ball class handles the movement of the ball and I want to check for a collision with the paddle, how would I send the position and area of the paddle from the Paddle class to the Ball class? Or is there a better more prefered way to handle the collision?

Share this post


Link to post
Share on other sites
Quote:
Original post by Hop chewer
So if I have a pong game, and the Ball class handles the movement of the ball and I want to check for a collision with the paddle, how would I send the position and area of the paddle from the Paddle class to the Ball class?


By... sending it. (The fact that you have this word in mind already almost directly tells you what to do.) When you want someone to do something for you, and that person needs some information that only you know in order to do that job, what do you do? Why, you tell that person the information. And the other person, presumably, anticipates that information.

In C++, a function anticipates information by declaring that it accepts parameters (which describe the information). You give information to a function by passing values for those parameters. Every object has access to its own members, so it can pass (the values of) its own members as parameters.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!