Hello,
I've run the following:
Code:
#include <iostream>
using namespace std;
class Rectangle
{
private:
int x, y, w, h;
public:
Rectangle();
int get_width() {return w;}
int get_height() {return h;}
};
class Area
{
private:
int area;
public:
Area();
int get_area();
};
Area::Area()
{
area = 0;
}
int Area::get_area()
{
area = myRectangle.get_width() * myRectangle.get_height();
return area;
}
Rectangle::Rectangle()
{
x = 0;
y = 0;
w = 10;
h = 20;
}
int main()
{
Rectangle myRectangle;
Area myArea();
cout << myArea.get_area() << endl;
cout << myRectangle.get_width() << endl;
return 0;
}
I get the following errors:
Code:
class5.cpp: In member function ‘int Area::get_area()’:
class5.cpp:33: error: ‘myRectangle’ was not declared in this scope
class5.cpp: In function ‘int main()’:
class5.cpp:51: error: request for member ‘get_area’ in ‘myArea’, which is of non-class type ‘Area ()()’
How do I access the get_width() and get_height() functions in get_area()? Since they are public in Rectangle, shouldn't I be able to access them in Area? If I comment out the two lines giving me issues, I can access myRectangle.get_width() from main. I don't understand why I can access get_width() in main but not in get_area().