Today's lecture and studio focus on how overloading of template and non-template functions and operators can be managed to achieve the benefits in flexibility and specificity that overloading offers, while avoiding ambiguity. Type conversions and scoping also impact overloading, and we will consider those issues as well.
Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.
As you work through these exercises, please record your answers, and when
you finish them please log into Canvas, select this course in this semester, and
then upload a .txt
file containing your answers on the Canvas page
for this studio assignment. Only one submission per team, please, and if
you need to re-submit it the person who originally submitted the studio should
please be the one to do that.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
First, ssh into shell.cec.wustl.edu
using your WUSTL
Key id and password and then use qlogin
to log into one
of the Linux Lab machines and then confirm that the version of g++
there is correct (as you did in Studio 0). Then
cd
into your directory for this course this semester, create a new
subdirectory for this studio, and cd
into it.
Copy into that subdirectory the Makefile
from one of your previous
studios, and as you develop your code for this studio please upate it so that
it builds an executable program file named studio14
.
Add a source and header file in which you will declare and define a class
that has a private member variable of type std::string
and a public
constructor that takes in a reference to a const std::string
and
uses that to intialize the private member variable, in its base/member initialization
list.
In those same files, declare and define a public left shift operator
(operator<<
) that takes a non-const reference to a
std::ostream
and a reference to a const object of your class type,
inserts the object into the std::ostream
, and returns a non-const
reference to the std::ostream
. In your class declaration, declare
that operator to be a friend of the class.
Add another source file to your directory, and in it define a main function that
declares two objects of the class type, each initialized with a different string,
and prints their contents with a space between them using std::cout
before then returning a descriptively named symbol that has value 0 to indicate
success. Compile and run your program, and as the answer to this exercise, please
show the output that your
program produced.
Add a public const member operator<
to your class that takes a
reference to a const object of the class type and returns true if and only if the
object's string member variable is less than the string member variable of the
object that was passed to it.
In your program's main function, use that operator to determine which of the two objects has the lesser string variable, and output the result of that comparison to the standard output stream. Compile and run your program, and as the answer to this exercise please show the output it produced.
operator<
that
is a friend of the class, takes in two references to const objects of the
class type, and achieves the same comparison.
Compile and run your program, and confirm that the output is the same as
in the previou exercise. As the answer to this exercise, please show the
declaration and definition of the non-member operator<
.
Uncomment the declaration and definition of the member version of
operator<
, compile your program, and note the warning or error that
occurs. If it only gave a warning (i.e., the code compiled) please run your program
and confirm that the output is the same as in the previous exercise. As the answer to
this exercise please (1) show the warning or error you got, and (2) explain briefly
why it occurred.
Again comment out declaration and definition of the member version of
operator<
.
Add a template header and source file to your directory, with the template header
file including the template source file, and update your Makefile
accordingly. In the template header and source file declare and define a function
template with single type parameter that has a void
return type, takes
a reference to a non-const std::ostream
and a reference to a const
variable of the parameterized type, and inserts the variable into the ostream.
In the header file for your program's main function, include the template header file, and in your main function replace all the places where it inserts an object of the class type into an ostream with a call to that function template.
Compile and run your program, and confirm that it produces the same output as in the previous exercise (or the one before it if the previous one didn't compile). As the answer to this exercise please show the declaration and definition of the function template.
Modify the function template that you added in the previous exercise so that it
also inserts a short C-style string into the ostream
to indicate that
printing happened from the template function.
In the header and source files for your program's main function, declare and define
a non-template function that has the same name as the function template, has a
void
return type, takes a reference to a non-const
std::ostream
and a reference to a const object of your class type for
this studio, inserts the object into the ostream, and also inserts a short C-style
string into the ostream
to indicate that
printing happened from the non-template function.
Put that declaration and definition into a new namespace, and in your program's main function prepend the name of the namespace and the scoping operator to one of the calls that prints out an object of the class type. Compile and run your program and as the answer to this exercise please show the output it produced (which should show both C-style strings indicating that the non-template function was used for one of the calls and that the template function was used for the others).
For this studio, please turn in the following:
Page posted Sunday October 30, 2022, by Chris Gill.