Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.

Slides:



Advertisements
Similar presentations
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.
Advertisements

C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
Correction of the Handout #include //Preprocessor using namespace std; int main (){ ………….. return 0; } A namespace is a named group of definitions. When.
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions.
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
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.
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
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7 Pointers and C-Strings
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Learning Objectives String Class.
Wednesday 09/23/13.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
COMS 261 Computer Science I
Chapter 2 part #3 C++ Input / Output
COMS 261 Computer Science I
C++ Programming Basics
COMS 261 Computer Science I
Modifying Objects Assignment operation Assignment conversions
Programming Strings.
Chapter 1 c++ structure C++ Input / Output
The Fundamentals of C++
Presentation transcript:

Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

Memory Depiction float y = 12.5;

Memory Depiction float y = 12.5; int Temperature = 32;

Memory Depiction float y = 12.5; int Temperature = 32; char Letter = 'c';

Memory Depiction float y = 12.5; int Temperature = 32; char Letter = 'c'; int Number; 'c' y Temperature Letter Number Unused space: integers/longs/floats/doubles should be word aligned on most CPUs, i.e. start at multiples of word size

Assignment Statement Basic form object = expression ; Celsius = (Fahrenheit - 32) * 5 / 9; y = m * x + b; Action Expression is evaluated Expression value stored in object Target becomes source

Swapping Values int Value1 = 10; int Value2 = 20; Suppose we want to swap Value1 and Value2?

Swapping Values int Value1 = 10; int Value2 = 20; int Hold = Value1;

Swapping Values int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2;

Swapping Values int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2;

Swapping Values int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2; Value2 = Hold;

Swapping Values int Value1 = 10; int Value2 = 20; int Hold = Value1; Value1 = Value2; Value2 = Hold; We swapped the values of objects Value1 and Value2 using Hold as temporary holder for Value1’s starting value!

Const Definitions Modifier const indicates that an object cannot be changed Object is read-only Useful when defining objects representing physical and mathematical constants const float Pi = ; Value has a name that can be used throughout the program const int SampleSize = 100; Makes changing the constant easy Only need to change the definition and recompile

Assignment Conversions Floating-point expression assigned to an integer object is truncated Integer expression assigned to a floating-point object is converted to a floating-point value Consider float y = 2.7; int i = 15; int j = 10; i = (int) y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples int i = 3; i += 4; // i is now 7 cout << i << endl; float a = 3.2; a *= 2.0; // a is now 6.4 cout << a << endl;

Increment and Decrement C++ has special operators for incrementing or decrementing an object by one Examples int k = 4; ++k; // k is 5 k++; // k is 6 cout << k << endl; int i = k++; // i is 6, k is 7 cout << i << " " << k << endl; int j = ++k; // j is 8, k is 8 cout << j << " " << k << endl; syntax semantics -- meaning

Nonfundamental Types Nonfundamental as they are additions to the language C++ permits definition of new types and classes A class is a special kind of type E.g. Standard Template Library (STL) provides class string Class objects typically have Data members that represent attributes and values Member functions for object inspection and manipulation Members are accessed using the selection operator (.) j = s.size(); Auxiliary functions for other behaviors Libraries often provide special-purpose types and classes Programmers can also define their own types and classes

Class string Used to represent a sequence of characters as a single object Some definitions string Name = "Joanne"; string DecimalPoint = "."; string empty = ""; string copy = name; string Question = '?'; // illegal

Class String To access a library use a preprocessor directive to add its definitions to your program file #include The using namespace std statement makes syntax less clumsy Without it std::string s = "Sharp"; std::string t = "Spiffy "; With it using namespace std; // std contains string string s = "Sharp"; string t = "Spiffy"; cin >> s; cout << t; “foo”.size() – illegal s.size() -- legal

Class string Some string member functions size() determines number of characters in the string string Saying = "Rambling with Gambling"; cout << Saying.size() << endl; // 22 substr() determines a substring (Note first position has index 0) string Word = Saying.substr(9, 4); // (pos, width) // returns “with” find() computes the position of a substring int j = Saying.find("it"); // 10 int k = Saying.find("its"); // ?  If string not found, value >= string.size() is returned string.find(substring, startpos) returns first position in string, after startpos, where the substring appears

Class string Auxiliary functions and operators getline() extracts the next input line string Response; cout << "Enter text: "; getline(cin, Response, '\n'); cout << "Response is \"" << Response << "\"” << endl; Example run Enter text: Want what you do Response is "Want what you do" character that terminates the line

Class string Auxiliary operators + string concatenation string Part1 = "Me"; string Part2 = " and "; string Part3 = "You"; string All = Part1 + Part2 + Part3; “foo “ + “bar” -- illegal += compound concatenation assignment string ThePlace = “IIT"; ThePlace += ", Bombay"; == ThePlace + “, Bombay” “Bombay” + ThePlace -- also legal

#include using namespace std; int main() { cout << "Enter the date in American format: " << "(e.g., January 1, 2007) : "; string Date; getline(cin, Date, '\n'); int i = Date.find(" "); string Month = Date.substr(0, i); int k = Date.find(","); string Day = Date.substr(i + 1, k - i - 1); string Year = Date.substr(k + 2, Date.size() - 1);//-k-2 string NewDate = Day + " " + Month + " " + Year; cout << "Original date: " << Date << endl; cout << "Converted date: " << NewDate << endl; return 0; } parse, parsing: breaking down a string into its parts

End of Chapter

Definition int NewStudents = 6;

Definition int NewStudents = 6; int OldStudents = 21;

Definition int NewStudents = 6; int OldStudents = 21; int TotalStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

Incrementing int i = 1;

Incrementing int i = 1; i = i + 1; Assign the value of expression i + 1 to i Evaluates to 2