User defined types, or structs as they are called in C++.
midpoint that
takes two points and returns their midpoint.point that encapsulated both the x and y
coordinates --- it's prototype would be
point midpoint(point a, point b);.
mid that encapsulated both alpha code and name
--- I'd have an array mid *A = new mid[20].
student ---
I'd just store it in an array of student
objects.
string, ifstream, and
ofstream objects that we've already been using are
structs rather than built-in types.
pointpoint would make
such a function simple and natural. We need to wrap up a
double for the x-coordinate and a
double for the y-coordinate into a single
object of a new type - point. Here's how that's
accomlished in C++:
|
|
main or of any other function definitions, and it must appear
before you try to use an object of type point. From the point of
this definition onwards you can use point as a new type.
double x within a point object named
P?P.xNote:
P.x is an object of type double, so anything you can
do with a double you can do with P.x!
struct are called data members.
point,
reading values into the object, and printing it out:
int main()
{
// Creates an object P of type point
point P;
// Reads & stores coordinate values
cout << "Enter x-coord: ";
cin >> P.x;
cout << "Enter y-coord: ";
cin >> P.y;
// Writes out point P
cout << "Point is (" << P.x
<< ',' << P.y << ")" << endl;
return 0;
}
cin >> P or cout << P?cin and cout know nothing about
the type point!
Answer: cin >> P.x >> P.y;
Note that cin >> P.x works perfectly well, because
cin is just reading into a double, which we know it
does just fine.
midpoint:
point midpoint(point a, point b)
{
point m;
m.x = (a.x + b.x)/2;
m.y = (a.y + b.y)/2;
return m;
}
Hopefully this code is pretty much self-explanatory. Notice that
by wrapping up two doubles in the type
point I can, in a sense, return two objects from a
function! Take a look at this complete
program that reads two points from the user and prints out
their midpoint.
midpoint(P,Q).
Q: What is the type of midpoint(P,Q)?
Answer: point
struct, cin doesn't know how to read that
type, and cout doesn't know how to write that type.
In fact, the compiler doesn't know how to do any of the implicit
or explicit conversions with your new types, so that neither
double(P), where P is an object of type
point, nor point(j), where
j is an object of type int will be
recognized.
The only things the compiler knows how to do with your user defined type are
assignment using the = operator, and copy (i.e.,
pass-by-value) arguments in function calls. These are done by
assigning/copying each data member independently.
So, the code on the right works nicely. We also saw the |
|
new is the same. For example, we can write:
point* A = new point[10];
Likewise, parameter passing is the same, parameter type matching for function
overloading is the same ... all of these things you've already learned still
apply.
midpoint function with arrays of two doubles ---
believe me, it'd be painful! Where things really get interesting is when we
wrap up objects of different types in one object, because there we really can't
use arrays like we could for points.
mid as follows:
struct mid
{
int alpha;
string first, last;
};
Notice that this struct has three data members, one of type int
and two of type string.
Mids.txt contains the names and alpha codes of
some past Midshipmen. I want to write a program that will read
that data and store it in an array for later processing.
To test what I've done, we'll simply allow the user to enter an alpha, and we'll return the name of the Mid with that alpha, or an error message if none is found. Creating the array and reading in data from the file is easy:
// Create and array of 41 Mids
mid* A = new mid[41];
// Open file
ifstream fin("Mids.txt");
// Read in mids
for(int i = 0; i < 41; i++)
fin >> A[i].alpha >> A[i].last >> A[i].first;
int search(mid* A, int n, int alpha)
{
for(int i=0; i<n; i++)
{
if( A[i].alpha == alpha )
return i;
}
return n;
}
In the main() function, we can call search() and print the search
result as follows:
int k = search(A, 41, alpha);
// print result of search
if (k == 41)
cout << "No Mid with that alpha was found!" << endl;
else
cout << A[k].first << " " << A[k].last << endl;
struct vs class
C++ has two names / keywords for user-defined
types: struct and class.
They are more or less the same (except that in structs members
are public by default, and in classes they are private by
default. Of course that distinction won't make any sense at
this point, but in case you come back and read this later ...),
but historically come from different places. C++
gets struct from C, the language that C++
extends. The term class comes from
"object-oriented programming", which is a style of programming
C++ supports, but which we do not cover in this course.
We are going to use the keyword struct to get you familiar with what you'll see in the context of C programming in your Systems Programming course. Of course this means that in your Objected Oriented Programming course (taught using Java) the keyword class will be familiar.
Enter triangle vertices: (0,0) (0,1) (1,0) Midpoint triangle verts: (0,0.5)(0.5,0.5)(0.5,0)Notice how this solution defines functions for writing and reading points!
struct to write a program that prints out the
10 youngest congresspeople, first and last
names. Here is a solution.