C++ Lecture 3
[Previous Lecture]
[Lecture Index]
[Next Lecture]
point.h
#ifndef POINT_H
# define POINT_H
class Point {
public:
Point(int x, int y);
int getX() const;
int getY() const;
void setX(int nx);
void setY(int ny);
void move(int dx, int dy);
double distanceTo(const Point &p) const;
/* ... */
private:
int x, y;
};
#endif /* POINT_H */
point.cc
#include <math.h>
#include "point.h"
Point::Point(int x, int y)
{
Point::x = x;
this->y = y;
}
double
Point::distanceTo(const Point &p) const
{
double dx = x - p.getX();
double dy = y - p.getY();
return sqrt(dx * dx + dy * dy);
}
int
Point::getX() const
{
return x;
}
int
Point::getY() const
{
return y;
}
void
Point::setY(int y)
{
this->y = y;
}
void
Point::setX(int x)
{
this->x = x;
}
void
Point::move(int dx, int dy)
{
x += dx;
y += dy;
}
rect.h
#ifndef RECT_H
# define RECT_H
# include "point.h"
class Rect {
public:
Rect(int x, int y, int width, int height);
void move(int dx, int dy);
bool pointInside(const Point &p) const;
// bad: exposes implementation details...
const Point &getUpperLeft() const {
return upperleft;
}
/* ... */
private:
Point upperleft;
int width, height;
};
#endif /* RECT_H */
rect.cc
#include "point.h"
#include "rect.h"
Rect::Rect(int x, int y, int width, int height)
: upperleft(x, y)
{
this->width = width;
this->height = height;
}
void
Rect::move(int dx, int dy)
{
upperleft.move(dx, dy);
}
bool
Rect::pointInside(const Point &p) const
{
if (upperleft.getX() <= p.getX()
&& p.getX() <= upperleft.getX() + width
&& upperleft.getY() <= p.getY()
&& p.getY() <= upperleft.getY() + height)
return true;
return false;
}
Misc Notes on Classes
- instance variables can be instances of other classes
- classes can be nested
- methods and variables must have distinct names
- several methods can have the same name if they take
different arguments
- kinds of methods (terminology):
- accessors: don't change the instance
- mutators: may change the instance
`accessor' methods should be declared const
(also constructors and destructors)
- be careful when (implicitly) copying instances
Can happen when
- passing class parameter by value
- returning class by value
- assigning one instance from another
Basic Object Oriented Concepts
- OO programs define, create and manipulate objects
- objects have
- a class defines an object type:
it the behaviour and the kind of state kept
- an instance of a class is an object (has identity)
- identity: where instance is stored in memory
- behaviour: specified by instance methods (defined in the class)
- state: stored in instance variables (defined in the class)
How to do OO design
- need to recognize an object
- examine problem, identify possible objects
(nouns often make good objects)
- pick ones that are useful:
- localize behaviour: if several objects share behaviour,
perhaps the shared aspect can be another object
that can be used by both
- work well with each other
- sometimes necessary to create non-obvious objects
to encapsulate diverse information
What makes a `good object'?
- interface hides details of implementation
- user can't create dependencies on implementation details
- interface allows several different possible implementations
- clear from interface how object is to be used
- minimize complexity of interface
- object is easy to use
- interface ensures proper use
- object is useful in several situations (either within a
single program or in several different programs)
- object simplifies the design or implementation
of another object
[Previous Lecture]
[Lecture Index]
[Next Lecture]