1 / 75

CSI 1340 Introduction to Computer Science II

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:

taline
Download Presentation

CSI 1340 Introduction to Computer Science II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSI 1340Introduction to Computer Science II Chapter 3 ADTs Unsorted List and Sorted List

  2. 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];

  3. // 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];

  4. C++ Class - salesperson.h class SalesPerson { public: void SetSales(int month, float amount); float GetSales(int month) const; private: float sales[12]; };

  5. Create a correct C++ implementation file (.cpp) for the SalesPerson class

  6. 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]; }

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

  8. 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; }

  9. Class Constructors

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

  11. Do we still need a public member function that assigns values to the private member variables if we include a constructor?

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

  13. Show the implementation for a default constructor for the SalesPerson class

  14. Default Constructor for SalesPerson SalesPerson::SalesPerson( ) { for (i = 0; i < 12; i++) sales[i] = 0.0; }

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

  16. Determining Class Value • Determines how values are copied from one object to another. • Consists of two operators: • Assignment operator • Copy constructor

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

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

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

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

  21. Testing the Copy Constructor

  22. 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; }

  23. Creating Your Own Copy Constructor

  24. 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; }

  25. Copy Constructor Implementation File (time.cpp) #include “time.h” Time::Time(const Time& w) { hour = w.hour; minute = w.minute; second = w.second; }

  26. Testing the Assignment Operator (=)

  27. 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; }

  28. Overloading theAssignment Operator (=)

  29. Assignment Operator (=) Header File (time.h) class Time { public: Time( ); . . . void operator=(const Time& w); private: int hour; int minute; int second; }

  30. Assignment Operator (=) Implementation File (time.cpp) #include “time.h” void Time::operator=(const Time& w) { hour = w.hour; minute = w.minute; second = w.second; }

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

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

  33. change state observe state process all ADT Unsorted List Operations Transformers • MakeEmpty • InsertItem • DeleteItem Observers • IsFull • LengthIs • RetrieveItem Iterators • ResetList • GetNextItem

  34. // 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 ; } ;

  35. Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos Class Interface Diagram UnsortedType class MakeEmpty IsFull LengthIs RetrieveItem InsertItem DeleteItem ResetList GetNextItem

  36. // 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++ ; }

  37. 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]

  38. After Inserting Hsing into anUnsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1]

  39. 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 ) ; }

  40. 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 ; } } }

  41. 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]

  42. 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]

  43. 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]

  44. 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]

  45. 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]

  46. 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-- ; }

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

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

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

  50. Deleting Bradley from anUnsorted List location: 1 length 3 info [ 0 ] Maxwell [ 1 ] Hsing [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] Decremented length.

More Related