31
Namespaces, Typedefs & Enums Tricks for naming things

Typedefs & Enums

  • Upload
    kylee

  • View
    53

  • Download
    0

Embed Size (px)

DESCRIPTION

Typedefs & Enums. Tricks for naming things. Typedef. typedef creates a new name for existing type Does not create new types! typedef exitingType newName ; Ex: typedef int number ; number x = 10; //number really means int. Why Typedef. Good uses Ugly constructs: - PowerPoint PPT Presentation

Citation preview

Page 1: Typedefs & Enums

Namespaces, Typedefs & Enums

Tricks for naming things

Page 2: Typedefs & Enums

Name Collisions

• Including lots of libraries…– Library A defines:bool checkPositive(int x);

– Library B defines:bool checkPositive(int y);

Page 3: Typedefs & Enums

Name Spaces

• Namespaces break names into groups

• To use something from namespace, specify namespace to resolve it in:LibA::checkPositive(10);

LibA::checkPositive(int x)LibA::somethingElse()LibA::x

LibB::checkPositive(int x)LibB::otherThing()

Page 4: Typedefs & Enums

Declaring in Namespace

• Can invent any arbitrarynamespace like this

Page 5: Typedefs & Enums

Using

• Using can bring in…– Whole namespace

– One identifier

Let me use anything fromLibrary1 without Library1::

Let me use foo() fromLibrary1 without Library1::

Page 6: Typedefs & Enums

Not Using Using

• Putting using namespace std in .h file frowned upon • std:: has lots of names• Anyone including your .h now stuck with them

Page 7: Typedefs & Enums

Typedef

• typedef creates a new name for existing type– Does not create new types!

typedef exitingType newName;

• Ex:typedef int number;

number x = 10; //number really means int

Page 8: Typedefs & Enums

Why Typedef

• Bad uses– Renaming int to number

Page 9: Typedefs & Enums

Why Typedef

• Good uses– Ugly constructs:

std::vector<std:pair<int, int> >::iterator myIt;

std::vector<std:pair<int, int> >::iterator myOtherIt;

Vs:typedef std::vector<std:pair<int, int> >::iterator VectorPairIterator;

VectorPairIterator myIt;

VectorPairIterator myOtherIt;

Page 10: Typedefs & Enums

Why Typedef

• Good uses– Dealing with platform issues

//On PC:typedef int int32;//On arduino processortypedef long int32;

//anywhere:

int32 myNum; //int32 definitely has 32 bits

Page 11: Typedefs & Enums

Constant Issue

• Constants = readable code

Page 12: Typedefs & Enums

Constant Issue

• Don't protect us from stupidity:

Page 13: Typedefs & Enums

Enums

• Enumerated Type – Custom data type with discrete set of possible

values

A weekday is something from:{Monday, Tuesday, Wednesday, Thursday, Friday}

Page 14: Typedefs & Enums

Enums

• Syntax: enum newType {valueList};

• Sample enums:enum standing {FRESHMAN, SOPHOMORE,

JUNIOR, SENIOR};

enum color {RED, BLUE, GREEN, BLACK, ORANGE};

Page 15: Typedefs & Enums

Enum Use

• Enum usage– Can make variables of that type– Variables can only have given values

enum standing {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};

standing studentYear = FRESHMAN;

standing student2Year = 12; //Error

Page 16: Typedefs & Enums

Enums

• Pluses of constants, without dangers

Page 17: Typedefs & Enums

Enum Rules

• Enum values must be valid identifiers– Start with letter, no special symbols, etc…

Page 18: Typedefs & Enums

Enum Rules

• Enum values must be valid identifiers• Can't reuse identifiers

Page 19: Typedefs & Enums

Enum Rules

• Enum values must be valid identifiers• Can't reuse values• Enum type names should be singular– Don't want:grades JohnsGrade;

Page 20: Typedefs & Enums

Enum Values

• Enums stored as integral values– Starting from 0:{Monday, Tuesday, Wednesday, Thursday, Friday} 0 1 2 3 4

Page 21: Typedefs & Enums

Enum Values

• Enums stored as integral values– Can modify by assigning

{Monday, Tuesday, Wednesday, Thursday, Friday} 10 11 30 31 32

Page 22: Typedefs & Enums

Enum Values

• Only use assignment/value if represent logical value:

Page 23: Typedefs & Enums

Relational Operators

• Relational operations work based on assigned values (order of enumerated values)

Page 24: Typedefs & Enums

Interacting:

• Cin can't read into enum type

– Read in string/int, use if:

Page 25: Typedefs & Enums

Interacting:

• Cout will print as number

– Print using if/switch/array

Page 26: Typedefs & Enums

Interacting:

• Legal to switch based on enum:

Page 27: Typedefs & Enums

Enum’s & Math

• Math with enum results in an int

Page 28: Typedefs & Enums

Enum’s & Math

• Can static cast into an enum– Can go out of range!

Page 29: Typedefs & Enums

Enum’s & Math

• Can static cast into an enum– Can go out of range!

– Prevent:

Page 30: Typedefs & Enums

Enum’s & Math

• Looping with enum:

Page 31: Typedefs & Enums

Enum’s & Math

• Work normally as parameteror return type: