751 likes | 998 Views
CSI 1340 Introduction to Computer Science II. Chapter 3 ADTs Unsorted List and Sorted List. Self-test. Create a class called SalesPerson that includes 2 public member functions: void SetSales(int month, float amount); float GetSales(int month) const; And 1 private member variable:
E N D
CSI 1340Introduction to Computer Science II Chapter 3 ADTs Unsorted List and Sorted List
Self-test Create a class called SalesPerson that includes 2 public member functions: void SetSales(int month, float amount); float GetSales(int month) const; And 1 private member variable: float sales[12];
// FILE: salesperson.h // CLASS PROVIDED: SalesPerson (an ADT for a SalesPerson) // Modification member function // void SetSales(int month, float amount); // Precondition: 1 <= month <= 12; amount has been // assigned a floating point value. // Postcondition: The sales figure for the month indicated // by month has been assigned amount. // Constant member function // float GetSales(int month) const; // Precondition: 1 <= month <= 12 // Postcondition: The sales figure for the month indicated // by month has been returned. // Private member variable // float sales[12];
C++ Class - salesperson.h class SalesPerson { public: void SetSales(int month, float amount); float GetSales(int month) const; private: float sales[12]; };
Create a correct C++ implementation file (.cpp) for the SalesPerson class
C++ Implementation - salesperson.cpp #include “salesperson.h” void SalesPerson::SetSales(int month, float amount) { sales[month - 1] = amount; } float SalesPerson::GetSales(int month) const { return sales[month - 1]; }
Show how the SalesPerson class can be utilized in a program (Main.cpp) to:(1) set the sales figure for month 1 to $1500.00 (2) set the sales figure for month 2 to $1000.00 (3) display the following: Sales for month 1 = $1500.00
C++ Program- main.cpp #include <iostream> #include “salesperson.h” using namespace std; int main( ) { SalesPerson s; s.SetSales(1, 1500.00); s.SetSales(2, 1000.00); cout << “Sales for month 1 = $“ << s.GetSales(1) << endl; return 0; }
Rules for Constructors • You may declare as many constructors as you like--one for each different way of initializing an object. • Each constructor must have a distinct parameter list so that the compiler can tell them apart. • Only one default constructor is allowed.
Do we still need a public member function that assigns values to the private member variables if we include a constructor?
Yes!! • Public member functions can be called at any time by the user. • A constructor (either the default or an alternative) is utilized only once, when an object is declared. It cannot be called by the user.
Show the implementation for a default constructor for the SalesPerson class
Default Constructor for SalesPerson SalesPerson::SalesPerson( ) { for (i = 0; i < 12; i++) sales[i] = 0.0; }
Overloading • Function Overloading: Creating several variants of the same function, distinguishing only by parameter types • Operator Overloading: Gives a new definition for an existing operator (i.e., =, = =, +, +=, . . .) • Often used to define new meanings for operators of a class.
Determining Class Value • Determines how values are copied from one object to another. • Consists of two operators: • Assignment operator • Copy constructor
Assignment Operator • y = x copies the value of x to y. • For a class, assignment should be carried out by copying the value of each private member variable from class x to class y.
Copy Constructor • Initializes a new object as an exact copy of an existing object. • The copy constructor is a constructor with exactly one parameter, and the data type of the parameter is the same as the constructor class, e.g., Time(const Time& w); • Examples of its use Time y(x); Time y = x; // Not assignment because y is created Time y = Time(x);
Assignment vs. Copy Constructor • Assignment operator • Assigns the values of one object to another object. • Copy Constructor • Used whenever a new object is created and initialized to an existing object of the same type.
Assignment Operator & Copy Constructor in C++ • C++ provides an automatic assignment operator and an automatic copy constructor. • For some classes, the automatic versions fail and programmers must either write their own or indicate that the value semantics are not safe to use.
Testing the Copy Constructor #include <iostream> #include “time.h” using namespace std; int main( ) { Time t; t.setTime(13,37,6); Time s(t); // May generate a fatal error t.printStandard( ); s.printStandard( ); return 0; }
Copy Constructor Header File (time.h) class Time { public: Time( ); Time(int hr,int min,int sec); Time(const Time& w); . . . private: int hour; int minute; int second; }
Copy Constructor Implementation File (time.cpp) #include “time.h” Time::Time(const Time& w) { hour = w.hour; minute = w.minute; second = w.second; }
Assignment Operator (=) Use in Main.cpp #include <iostream> #include “time.h” using namespace std; int main( ) { Time t; t.setTime(13,37,6); Time s; s = t; // May generate a fatal error t.printStandard( ); s.printStandard( ); return 0; }
Assignment Operator (=) Header File (time.h) class Time { public: Time( ); . . . void operator=(const Time& w); private: int hour; int minute; int second; }
Assignment Operator (=) Implementation File (time.cpp) #include “time.h” void Time::operator=(const Time& w) { hour = w.hour; minute = w.minute; second = w.second; }
What is a List? • A list is a homogeneous collection of elements, with a linear relationship between elements. • That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.
Sorted and Unsorted Lists UNSORTED LIST Elements are placed into the list in no particular order. SORTED LIST List elements are in an order that is sorted in some way -- either numerically or alphabetically by the elements themselves, or by a component of the element (called a KEY member) .
change state observe state process all ADT Unsorted List Operations Transformers • MakeEmpty • InsertItem • DeleteItem Observers • IsFull • LengthIs • RetrieveItem Iterators • ResetList • GetNextItem
// SPECIFICATION FILE ( unsorted.h ) #include “ItemType.h” class UnsortedType// declares a class data type { public : // 8 public member functions void MakeEmpty () ; bool IsFull ( ) const ; int LengthIs ( ) const ; // returns length of list void RetrieveItem ( ItemType& item, bool& found ) ; void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item ) ; void ResetList ( ); void GetNextItem ( ItemType& item ) ; private : // 3 private data members int length ; ItemType info[MAX_ITEMS] ; int currentPos ; } ;
Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos Class Interface Diagram UnsortedType class MakeEmpty IsFull LengthIs RetrieveItem InsertItem DeleteItem ResetList GetNextItem
// IMPLEMENTATION FILE ARRAY-BASED LIST ( unsorted.cpp ) #include “itemtype.h” void UnsortedType::MakeEmpty ( ) // Pre: None. // Post: List is empty. { length = 0 ; } void UnsortedType::InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. Item is not in list. // Post: item is in the list. { info[length] = item ; length++ ; }
Before Inserting Hsing into anUnsorted List The item will be placed into the length location, and length will be incremented. length 3 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] . . . [MAX_ITEMS-1]
After Inserting Hsing into anUnsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]
void UnsortedType::LengthIs ( ) const // Pre: List has been inititalized. // Post: Function value == ( number of elements in list ). { return length ; } bool UnsortedType::IsFull ( ) const // Pre: List has been initialized. // Post: Function value == ( list is full ). { return ( length == MAX_ITEMS ) ; }
void UnsortedType::RetrieveItem ( ItemType& item, bool& found ) { bool moreToSearch ; int location = 0 ; found = false ; moreToSearch = ( location < length ) ; while ( moreToSearch && !found ) { switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; break; case EQUAL : found = true ; item = info[ location ] ; break ; } } }
Retrieving Ivan from anUnsorted List moreToSearch: true found: false location: 0 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]
Retrieving Ivan from anUnsorted List moreToSearch: true found: false location: 1 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]
Retrieving Ivan from anUnsorted List moreToSearch: true found: false location: 2 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]
Retrieving Ivan from anUnsorted List moreToSearch: true found: false location: 3 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]
Retrieving Ivan from anUnsorted List moreToSearch: false found: false location: 4 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]
void UnsortedType::DeleteItem ( ItemType item ) // Pre: item’s key has been inititalized. // An element in the list has a key that matches item’s. // Post: No element in the list has a key that matches item’s. { int location = 0 ; while (item.ComparedTo (info [location] ) != EQUAL ) location++; // move last element into position where item was located info [location] = info [length - 1 ] ; length-- ; }
Deleting Bradley from anUnsorted List location: 0 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] Key Bradley has not been matched.
Deleting Bradley from anUnsorted List location: 1 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] Key Bradley has been matched.
Deleting Bradley from anUnsorted List location: 1 length 4 info [ 0 ] Maxwell [ 1 ] Hsing [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] Placed copy of last list element into the position where the key Bradley was before.
Deleting Bradley from anUnsorted List location: 1 length 3 info [ 0 ] Maxwell [ 1 ] Hsing [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] Decremented length.