CS 112 - Week 5 Lecture 2 - 2022-09-22
TODAY WE WILL
* announcements
* continue intro to writing a C++ class!
* prep for next class
* TOMORROW during lab: Exam 1
* Watch for e-mails (after Exam 1!) as parts
of Homework 4 become available!
=====
* using separate compilation style,
for a class,
* we'll put its class definition in a header
file with suffix .h (PlayerChar.h)
* and we'll put the implementations of the
class' methods in an implementation file
with suffix .cpp -> we'll create PlayerChar.cpp
today
* note: CS 112 class style:
each constructor method should initialize
all of the new object's data fields
* be sure to #include the class' header file
in its implementation file!
* IN implementing each method,
you "tell" the compiler that you are
implementing a method of a class
by PRECEDING the method name in its
header with the class name and the
SCOPE RESOLUTION operator, ::
=====
function to_string, from C++ string library
* has two overloaded versions
* ONE expects an int, and returns a string version
of that int
* ONE expects a double, and returns an (ugly)
version of that double
* FUN FACT (added AFTER CLASS!):
* to_string is SUCH a useful method name, you might
be tempted to include it in a class you write!
* Are you in trouble if you want to use the string
library's to_string method in your class' implementation
of to_string?
...not if you use the SCOPE RESOLUTION operator to
let the compiler know you want the standard library's
(std) string class' version!
string MyClass::to_string() const
{
return "MyClass(blah blah, " + std::to_string(13) + ", blah blah)";
}