C++/classes in C++
Expert: vijayan - 10/12/2009
QuestionHello,
I would like to ask you please about classes in C++. I'm trying to basically work with two classes that are named Rectangle and Square, respectively. The rectangle class will hold the coordinates of the upper left point and the width and height as attributes. The square class should hold the upper left point as attribute and the sidelength as float value. All points should be stored in a Point2D class with attributes (x,y). These coordinates are floating values and hence, the attributes should be of type double.
Now, I need to design the classes Rectangle and Square with an
1. all argument constructor
2. default constructor (should initialize all point coordinates to 0 and all dimensions to 1)
3. constructor with one argument (top left coordinate, dimensions default to 1)
4. a copy constructor.
5. Rectangle must also have a constructor which allows one to construct a rectangle from a Square (a square is also a rectangle).
6. Then I need to create a function intersection in both, Rectangle and Square. The function should return a rectangle representing the area of intersection.
Now, here is what I have done from code so far and I dont know how to do the rest of it :(
#include <iostream>
using namespace std;
class Point2D
{
public:
double x, y;
Point2D()
{
x = 0;
y = 0;
}
};
class Rectangle
{
//class attributes
Point2D upperLeftPntR;
double height;
double width;
public:
//default constructor
Rectangle()
{
upperLeftPntR.x = 0;
upperLeftPntR.y = 0;
height = 1;
width = 1;
}
//all argument constructor
Rectangle(Point2D upperLeftPntR, double height, double width);
//copy constructor
Rectangle(const Rectangle& cRec) {};
//one argument constructor
//constructor of a rectangle from a square
Rectangle(const &Square):UpperLeftPntR.x(Square.UpperLeftPntS.x), UpperLeftPntR.y(Square.UpperLeftPntS.y), witdth(sideLength), height(sideLength){}
};
class Square
{
//class attributes
Point2D upperLeftPntS;
float sideLength;
public:
//default constructor
Square()
{
upperLeftPntS.x = 0;
upperLeftPntS.y = 0;
sideLength = 1;
}
//all argument constructor
Square(Point2D upperLeftPntS, float sideLength);
//copy constructor
Square(const Square& cSqr) {};
//one argument constructor
};
AnswerOk. This is homework, isn't it? I'll help you get going, but you really need to try and complete this on your own - or you will not be learning anything.
class Point2D is fine, but writing it this way would make things easier later on in initializing the object. I presume that you already know about default values for parameters - if not read up on it, for instance here:
http://www.cplusplus.com/doc/tutorial/functions2/
class Point2D
{
public:
double x, y;
Point2D( double xx = 0.0, double yy = 0.0 ) : x(xx), y(yy) {}
};
The class Rectangle would look something like this (also read the comments to understand what is going on)
class Square ; // (forward) declaration of square
// class square needs to be declared before we can use it in name
// see
http://www.devx.com/tips/Tip/12583
class Rectangle
{
//class attributes
Point2D upperLeftPntR;
double height;
double width;
public:
//default constructor
// upperLeftPntR is initialized to (0,0) by the default constructor for Point2D
Rectangle() : height(1.0), width(1.0) {}
// one argument constructor
// upperLeftPntR is initialized by using the copy constructor for Point2D
Rectangle( const Point2D& ulpt ) : upperLeftPntR(ulpt), height(1.0), width(1.0) {}
//all argument constructor
// upperLeftPntR is initialized by using the copy constructor for Point2D
Rectangle( const Point2D& ulpt, double hh, double ww )
: upperLeftPntR(ulpt), height(hh), width(ww) {}
//copy constructor
// upperLeftPntR is initialized by using the copy constructor for Point2D
Rectangle( const Rectangle& cRec ) : upperLeftPntR(cRec.upperLeftPntR),
height(cRec.height), width(cRec.width) {}
//one argument constructor
//constructor of a rectangle from a square
// square needs to be declared before we can use it in name
// we can define (implement) the constructor only after the
// definition of square.
Rectangle( const Square& sq ) ;
};
class Square
{
//class attributes
Point2D upperLeftPntS;
float sideLength;
public:
// ....
// deliberately left blank - do this on your own
// look at the class Rectangle for an example
};
//one argument constructor
//constructor of a rectangle from a square
// Once the class Square has been defined, we can implement this constructor.
// however, the constructor needs to access the upper left point and side length
// of the square which are private. We need to have accessor functions which will
// allow us to retrieve these values. eg.
// const Point2D& Square::top_left() const { return upperLeftPntS ; }
Rectangle::Rectangle( const Square& sq )
{
// write this on your own.
}
Once this much has been done, it compiles without errors etc., add the last part:
Create the function intersection in both, Rectangle and Square. The function should return a rectangle representing the area of intersection.