Rational
class in C++. As before, I will be supplying a test program (rational_test.cpp
) as well as an exception class
(rational_exception.h
). In addition, I am providing a partial rational.h
to illustrate the proper parameter passing conventions used.
You might want to not look at these files initially, and see if you can transliterate them yourself from the Java version I supplied, using the parameter transmission
rules we discussed.
The basic specs are the same — the Notes section that follows contains language-specific differences. Any suggestions I provide are exactly that — suggestions. As long as you conform to the specs, and the CodeLab interface (see below), your are free to implement this in any manner you see fit.
The only additional function you need to write is a void print(ostream &os) const;
member function that prints out the Rational
. This replaces
the toString
of your Java implementation, and should have the same semantics.
rational_test.cpp
rational.h
<<
operator
rational.cpp
rational_exception.h
gcd.h
gcd
function here. Since it's an .h
file and potentially included by many sources in a program using it,
I declared it inline to prevent 'multiple definitions' link errors.
rational.h
and rational.cpp
as two separate exercises in CodeLab. As I will be supplying the other,
the function signatures in the .h
and .cpp
files must match (which is the purpose of this — to force you to use the
correct signatures). All the function definitions (bodies) should go into the .cpp
, i.e., none should be coded inline in the class declaration
in the .h
.
&
); this is true of both fundamental as well as class types
const … &
)
this
is a pointer, so to get to fields of the object, you need ->
and to refer to the object
you need *this
<<
operator for Rational
. This function relies on your print
function
described in the overview.
cout <<
where r
is of type Rational
rational.h.skeleton
that you can access (like all the other files provided)
in the link at the bottom of this page.
Rational
class declaration, with the operator below and declared inline
using namespace std;
in your .h
file (we will discuss the reason for this when we get to separate compilation in Part II).
The consequence of this is that anything from the Standard Library (e.g. ostream
, cout
, etc) must be fully qualified; i.e.,
must be prefixed with std::
.
rational.h
rational.cpp
h
/ .cpp
file; it may also be the case that I made a mistake in the signature). In the event of such (interface) errors, the compiler
will complain about the two functions signatures not matching. In order for you to figure out your error, you should know which signature is your
and which is mine, and where the compiler will be doing the complaining. In 4.1.1, the compiler will encounter the .h
file first (because the
#include
is encountered at the top of the .cpp
file), and thus the error will be manifested when it reaches my signature
in the .cpp
which won't match. Conversely, in 4.1.2 — where I am providing the .h
file, the error will again appear
in the .cpp
signature, which is yours this time.
vector
container of the Standard Template Library (STL). The program is an extension of an
upgrade of Lab 1.2 (which we present below).
Person
class has been introduced for the purposes of abstracting/modeling the name/id pair
.cpp
file would be need, however, we need to initialize the static data member (.cpp
file.
vector
of Person
objects.
auto
which is a double-edged sword:
sort
function from the algorithms section of the STL.
begin
and end
of a container.
These refer to the first element and the position AFTER the last element
end
is similar in semantics to the endIndex
parameter of the substring
method of
Java's String
class
sort
function is overloaded with a signature that only takes the iterators. This version
requires the element type (in our case Person
to possess a <
operator, but since we
haven't introduced operator overloading, we are forced to use the overloaded version presented in our example.
person
class into a Student
class, it introduces an additional Course
class.
If we had covered inheritance already (we just touched on it), we would make Person
; instead, we'll
simply use Person
as a starting point and create a whole new class for Student
.
students.dat
contains student transcript in the following format:
id name course_code credits grade … -1 id name …for example:
143 Jones 1234 2 C -1 123 Smith 4321 4 A 132 3 B -1(Note the formatting is for readability, everything could have been placed on one line; do NOT use
getLine
or any other line-oriented I/O; rather use
token-orieinted I/O, e.g. >>
).
Write a program that reads in the students, calculates their GPAs, and print out their transcripts in GPA order:
143 Smith: 3.57143 4321 (4 credits): A 132 (3 credits): B 143 Jones: 2 1234 (2 credits): C 2 records processed
course.h
A Course
class that represents an individual course, the course code, credits and grade. The behavior (member functions)
include getfunctions for the data members, a constructor, and a print
function, with the same signature as the one you wrote for
Rational
.
<<
operator to allow you to directly print a course using cout << course
:
inline std::ostream &operator <dd<(std::ostream &os, const Course &course) {course.print(os)C; return os;}
course.h
course.cpp
file (not that there's anything wrong with having a .cpp
file; I just want you to see as many
variations as possible). Alternatively, you can simply have the headers in the class declaration and place the function bodies after the class declaration
(still in the .h
file) — but then you need the inline
keyword (and don't forget the scope resolution operator in front of the
function name, since it;s not strictly within the class declaration).
student.h
/student.cpp
A Student
class that represents a student. This is similar to the Person
class
above, but also maintains a vector of Course
objects. The behavior is a constructor, get functions to access the data
members, and a getGPA
that calculates and returns the student's GPA (this is not strictly speaking a get function as it is
not merely accessing a data member; rather it's doing a calculation. There should also be the usual print
function, and
as you're all seasoned hackers and code pirates by now, YOU write the <<
on your own (I will be available for consultation if you get stuck).
print
and getGPA
are more than single-liners (they involve iterating through the vector), we are splitting this class into
.h
and .cpp
files
app.cpp
the application file. The application should declare a vector of Student
s, fill it from the data file,
sort it by GPA, and then print the output.