Intro the Computer Sc

You have to read the slide and answer the questions do the lab

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

CSE 112 Introduction to Computer Science II
CSE 112 – Lab #8 Corporate Sales – Vectors, Classes, and Storing Objects
Reference Programs 17-7 thru 17-10 and the Product.h code on page 1084
Implement the Product class on page 1084 in a header file Products.h adding the attributes shown
below. Create instances of the class, populate the objects with the data provided below from a file
called ‘productInfo.txt’, and store the objects in a vector.
1.
2.
3.
4.
5.
Display all of the products and their information (see below) using a function.
Prompt the user for the name of the product they would like to change.
Prompt the user for the change – ‘p’ = price, ‘u’ = units, ‘r’ = reorder point
Update the product information
Display all of the product information calling the display function.
void display_products(vector p);
If there is a change and it does not persist when the products are displayed and you are using ‘auto’ in
the loop, change to accessing the vector like an array with brackets.
for(int i = 0; i < 6; i++) The program will use the class definition provided below in a separate *.h file, and use the member functions as appropriate. Output Format – tabs and spacing required: 1 Assignments submitted after the due date will incur a grade penalty of three (3) points per day after the first day, and will not be accepted more than one (1) week late. CSE 112 Introduction to Computer Science II // Product class definition for Product.h file class Product { private: string name; int units; double price; int reOrderPoint; public: // constructor Product(string n, double p, int u) { name = n; price = p; units = u; reOrderPoint = 3; } void setName(string n) { name = n; } void setPrice(double p) { price = p; } void setUnits(int u) { units = u; } void setReorderPoint(int r) { reOrderPoint = r; } string getName() const { return name; } double getPrice() const { return price; } int getUnits() const { return units; } int getReorderPoint() const { return reOrderPoint; } }; 2 Assignments submitted after the due date will incur a grade penalty of three (3) points per day after the first day, and will not be accepted more than one (1) week late. CSE 112 Introduction to Computer Science II Date for "productInfo.txt" T-Shirt 19.50 24 Mug 5.99 10 Clock 14.50 6 Frame 12.99 12 Poster 4.99 20 Lamp 16.50 3 3 Assignments submitted after the due date will incur a grade penalty of three (3) points per day after the first day, and will not be accepted more than one (1) week late. Chapter 17: The Standard Template Library Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. 17.1 Introduction to the Standard Template Library Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. The Standard Template Library The Standard Template Library (STL): an extensive library of generic templates for classes and functions. Categories of Templates: Containers: Class templates for objects that store and organize data Iterators: Class templates for objects that behave like pointers, and are used to access the individual data elements in a container Algorithms: Function templates that perform various operations on elements of containers Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. 17.2 STL Container and Iterator Fundamentals Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. Containers Sequence Containers Stores data sequentially in memory, in a fashion similar to an array Associative Containers Stores data in a nonsequential way that makes it faster to locate elements Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. Containers Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. Containers Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. Container Adapters Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. STL Header Files Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. The array Class Template An array object works very much like a regular array A fixed-size container that holds elements of the same data type. array objects have a size() member function that returns the number of elements contained in the object. Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. The array Class Template The array class is declared in the header file. When defining an array object, you specify the data type of its elements, and the number of elements. Examples: array numbers; array names; Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. The array Class Template Initializing an array object: array numbers = {1, 2, 3, 4, 5}; array names = {"Jamie", "Ashley", "Doug", "Claire"}; Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. The array Class Template The array class overloads the [] operator. You can use the [] operator to access elements using a subscript, just as you would with a regular array. The [] operator does not perform bounds checking. Be careful not to use a subscript that is out of bounds. Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. Iterators Objects that work like pointers Used to access data in STL containers Five categories of iterators: Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved. Similarities between Pointers and Iterators Pointers Iterators Use the * and -> operators to dereference
Yes
Yes
Use the = operator to assign to an element
Yes
Yes
Use the == and != operators to compare
Yes
Yes
Use the ++ operator to increment
Yes
Yes
Yes
Yes
(bidirectional
and randomaccess
iterators)
Use the — operator to decrement
Use the + operator to move forward a specific number of
elements
Use the – operator to move backward a specific number of
elements
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Yes
Yes
Yes
Yes Yes
(bidirectional
and randomaccess
iterators)
Iterators
To define an iterator, you must know what type of
container you will be using it with.
The general format of an iterator definition:
containerType::iterator iteratorName;
Where containerType is the STL container type, and
iteratorName is the name of the iterator variable that
you are defining.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Iterators
For example, suppose we have defined an array object,
as follows:
array names = {“Sarah”, “William”, “Alfredo”};
We can define an iterator that is compatible with the
array object as follows:
array::iterator it;
This defines an iterator named it. The iterator can be
used with an array object.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Iterators
All of the STL containers have a begin() member
function that returns an iterator pointing to the container’s
first element.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Iterators
All of the STL containers have a end() member function
that returns an iterator pointing to the position after the
container’s last element.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Iterators
You typically use the end() member function to know
when you have reached the end of a container.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Iterators
You can use the auto keyword to simplify the definition
of an iterator.
Example:
array names = {“Sarah”, “William”, “Alfredo”};
auto it = names.begin();
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Mutable Iterators
An iterator of the iterator type gives you read/write
access to the element to which the iterator points.
This is commonly known as a mutable iterator.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Constant Iterators
An iterator of the const_iterator type provides readonly access to the element to which the iterator points.
The STL containers provide a cbegin() member
function and a cend() member function.
The cbegin() member function returns a const_iterator
pointing to the first element in a container.
The cend() member function returns a const_iterator
pointing to the end of the container.
When working with const_iterators, simply use the container
class’s cbegin() and cend() member functions instead of the
begin() and end() member functions.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Reverse Iterators
A reverse iterator works in reverse, allowing you
to iterate backward over the elements in a
container.
With a reverse iterator, the last element in a
container is considered the first element, and the
first element is considered the last element.
The ++ operator moves a reverse iterator
backward, and the −− operator moves a reverse
iterator forward.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Reverse Iterators
The following STL containers support reverse
iterators:
array
deque
list
map
multimap
multiset
set
vector
All of these classes provide an rbegin() member
function and an rend() member function.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Reverse Iterators
The rbegin() member function returns a reverse
iterator pointing to the last element in a container.
The rend() member function returns an iterator pointing
to the position before the first element.
Reverse iterator
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Reverse Iterators
To create a reverse iterator, define it as
reverse_iterator
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
17.3
The vector Class
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The vector Class
A vector is a sequence container that works like an
array, but is dynamic in size (it automatically handles its
own storage requirements in case it grows).
Overloaded [] operator provides access to existing
elements
The vector class is declared in the header
file.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
vector Class Constructors
Default Constructor
vector name;
Creates an empty vector.
Fill Constructor
vector name(size);
Creates a vector of size elements. If the elements
are objects, they are initialized via their default
constructor. Otherwise, initialized with 0.
Fill Constructor
vector name(size, value);
Creates a vector of size elements, each initialized
with value.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
vector Class Constructors
Range Constructor
vector name(iterator1, iterator2);
Creates a vector that is initialized with a range of
values from another container. iterator1 marks the
beginning of the range and iterator2 marks the end.
Copy Constructor
vector name(vector2);
Creates a vector that is a copy of vector2.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Subscript notation
Range-based for loop
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Initializing a vector
In C++ 11 and later, you can initialize a
vector object:
vector numbers = {1, 2, 3, 4, 5};
or
vector numbers {1, 2, 3, 4, 5};
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Adding New Elements to a vector
The push_back member function adds a
new element to the end of a vector:
vector numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Accessing Elements with the at()
Member Function
You can use the at() member function to
retrieve a vector element by its index with
bounds checking:
vector names = {“Joe”, “Karen”, “Lisa”};
cout

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER