CS 250 Computer Programming and Problem Solving - FALL 1998 |
---|
[ Home | Syllabus | Notes | Glossary | CS250 WEB Version Home Page ]
FixedString: Example Revised
Using FixedString in another class (version 2)
Let's fix the problem with the lack of assignment of
C-style strings to FixedString objects
(which caused the need for a temporary FixedString object in SetLast and
SetFirst).
Header file is the same.
//########################################################### // NAME: class containing a person's name (version 2) // Name contains a first and last name // Implements input/output overloads and access and modifier functions // has both default and tow non-default constructors allowing // construction from both C-style and FixedString objects // Programmer: Chris Wild //###########################################################
#include "Name.h"
// skip the unchanged stuff
void Name::SetFirst(const char * first) // PRE: first is a string less than MaxSize // POST: sets first name to copy of first { firstName = first; }
void Name::SetFirst(const FixedString & first) // POST: sets first name to copy of first { firstName = first; }
void Name::SetLast(const char * last) // PRE: last is a string less than MaxSize // POST: sets last name to copy of last { lastName = last; } void Name::SetLast(const FixedString & last) // POST: sets last name to copy of last { lastName = last; }
// FIXEDSTRING.H - FixedString class definition // Fixed-length string class: allocate 256 characters for every string // Makes strings first class objects: Version 1 // Construct from C-style strings // or by Copying another FixedString Object // Overload I/O operators
#ifndef FIXEDSTRING_H // see page 96 for explanation #define FIXEDSTRING_H #include <iostream.h> #include <iomanip.h> const int MaxSize = 256; // Maximum allowable string size
class FixedString { // skip over friend function prototypes
public: // skip constructors FixedString & operator =(const char * s); // assign a C-style string to a FixedString // POST: value of this object is a copy of "s" and // a reference to this object is returned for further use
private: char str[MaxSize+1]; // String characters }; #endif
// skip (most) unchanged stuff
FixedString::FixedString( const char * s ) // Construct from a C-style string. // PRE: s is a string less than MaxSize characters // POST: constructs new string copying string "s" // if "s" is more than 256 characters, only uses first 256 { strncpy(str ,s,MaxSize); str[MaxSize] = '\0'; // in case "s" is too big }
FixedString & FixedString::operator =(const char * s) // assign a C-style string to a FixedString // POST: value of this object is a copy of "s" and // a reference to this object is returned for further use { strncpy(str,s,MaxSize); str[MaxSize] = '\0'; // in case "s" is too big return *this; }
#include "Name.h"
void main() { Name name1; cout << "name1 initially:\n" << name1 << endl;
cin >> name1; cout << "name1 after input:\n" << name1 << endl;
Name name2("Chris", "Wild"); cout << "name2 initially:\n" << name2 << endl;
name2.SetFirst("John"); cout << "name2 after set First:\n" << name2 << endl;
name1 = name2;
cout << "name1 First after assignment:" << name1.GetFirst() << endl; cout << "name1 Last after assignment:" << name1.GetLast() << endl;
//**** show assignment FixedString string1("this is string1"); FixedString string2("string2 I am");
cout << "string1:" << string1 <<"|\n"; cout << "string2:" << string2 <<"|\n"; string1 = string2 = "assign this string"; cout << "string1:" << string1 <<"|\n"; cout << "string2:" << string2 <<"|\n"; }
Designing Classes
Member functions can be broken in different types (constructor, modifier, selector, converter, iterator).
After design, the class interface can be published (as header files) and the following can happen somewhat in parallel:
Of course changing the class interface would cause changes in all three activities.
Principles
Goals
reusability: can be reused
OO Analysis and Design Example: Doctor Office
Requirement:
Schedule appointments for patients to see one of several doctors whose schedule is divided into 15-minute appointment slots beginning at 8AM and finishing at 6PM. Also print out schedule for each doctor, listing time and patient name of each appointment.
Input from terminal and output each schedule to a file for later printing.
Only handle one day's appointments for each doctor.
Analysis:
(nouns in bold and verbs in italic)
Relationships:
- doctors have schedules
- schedules have appointments
- appointments have patients and time
- Office Master Schedule has doctors.
- Patients can schedule an appointment with a doctor
- Somebody can print a schedule
Operations:
Doctor:
- AddToSchedule
- ShowAppointments
DailySchedule:
- SetAppointment
- IsTimeSlotFree
- ShowAppointments
Appointments:
- Constructor
- IsScheduled
Patient:
- InputName
- ChooseDoctor
- ChooseTimeSlot
- SetAppointment
Scheduler:
- ScheduleOneAppointment
- ScheduleAllAppointments
- PrintAllAppointments
TimeSlot:
- I/O