Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE 2341 Object Oriented Programming with C++ Note Set #6.

Similar presentations


Presentation on theme: "1 CSE 2341 Object Oriented Programming with C++ Note Set #6."— Presentation transcript:

1 1 CSE 2341 Object Oriented Programming with C++ Note Set #6

2 2 Quick Look Use default and overloaded constructors Declare and work with an array of objects Validate user input from within a class

3 3 Overloaded Constructors #ifndef INVENTORYITEM_H #define INVENTORYITEM_H #include using namespace std; class InventoryItem { private: char *description; int units; public: InventoryItem() { description = new char[51]; } InventoryItem(char *desc) { description = new char[strlen(desc)+1]; strcpy(description, desc); } InventoryItem.h

4 4 Overloaded Constructors InventoryItem(char *desc, int u) { description = new char [strlen(desc) + 1]; strcpy(description,desc); units = u; } ~InventoryItem() { delete[] description; } void setDescription(char *d) { strcpy(description, d); } void setUnits (int u) { units = u; } char *getDescription() { return description; } int getUnits() { return units; } }; #endif InventoryItem.h

5 5 Declaring Objects Because of the overloaded constructors, can instantiate in different ways InventoryItem ii; InventoryItem ii2(“Wrench”); InventoryItem ii3(“Hammer”, 10);

6 6 Constructors and Destructors A class can only have one default constructor A class can have any number of overloaded constructors A class can have only one destructor

7 7 Objects and Arrays Possible to declare arrays of objects –Default constructor would be called for each element in the array. InventoryItem store[10];

8 8 Objects and Arrays May use initialization lists to call overloaded constructors Square tres[3] = {5, 6, 10}; InventoryItem store[3] = { “Wrench”, “Hammer”, “Pliers” };

9 9 Objects and Arrays Need more complex initialization list if number of args to constructor is greater than 1 InventoryItem inventory[3] = { “Hammer”, InventoryItem(“Wrench”,17), } First Element Second Element Third Element- Which constructor????

10 10 Objects and Arrays InventoryItem inventory[3] = { InventoryItem(“Hammer”, 12), InventoryItem(“wrench”, 17), InventoryItem(“pliers”, 10) }; for (int i =0; i < 3; i++) { cout << inventory[i].getDescription(); cout << inventory[i].getUnits(); }

11 11 Design Time Design an array class. The class should –create an array of integers of any size at runtime –perform bounds checking –allows setting of a particular element of an array –retrieves a particular element from the array

12 12 Intlist #ifndef INTLIST_H #define INTLIST_H class IntList { private: int *list; int numElements; bool isValid(int); public: IntList(int); ~IntList(); bool set(int, int); bool get(int, int &); }; #endif Intlist.h

13 13 Intlist #include “IntList.h” IntList::IntList (int size) { list = new int[size]; numElements = size; for (int ndx =0; ndx < size; ndx++) list[ndx] = 0; } IntList::~IntList() { delete [] list; } Intlist.cpp

14 14 Intlist bool IntList::isValid(int element) { if (element < 0 || element >= numElements) return false; else return true; } bool IntList::set(int element, int value) { if (isValid(element)) { list[element] = value; return true; } else return false; } Intlist.cpp

15 15 Intlist bool IntList::get(int element, int &value) { if (isValid(element)) { value = list[element]; return true; } else return false; } Intlist.cpp

16 16 Fini ?


Download ppt "1 CSE 2341 Object Oriented Programming with C++ Note Set #6."

Similar presentations


Ads by Google