Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Introduction to C Programming
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Overview creating your own functions calling your own functions.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Systems Programming Concepts
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Lecture 19: Simple Data Types. 2 Lecture Contents: t Representation and conversion of numeric types t Representation and conversion of type char t Enumerated.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Simple Data Types Problem Solving, Abstraction, and Design using.
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
CSC 270 – Survey of Programming Languages C++ Lecture 1 : C++ As A Better C.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Simple Data Types Built-In and User Defined Chapter 10.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CS 1430: Programming in C++.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Enumerated Types Mr. Dave Clausen La Cañada High School.
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.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Prof. Amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays.
CSCE 210 Data Structures and Algorithms
LESSON 06.
CSCE 210 Data Structures and Algorithms
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
Enumeration Types and typedef
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
بسم الله الرحمن الرحيم.
C Arrays Systems Programming.
Data Types Chapter 8.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
Namespaces, Typedefs & Enums
MON TUE WED THU
Chapter 1: Introduction to Data Structures(8M)
2008 Calendar.
Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Structures Chapter 4.
2008 Calendar.
Presentation transcript:

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types

Prof. amr Goneid, AUC2 More on Simple Data Types

Prof. amr Goneid, AUC3 More on Simple Data Types #define Directive Promotion of Types Type Casting typedef Declaration Enumerated Data Types

Prof. amr Goneid, AUC4 1. #define Directive Used to give meaningful names to constants Used in older C programs prior to introduction of constants #define Example #define pi // The same as const float pi = ;

Prof. amr Goneid, AUC5 #define Directive (Example) // File: try.cpp // Uses #define Directive #include using namespace std; #define BEGIN { #define END } #define MOD % #define EQ ==

Prof. amr Goneid, AUC6 #define Directive (Example) int main() BEGIN // Local data... intn, d ; cout > n ; for ( ; ; ) BEGIN d = n MOD 2; cout << d; n /= 2; if (n EQ 0) break; END cout << endl; return 0; END

Prof. amr Goneid, AUC7 2. Promotion of Types Type promotion promoting a lower type to a higher type, e.g. 3 + x /2 if x is float, constants would be promoted to float as well and actually be 2.0 and 3.0 Type conversions int to float (number.0) float to int (truncation occurs)

Prof. amr Goneid, AUC8 Mixing Types Example: float x, z ; int y; x = 3.89 ; y = x ; z = y ; // y would contain 3 and z would contain 3.0

Prof. amr Goneid, AUC9 Avoid mixing types but if you need to you can cast a type Type casting allows you to change a type within the program for a specific function Form: type (variable)or (type) variable Example: int n ; float sum, average ; average = sum / float (n); // or average = sum / (float) n ; 3. Type Casting

Prof. amr Goneid, AUC10 Type Casting Example Example: for (j = 0; j <= 25; j++) { for (k = 0; k <= j; k++) cout << char (int (‘A’) + k); cout << endl; }

Prof. amr Goneid, AUC11 Used to give other names to existing data types. The new name can then be used as a qualified data type. Original type remains active. Syntax: typedef ; 4. typedef Declaration

Prof. amr Goneid, AUC12 typedef Declaration Examples: typedef int itemtype ; typedef float costType ; typedef char gradeType ; ……… itemtype item ; …… item = 7 ; costtype cost ; …… cost = ; gradetype grade ; …grade = ‘B’ ;

Prof. amr Goneid, AUC13 5. Enumerated Data Types

Prof. amr Goneid, AUC14 Of course C++ will give errors for the following declarations: icecream scoop ; fruit summerfruit ; color thiscolor ; …………… scoop = vanilla; summerfruit = mango; thiscolor = red ; fruit2 = fruit (int(mango) + 1); How can we make C++ accept these types? Enumerated Data Types

Prof. amr Goneid, AUC15 Enumerated Types Enumerated Data Types: Create new ordinal data types by enumerating their elements in ascending rank ( 0, 1,2 …). Example: enum weekday {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; ……. weekday day ; day = Mon ;

Prof. amr Goneid, AUC16 Using Enumerated Types All operations on ordinal types can be performed on enumerated types. enum classid {freshman, sophomore, junior, senior}; classid newClass; if (newClass == freshman) do something else if (newClass == sophomore) …..

Prof. amr Goneid, AUC17 Examples: enum color {red,green,blue,yellow}; color color1,color2, c ; color1 = red; color2 = color (int(green) +1); for(c = red; c <= yellow; c++) cout << int(c) << “ “ ; cout << endl; color1 = color (3); if(color1 > blue) cout << “yellow”; yellow Using Enumerated Types

Prof. amr Goneid, AUC18 Example: color.cpp // DISPLAYS THE VALUE OF thisColor void writeColor (color thisColor) { switch (thisColor) { case red: cout << "red"; break; case green: cout << "green"; break;

Prof. amr Goneid, AUC19 color.cpp case blue: cout << "blue"; break; case yellow: cout << "yellow"; break; default: status = 0; cerr << "*** ERROR: Invalid color value." << endl; }

Prof. amr Goneid, AUC20 Explicit Enumeration C++ allows to give explicit values to enumerators. For Example: enum NumberBase { Binary = 2, Octal = 8, Decimal = 10, Hexa = 16}; We can also start from a number other than zero. For example: enum German { Ein = 1, Zwei, Drei, Vier};