Namespaces, Typedefs & Enums

Slides:



Advertisements
Similar presentations
Java for C++ Programmers Second Night. Overview First Night –Basics –Classes and Objects Second Night –Enumerations –Exceptions –Input/Output –Templates.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
True or false A variable of type char can hold the value 301. ( F )
Structure of a C program
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Basic Elements of C++ Chapter 2.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
1 C++ Syntax and Semantics, and the Program Development Process.
Beginning C++ Through Game Programming, Second Edition
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Programming
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Enum. enum – a new type 2 enum is a set of constant int values, that defines a type: enum Season { WINTER,// = 0 by default SPRING,// = WINTER + 1 SUMMER,//
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Control Statements
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
11 Chapter Structured Data
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Subtype Enumeration type
Programming Fundamentals
LESSON 4 Decision Control Structure
Basic Elements of C++.
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
Indexer AKEEL AHMED.
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Enumerated DATA Types Enum Types Enumerated Data Types
Basic Elements of C++ Chapter 2.
Data Types Chapter 8.
Enumerations & Annotations
Built-In (a.k.a. Native) Types in C++
Enumerations CS Fall 1999 Enumerations.
Chapter 11: Structured Data.
More On Enumeration Types
Variables, Identifiers, Assignments, Input/Output
SystemVerilog for Verification
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Writing Functions.
Enumerated DATA Types Enum Types Enumerated Data Types
Time Activity Music PE Library Computer Monday Tuesday Wednesday
Standard Version of Starting Out with C++, 4th Edition
7th Grade Math Warm-ups Week of December 5-9, 2016.
Enum.
Presentation transcript:

Namespaces, Typedefs & Enums Tricks for naming things

Name Collisions Including lots of libraries… Library A defines: bool checkPositive(int x); Library B defines: bool checkPositive(int y);

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()

Declaring in Namespace Can invent any arbitrary namespace like this 

Using Using can bring in… Whole namespace One identifier Let me use anything from Library1 without Library1:: Let me use foo() from Library1 without Library1::

Not Using Using Best practice : don't include whole std namespace in .h files std:: has lots of names Anyone including your .h now stuck with them

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 Bad uses Renaming int to number

Why Typedef Good uses Vs: 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;

Why Typedef Good uses Dealing with platform issues //On PC: typedef int int32; //On arduino processor typedef long int32; //anywhere: int32 myNum; //int32 definitely has 32 bits

Constant Issue Constants = readable code

Constant Issue Don't protect us from stupidity:

Enums Enumerated Type Custom data type with discrete set of possible values A weekday is something from: {Monday, Tuesday, Wednesday, Thursday, Friday}

Enums Syntax: enum newType {valueList}; Sample enums: enum standing {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; enum color {RED, BLUE, GREEN, BLACK, ORANGE};

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

Enums Pluses of constants, without dangers

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

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

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

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

Enum Values Enums stored as integral values Can modify by assigning {Monday, Tuesday, Wednesday, Thursday, Friday} 10 11 30 31 32

Enum Values Only use assignment/value if represent logical value:

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

Interacting: Cin can't read into enum type Read in string/int, use if:

Interacting: Cout will print as number Print using if/switch/array

Interacting: Legal to switch based on enum:

Enum’s & Math Math with enum results in an int

Enum’s & Math Can static cast into an enum Can go out of range!

Enum’s & Math Can static cast into an enum Can go out of range! Prevent:

Enum’s & Math Looping with enum:

Enum’s & Math Work normally as parameter or return type: