Note: write explicit at the beginning of constructor if it only takes in one value
This little blob contains some after class notes I wanted to take regarding Iterators.
Iterator is actually a standard library class, and it helps us move through containers
It is similar to how we can move through an array using pointers and the [] notation.
Imagine that you have the code below:
Note: No need to fully understand code below, from internet, just want to demonstrate
Versus:
Here, we need two separate functions to compute the sum of the data in a linked-list and a dynamic array.
The way each data structure is structured is different, hence why we have two different functions
However, the logic behind how we compute this sum is nearly identical
We iterate through the structure at each “node”, and add each data into a sum
Iterators provide us a level of abstraction, to be able to design a way of computing sum in once function, that is capable of taking in either data type
Here’s another way to look at it
Integers have ++ to increment 0 to 1
Pointers have ++ to increment from one bit in memory to a next one
These capabilities allow us to iterate through integers in a for loop, or through an array using incrementing pointers
Wrapping our “list” class with an iterator class gives us the power to define a new ++ for the class we are working with
END OF OWN NOTE, RESUMING LECTURE NOTES
c++ has automatic type deduction. By using the keyword auto, we can define a variable without knowing its type.
So, the compiler checks the type of y, and provides that same type to x. If y was an object, then of course, compiler would call on the copy constructor to build x.
So, we could have used the following for loop in the above code:
Another way to make this for loop shorter and cleaner, is using a range-based for loop. When do we use range-based for loops?
Methods begin and end (imagine an array, is best example, or linked list, etc.)
Iterator supports:
operator ++
operator !=
operator *
There are different ways we can provide ways to access a classes’ data:
Now, consider another example:
When we have this function as a member of the above Vec class
So, everything up until this point will be on the midterm. Assignment 3 is really good practice for midterm, do it ASAP.
Again, consider the followig:
Standalone function: Defined outside of your class
Now, let us jump back to the Vec class we defined earlier:
Again, this needs review, a tad confusing
Operators should be standalone functions, e.g:
operator <<
operator >>
Now, there are certain operators that can, and should, be members of a class:
operator =
operator →
operator []
operator ( )
operator T( ), with T being a type, type casting
The above are just some examples, know of them, don’t need to know in depth how they work
Declaring arrays of objects
Say we wanted to initialize an array of Vec objects:
So, how can we solve this? There are a couple of options:
Define a default constructor (so a constructor that has nothing passed to it)
Initialize every element during the declaration (for stack allocated arrays)
Create arrays of pointers (for heap arrays), where each pointer points to ab object which we can define later in the code
Example of sol.2 :
Example of sol.3 :
Note that we need to delete like this if we are using the default destructor, experiment with this. With a user-defined destructor, could be done inside the destructor.
Now, let us revisit the Student class:
Say we want to see how many times we call grade(). We can do the following: