Charlotte Curtis March 18, 2024
Textbook Chapter 13
void clear_list(Node *&head) { while (head) { Node *temp = head; head = head->next; delete temp; } }
Textbook Sections 10.2-10.3
class
"The act of separating the essential qualities of an idea or object from the details of how it works or is composed" - Nell Dale and Chip Weems
Say I provide a header file and precompiled object file for the following functions:
// Reads a date formatted as year-month-day from source void read_date(Date &date, std::istream &source); // Writes the date to the output stream as year-month-day void write_date(const Date &date, std::ostream &out);
int
+
-
*
/
%
==
!=
<
>
<=
>=
=
++
--
Note: According to our textbook, built-in types are ADTs
IntList
1
insert
delete
retrieve
search
length
sort
print
.h
.o
To implement an ADT, we need to define a class
A class is a blueprint for creating objects, much like how a struct is a blueprint for creating data structures
struct
struct STime { int hours; int minutes; int seconds };
class CTime { int hours; int minutes; int seconds; void write(std::ostream &out); };
A class is a type of object, just like int or string or Node
string
Node
Member functions are accessed using . or -> just like member variables
.
->
STime now = {5, 0, 0}; // struct CTime bedtime; // class - can't use {} to initialize bedtime.hours = 11; // uh oh, this doesn't work either!
class ClassName { public: // Public member functions (maybe some variables) private: // Private member variables and functions };
public
private
Time
In general, anything functions that the user of the class needs to access should be public, and anything else should be private - including member variables!
hours
minutes
seconds
write(std::ostream &out)
set(int h, int m, int s)
int compare(Time other)
void increment()
All this being said, the C++ FAQ recommends avoiding trivial getters/setters
time.h
Common for a class to have its own header file and implementation file (.cpp)
.cpp
#ifndef TIME_H #define TIME_H class Time { public: void write(std::ostream &out); void set(int h, int m, int s); int compare(Time other); void increment(); private: int hours; int minutes; int seconds; }; #endif // TIME_H
How much memory is allocated when the following code is executed?
class Student { public: void set(int id, const char *name); void write(std::ostream &out); private: char name[20]; int id; };
What is the main difference between a struct and a class?
#include
// In main.cpp Time now; // object on the stack Time *later = new Time; // pointer to object on the heap now.set(3, 30, 0); // set the time for now later->set(5, 0, 0); // set the time for later
// in time.cpp ReturnType ClassName::func_name(Parameters) { // Function body }
::
std::ostream
std::cout
set
Time::set
// in time.cpp void Time::set(int h, int m, int s) { hours = h; minutes = m; seconds = s; }
this
this->hours = h;
Say we want to find the index position of the word "World" in a string:
hello = "Hello, World!" pos = hello.find("World")
string hello = "Hello, World!"; int pos = hello.find("World");
find
hello
How is find implemented? No idea! Thanks, abstraction.
// in main.cpp Time now; now.set(3, 30, 0);
now
Time *later = new Time; later->set(5, 0, 0);
(*later).set(5, 0, 0);
We have a few more functions to implement:
write
compare
increment
The funkiest one is compare - in addition to the default this parameter, it needs another Time object