CO1401 Week 13 Lecture

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

CO1401

Programming

Week 13
Some useful syntax
Enumerated data types

● Constants are good.


● A common situation you find in programming is a set of
associated constants.
● For example, consider a menu.
Enumerated data types

● Magic numbers are bad.


● So replace with constants
Enumerated data types

● Constants are good.


● A common situation you find in programming is a set of
associated constants.
● For example, consider a menu.
Enumerated data types

● This iso common that the language supports associated


constants: called enumerated data types.
● Replace with enumerated type.
Enumerated data types

● By default enums start at 0 and automatically go up by 1.


● So “Read” would have a value of 0, “Display” a value of 1.
● However, you can set enums explicitly to start at any number

● So “Read” now has a value of 1, “Display” a value of 2.


● We can also modify numbers in the sequence

● In this last case “Read” has a value of 0, “Display” a value


of 3, “Write” a value of 4 (since it follows on from
“Display”.
• Unfortunately this still leaves a problem to do with
how we name things. We use obvious names for
things. Take the word "Display" that I've used. Let's
say the menu option called a display function. It
would be sensible to call the function Display. If
there's any other operations that involve displaying
material we would probably want to call them Display
as well. It would be easy to accidentally declare a
variable called Display.
• This leads to confusion.
• The problem is made more acute when we share code
with other people.
• “Class” enum. Strongly typed, so scope is
clear
• Typed enum, specify the integer type you want to use

enum Flag : char


{
Debug, NoWarnings, Strict
};
// Add the exact type you want to use after
// the colon :
C11

• Updates to the language.


• Lots to explore.
• This is a few, hopefully useful things.
• Strongly typed enums are one introduction in C11,
here as some other examples.
auto

• auto, automatic type deduction:


auto

• Be careful.
• auto just saves typing time.
• It has no effect on the speed of your program.
• Use it when the type of a variable “is hard to know
exactly or hard to write” (Stroustrup)
• Do not use it by default. If an object has a type such
as an integer then use the data type!
• Because auto is completed by the compiler at compile
time you do not always know what data type has been
created, and this can cause problems.
Ranged-based for loop

• C11 introduced ranged-based loop.


• Don’t need to declare an iterator

• “i” is not the index, but the contents of the string


• So it’ll output the characters ‘h’, ‘e’, ‘l’, ‘l’, ‘o’ and so
on until the end of the string.
Ranged-based for loop
• Works with arrays, initializer lists and any types that
have a begin() and end().
Ranged-based for loop
• A good place to use auto!
Ranged-based for loop

• Very easy to use.


New C11 style array

● New safer array


New C11 style array

● Need to include the library <array>

● The new style array is what is known as a wrapper. A wrapper "wraps around" another piece of code.
● The wrapper manages the code. It hides it away inside a shell. It looks after the code in a nice clean fashion.
● The new style array wraps around old style array. An array exists inside, but you get to use the wrapper. What it gives you is an array with a lot of useful additions.
C11 array

● Syntax for declaration has changed. However, it looks


similar to that of vectors (and the other container classes).
● You must specify the size of the array when initialising.

● However, you can assign a list to the array


C11 array

● You will get a compiler error if the size is too large

● Smaller is fine. Empty elements are automatically filled with zeroes.


C11 array

● Can use the subscript operator


C11 at()

● Can use the at() syntax. This is useful because out of


bounds checking gets done.
Size

● Access to some useful methods.


● For example, size() provides the size of the array
Ranged-based for loop + auto

● Makes coding a loop and an array very straightforward.


Ranged-based for loop + auto

● Can assign to array when using ranged-based for loop by


using a reference, i.e. the ampersand.
● The following code assigns 10 to each element of the
array
Multidimensional arrays
● A bit cumbersome, but can be done. Revision expected
with C14.
Multidimensional arrays
● This works fine!
Iterators

● Use the STL syntax and iterators.


Functions

● Pass over to functions.


● Remember that you need to specify the size.
● Use reference parameter in order to make more efficient.
Methods

● Lots of other useful methods.


● fill() inserts numbers into all elements of the array. This would be useful, for
example, with larger arrays.

● Also have STL container methods such as front() and


back().

You might also like