# classes

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

## 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 on other sites
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 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 on other sites

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 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?

theTroll

##### Share on other sites
Quote:
 Original post by Hop chewerSo 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.

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633270
• Total Posts
3011158
• ### Who's Online (See full list)

There are no registered users currently online

×