Wednesday, 10/23/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/23/02  QUESTIONS??  Today:  Discussion of HW #3  The const modifier for functions.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Monday, 11/18/02, Slide #1 CS 106 Intro to CS 1 Monday, 11/18/02  QUESTIONS??  Today:  Hand back, discuss HW #4  Discussion of Lab 10  Exam #2 Friday.
CS Nov 2006 C-strings.
Chapter 2 Data Types, Declarations, and Displays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Data Types, Expressions and Functions (part I)
1 Chapter 10 Characters, Strings, and the string class.
1 Chapter 9 Scope, Lifetime, and More on Functions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
CSC 107 – Programming For Science. The Week’s Goal.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University CONTROL STRUCTURES Simple If: if (boolean exp) { statements.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Characters, Strings, And The string Class Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Operations Chapter 4 & Section Expressions As we've seen, in a C++ program, any finite sequence of objects and operations that combine to produce.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Chapter Characters, Strings, and the string class 10.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
1 CSC241: Object Oriented Programming Lecture No 08.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
A Sample Program #include using namespace std; int main(void) { cout
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Data Types and Expressions
C Characters and Strings
Characters, C-Strings, and More About the string Class
Chapter 8 - Characters and Strings
Standard Version of Starting Out with C++, 4th Edition
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Variables Kingdom of Saudi Arabia
Operator Overloading; String and Array Objects
10.1 Character Testing.
COP 3330 Object-oriented Programming in C++
Engineering Problem Solving with C++ An Object Based Approach
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
Fundamental Programming
HNDIT11034 More Operators.
Presentation transcript:

Wednesday, 10/23/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/23/02  QUESTIONS??  Today:  Discussion of HW #3  The const modifier for functions and parameters  The char object type  Some new numeric operators  Reading: pp of Chapter 6  Exercises: p. 150 #9, 10, 19

Wednesday, 10/23/02, Slide #2 Using the const modifier on member functions  If a member function does not change the invoking object, we can prevent accidental change by adding the const modifier (Recall: Standing for “Constant”):  Complex Complex::AddC (Complex x)  Function code permitted to change invoking object  Complex Complex::AddC (Complex x) const  Function code not permitted to change invoking object  Which Complex member functions should be const? Constructor? InputC()? OutputC()? AddC()? SubC()? MultC()?

Wednesday, 10/23/02, Slide #3 Using the const modifier on parameters  We use reference parameters if we do want to change the argument -- the actual argument is used  We use value parameters if we don’t want to change the argument -- a copy of the argument is used  If parameters are class objects, sometimes copies can be big -- we want to use the actual object, but not change it  Const reference parameters let us use the actual object, but don’t let us change it: Complex Complex::AddC (Complex x) const //uses a copy of x Complex Complex::AddC (Complex& x) const //uses x, can change x Complex Complex::AddC (const Complex& x) const //uses x, can’t // change x

Wednesday, 10/23/02, Slide #4 Type char objects  char is a built-in (“primitive”) object type, like int, float, double  char declaration: char ch; or char c1(‘%’); or char c2 = ‘x’;  char constants: single characters enclosed in single quotes  Operators that can be used with type char:  Extract: >>, Insert: >, Insert: <<  Extraction operator ignores whitespace -- uses it to separate one (non-whitespace) char from the next.  Assign: =, Comparisons: ==, !=, >, =,, =, <=  Comparisons are based on the order of characters in terms of their ASCII codes

Wednesday, 10/23/02, Slide #5 Changing types (in general)  There are two main ways to change objects from one type to another:  Assign the object to a variable of the new type:  int num = 3.14 (what value does num have?)  char letter = “Hi Mom” (what’s letter now?)  Use “Typecasting” – type acts as a function:  int(3.14) ? int(‘b’) ? char(126) ? double(10) ?

Wednesday, 10/23/02, Slide #6 char library functions  Since char is a primitive type (not a class type), we don’t use the ‘dot’ notation (unless the function has a char parameter, but is a member of some other class)  Some functions that return true/false (bool) values (I’ve given prototypes to show return and parameter types):  bool islower (char c);  bool isupper (char c);  bool isalpha (char c);  bool isdigit (char c);  bool isalnum (char c);  bool isspace (char c);  bool ispunct (char c);  bool isprint (char c);

Wednesday, 10/23/02, Slide #7 The library: for changing to uppercase, lowercase  contains the following two functions:  int tolower(char c);  int toupper(char c);  Returns the ASCII code of c changed to lower/upper case  To get type char, either assign to char object or use char() typecasting Program segment: char c1 = 'a', c2 = 'B'; cout << c1 << c2 << endl; cout << toupper(c1) << tolower(c2) << endl; char c3 = toupper(c1); cout << c3 << char(tolower(c2)) << endl; Output: aB 6598 Ab

Wednesday, 10/23/02, Slide #8 More int operators: Mod operator %  Produces the remainder of the integer division  Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4  % has same precedence as * and /

Wednesday, 10/23/02, Slide #9 Increment and Decrement Operators ++ and --  Increment and Decrement (prefix and postfix):  ++x, x++ : increase value of x by 1  --x, x-- : decrease value of x by 1  Examples:  ++count;  turnsLeft--;

Wednesday, 10/23/02, Slide #10 Other operators: Compound Assignment  x += y; Replaces x with x + y  x -= y; Replaces x with x - y  Examples:  Balance += Deposit;  TimeLeft -= TimeOfTurn;