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).

(click for source code)

rainbow.gif (2251 bytes)
Header file is the same.

rainbow.gif (2251 bytes)

//###########################################################
// 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;
}

 

rainbow.gif (2251 bytes)

 

// 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 hptrd_left.gif (955 bytes)
#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); hptrd_left.gif (955 bytes)
	// 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 hptrd_left.gif (955 bytes)
	
private:
	char str[MaxSize+1];  // String characters
};
#endif hptrd_left.gif (955 bytes)

rainbow.gif (2251 bytes)

 

// 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 hptrd_left.gif (955 bytes),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; hptrd_left.gif (955 bytes)
}

rainbow.gif (2251 bytes)

#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"; hptrd_left.gif (955 bytes)
	cout << "string1:" << string1 <<"|\n";
	cout << "string2:" << string2 <<"|\n";
	
}

 


 

Designing Classes

 

object-oriented analysis:
process of examining the application requirements to discover the classes and object, their state, behaviors and relationships in the vocabulary of the problem domain.
 
Find the nouns (possible classes) and verbs (possible member functions). Verbs can also denote relationships between classes (a student has a class, an employee is a person) or functions on objects.
 
object-oriented design:
is a design method that breaks the problem into class and object abstractions to logically structure the solution

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:

  1. class implementation
  2. class usage (programmers who use the class can start writing code to the specification)
  3. class testing (a testing strategy and test programs can be written)

Of course changing the class interface would cause changes in all three activities.

Principles

abstraction:
define essential properties and behaviors while hiding the inessential details.
e.g. float, while, sqrt
The class interface (what you put in the header file) should represent an abstraction of the class
 
encapsulation:
ability to hide information about the details of an abstraction and to protect those details from unwarranted use. This means that the details could be changed without changing the abstraction. The class and function are the primary encapsulation mechanisms in C++.
modularity:
division of a program into units (modules) which have a well defined interface. modules are building blocks for a program.
 
hierarchy:
a relationship between modules where one module is the parent or controller of another module. Inheritance (IS-A) relationships between classes and calling/called relationships between functions are examples of hierarchies.
 
strongly typed language:
Language which requires that only compatible types are used in expressions. Helps catch incorrect usage of variables.

Goals

component:
group of objects that work together

 


 

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:

doctor1.gif (5676 bytes)

Operations:

Doctor:

DailySchedule:

Appointments:

Patient:

Scheduler:

TimeSlot:

 


 


Copyright chris wild 1998.
For problems or questions regarding this website contact [Chris Wild (e-mail:wild@cs.odu.edu].
Last updated: September 23, 1998.