Although, this is not going to be a major theme for this course, I'd like to
show it to you so that you understand that we really can build new types in C++
if we want to. Also, I should note, this is why + and >> and so on work
with C++ string
objects. The implementers of the string library
defined all these operators for their nice string type.
q
to quit.
First solution. My first solution is a
pretty simple program.
The meat the program is: |
Operator overloading.
While it may be simple, it would be nice to be able to write the function as if
point were a built-in type, meaning that I could add points
p and m by saying p + m .
|
|
|
p + m
means for point
objects
p
and m
. Doing this is quite easy once you
understand the following:
a + b
is just the same as the function call
operator+(a,b)
in C++.
So if you want to tell the compiler what +
means for two
point
objects, you need to define the function
operator+(point a,point b)
--- i.e. overload the
+
operator for point
s. The prototype is
clear:
point operator+(point a, point b);
... at least I hope it's clear that we should return a point when
we add two points. The function definition is ... just like any
other function definition:
point operator+(point a, point b)
{
point S;
S.x = a.x + b.x;
S.y = a.y + b.y;
return S;
}
point
struct over and over, and
you'll like being able to add point
s. Wouldn't it be nice to
define the midpoint
function like this:
point midpoint(point a, point b)
{
return (a + b)/2.0;
}
A Π B
,
where "Π" stands for some operator, then that is equivalent
to a function call operatorΠ(A,B)
.
So, to subtract two points we'd define
point operator-(point A, point B);
... and to compare two points with less than we'd define
bool operator<(point A, point B);
... or to multiply a point (on the left) by a real number (on
the right) we'd define
point operator*(point A, double w);
operator+
for two
point
objects, what else would you need?
Well, (a + b)
is an object of type
point
, and I'm dividing it by an object of type
double
, so I need to define
operator/(point,double)
. What type of object should
be returned here?
point operator/(point P, double z)
{
point Q;
Q.x = P.x / z;
Q.y = P.y / z;
return Q;
}
istream& operator>>(istream &in, point &A)
{
char c;
return in >> c >> A.x >> c >> A.y >> c;
}
ostream& operator<<(ostream &out, point A)
{
return out << '(' << A.x << ',' << A.y << ')';
}
The prototypes of these two should actually make some sense.
For example, we've talked before about how cin >> x
actually evaluates back to cin
.
To wrap all of this function overloading stuff up, consider this example: point.h and point.cpp.
If your program creates and array to store data in a file, and the file (rather than you the programmer) is what determines how many elements ought to be in the array. The size of the array is bound to change from run to run of the program, because the file may change.
A static array wouldn't work for a program that reads in data from a file that contains an unknown amount of information. How big should you the programmer make the array? You don't know.
Q: For a static array A
, do you have to delete []
A;
, when you don't need it anymore?
No. (Only when A is created by new int [6]).
struct Quad
{
char label;
point vert[4];
};
If A
is a static array, the pointer A
cannot be changed.
To understand the difference between this version of
Quad
and the previous version, consider this picture:
Note:
Quad
object; the array is outside of the Quad object,
somewhere else in memory.
vert
is embedded in the
Quad
object.
The above picture really tells you all you need to know to understand the difference between using static and dynamic arrays ... when you really can use static arrays.
Q: Can you change the contents of a static array?
A: Of course, yes
Q:
Suppose that I have a Quad object S that contains the
label 'Q' and the vertices (0,0) (1,0) (1,1) (0,1). I then print out
S and then R . What will I get?
A:
It depends whether I'm using the static version of
|
|
Dynamic Version | Static Version |
Q:
How does swap(A,B)
behave differently for
two Quad
s, A
and B
,
with the dynamic versus static array versions of
Quad
?
A: Once again, the above picture should tell you that while the result is the same, a lot more work gets done in the static case, where the entire contents of the arrays are swapped, rather than simply the pointers.
int prime[10] = {2,3,5,7,11,13,17,19,23,29};
Note: this is purely about static arrays, it doesn't
concern structs at all.
Here's my solution.