GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Apr-2009, 09:17
scuzzo scuzzo is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 19
scuzzo is an unknown quantity at this point

Operator Overloading


I'm sinking fast and need some help. I'm taking my second semester of c++ programming and for this assignment I need to create a class and use overloaded operators to input and output and ex.
Define and implement a class named Rectangle. Its partial declaration is given below:

CPP / C++ / C Code:
class Rectangle
{
private: 
	Point p;			// top-left corner of the rectangle
	float width, height;	// width and height of the rectangle

public:
	// constructors
	Rectangle();	// default values: p=(0,0) width=1 height=1
	Rectangle(Point pt, float w, float h);

// --------------------------------------------
	// input and output
// --------------------------------------------
// Implement >> and << here as non-member functions. 
// Operator << should plot(draw) the rectangle.  
// Operator >> should prompt for user input of the x-y
// coordinates of the top-left corner, width and height of a 
// rectangle.

// --------------------------------------------
	// *: scale the rectangle
// --------------------------------------------
// Implement * as a non-member function
// Implement *= as a member function.

// --------------------------------------------
	// +: move the rectangle to another location
// --------------------------------------------
// Implement + as a non-member function
// Implement += as a member function.

// --------------------------------------------
// Compare two rectangles
// --------------------------------------------
// Implement < and == operators as non-member functions, 
// where R1<R2 is true if R1 is contained in R2, 
// R1==R2 is true if R1 and R2 overlaps
};


Write a program to test your class. The program should provide a menu to a user to read, plot, scale, and move a rectangle, as well as enter another rectangle for comparison. The following is an example when running the program.

I am so very very lost. Can anyone suggest any resource that will go VERY slowly or give me some pointers on where to start.
Last edited by admin : 10-Apr-2009 at 10:52. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 10-Apr-2009, 10:18
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Operator Overloading


Here is how you would apply the constructors and the scale:

CPP / C++ / C Code:
// Rectagle.h

class Point
{
public:
	int X;
	int Y;

	Point& operator=(const Point& p)
	{
		if(this != &p)
		{
			X = p.X;
			Y = p.Y;
		}

		return *this;
	}
};


class Rectangle
{
private:
	Point p;
	float width, height;

public:
	// Constructors
	Rectangle();
	Rectangle(Point pt, float w, float h);

	// Member operators
	Rectangle& operator*=(float scale);

	// Non-member operators
        // NOTE: Declare as a "friend" so that it can access Rectangle's private members.
	friend Rectangle operator*(const Rectangle& rectangle, float scale);
        
};

CPP / C++ / C Code:
// Rectangle.cpp

#include "Rectangle.h"

// Constructor
Rectangle::Rectangle()
{
	p.X = 0;
	p.Y = 0;
	width = 1.0;
	height = 1.0;
}

// Constructor
Rectangle::Rectangle(Point pt, float w, float h)
{
	p.X = pt.X;
	p.Y = pt.Y;
	width = w;
	height = h;
}

// Non-member scale operator
Rectangle operator*(const Rectangle& rectangle, float scale)
{
	return Rectangle(rectangle.p, rectangle.width*scale, rectangle.height*scale);
}

// Member scale operator
Rectangle& Rectangle::operator*=(float scale)
{
	width *= scale;
	height *= scale;
	return *this;
}
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:17.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.