Thread: passing one class to another

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    passing one class to another

    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?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    area = myRectangle->w * myRectangle->h;
    w is a private member of the Rectangle class. You don't have access to this variable from the Area class. You can either add an accessor to the Rectangle class like this:
    Code:
    int GetWidth() { return w; }
    or you can make Area a friend class of the Rectangle class. This would give the Area class access to Rectangle's private variables. This is done by putting
    Code:
    friend class Area;
    somewhere in the Rectangle class. Note that the preferred way would be to go the accessor route.

    Also, you don't appear to be saving the passed Rectangle in Area's constructor. You may want to change it to:
    Code:
    Area::Area(Rectangle *myRectangle)
    {
    	area = 0;
            this->myRectangle = myRectangle;
    }
    Note that the this specifier is needed since the myRectangle member is shadowed by the parameter (they have the same name).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    Here is my updated code. Everything is working now. thanks.
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Rectangle
    {
    	private:
    	int x, y, w, h;
    	
    	public:
    	Rectangle();
    	void set_data(int, int, int, int);
    	int get_width() {return w;}
    	int get_height() {return h;}
    };
    
    class Area
    {
    	private:
    	int area;
    	Rectangle *myRectangle;
    	
    	public:
    	Area(Rectangle *myRectangle);
    	int get_area();
    };
    
    Area::Area(Rectangle *myRectangle)
    {
    	area = 0;
    	this->myRectangle = myRectangle;
    }
    
    int Area::get_area()
    {
    	myRectangle->set_data(0, 0, 5, 10);
    	area = myRectangle->get_width() * myRectangle->get_height();
    	return area;
    }
    
    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);
    	
    	myArea.get_area();
    	cout << "Area = " << myArea.get_area() << endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Passing array through class constructor
    By sonicdoomx in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 01:42 PM
  3. Passing Pointer To Class In A Function
    By prog-bman in forum C++ Programming
    Replies: 11
    Last Post: 07-25-2004, 06:34 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Passing functions by value in a class...
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2002, 05:57 PM