Hello,
I am trying to pass Rectangle class to Area class so I can use the data types from Rectangle in Area.
Code:
#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.
Code:
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?