J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions.

Slides:



Advertisements
Similar presentations
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Functions Computational assistants.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
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.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 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.
Input & Output: Console
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
EzWindows API A Graphical Application Programmer Interface JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
1 C++ Syntax and Semantics, and the Program Development Process.
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++.
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.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
The Class Construct Defining objects with attributes and behavior JPC and JWD © 2002 McGraw-Hill, Inc.
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.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
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.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Computing and Statistical Data Analysis Lecture 2
Basic Elements of C++.
Defining objects with attributes and behavior
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Learning Objectives String Class.
CS150 Introduction to Computer Science 1
COMS 261 Computer Science I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
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
Chapter 1 c++ structure C++ Input / Output
The Fundamentals of C++
Presentation transcript:

J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions

Ch 3 / Foil 2 Memory Depiction float y = 12.5; int Temperature = 32; char Letter = 'c'; int Number;

Ch 3 / Foil 3 Assignment Statement l Basic form l object = expression ; Celsius = (Fahrenheit - 32) * 5 / 9; y = m * x + b; i = i + 1; Remember = CurrentValue; l Action n Expression is evaluated n Expression value stored in object Target becomes source

Ch 3 / Foil 4 Assignment Statement int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents;

Ch 3 / Foil 5 Assignment Statement TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

Suppose Value1 = 10; Value2 = 20; Consider int Hold = Value1; Value1 = Value2; Value2 = Hold; Ch 3 / Foil 6

Ch 3 / Foil 7 Incrementing int i = 1; i = i + 1; Assign the value of expression i + 1 to i Evaluates to 2

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

// Program 3.2 #include using namespace std; int main() { cout << "Enter mass of hydrocarbon (in grams)\n" "followed by the number of carbon atoms\n" "followed by the number of hydrogen atoms\n" "(e.g ): " ; float Mass; int CarbonAtoms; int HydrogenAtoms; cin >> Mass >> CarbonAtoms >> HydrogenAtoms; Ch 3 / Foil 9

// Program 3.2 (continued) const int CarbonAMU = 12; const int HydrogenAMU = 1; long int FormulaWght = (CarbonAtoms * CarbonAMU) + (HydrogenAtoms * HydrogenAMU); const double AvogadroNbr = 6.02e23; double Molecules = (Mass / FormulaWght) * AvogadroNbr; cout << Mass << " grams of a hydrocarbon\nwith " << CarbonAtoms << " carbon atom(s) and " << HydrogenAtoms << " hydrogen atom(s)\ncontains " << Molecules << " molecules" << endl; return 0; } Ch 3 / Foil 10

Ch 3 / Foil 11 Sample I/O Behavior

Ch 3 / Foil 12 Assignment Conversions l A floating-point expression assigned to an integer object is truncated l An integer expression assigned to a floating-point object is converted to a floating-point value l Consider float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

Ch 3 / Foil 13 Compound Assignment l C++ has a large set of operators for applying an operation to an object and then storing the result back into the object l 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;

Ch 3 / Foil 14 Increment and Decrement l C++ has special operators for incrementing or decrementing an object by one l 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;

Ch 3 / Foil 15 Nonfundamental Types l Nonfundamental as they are additions l C++ permits definition of new types and classes n A class is a special kind of type l Class objects typically have n Data members that represent attributes and values n Member functions for object inspection and manipulation n Members are accessed using the selection operator (.) j = s.size(); n Auxiliary functions for other behaviors l Libraries often provide special-purpose types and classes l Programmers can also define their own types and classes

Ch 3 / Foil 16 Nonfundamental Types l Examples Standard Template Library (STL) provides class string n EzWindows library provides some graphical types and classes – SimpleWindow is a class for creating and manipulating window objects – RectangleShape is a class for creating and manipulating rectangle objects

Ch 3 / Foil 17 Nonfundamental Types l To access a library use a preprocessor directive to add its definitions to your program file #include l Using statement makes syntax less clumsy n Without it std::string s = "Wahoo"; std::string t = "Spiffy"; n With it using namespace std; // std contains string string s = "Wahoo"; string t = "Spiffy";

Ch 3 / Foil 18 Class string l Class string n Used to represent a sequence of characters as a single object l Some definitions string Name = "Joanne"; string DecimalPoint = "."; string Question = '?'; // illegal

Ch 3 / Foil 19 Class string l Some string member functions n size() determines number of characters in the string string Saying = "Rust never sleeps."; cout << Saying.size() << endl; // 18 n substr() determines a substring (Note first position has index 0) string Word = Saying.substr(11, 16); // sleeps n find() computes the position of a subsequence int j = Word.find("ee"); // 2 int k = Rust.find("steel"); // ?

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

Ch 3 / Foil 21 Class string l Auxiliary operators n + string concatenation string Part1 = "Me"; string Part2 = " and "; string Part3 = "You"; string All = Part1 + Part2 + Part3; n += compound concatenation assignment string ThePlace = "Brooklyn"; ThePlace += ", NY";

#include // Program 3.4 #include using namespace std; int main() { cout << "Enter the date in American format: " << "(e.g., December 29, 1953) : "; 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); string NewDate = Day + " " + Month + " " + Year; cout << "Original date: " << Date << endl; cout << "Converted date: " << NewDate << endl; return 0; }

Ch 3 / Foil 23 EzWindows Library Objects l Definitions are the same form as other objects l Example SimpleWindow W; l Most non-fundamental classes have been created so that an object is automatically initialized to a sensible value SimpleWindow objects have member functions to process messages to manipulate the objects Most important member function is Open() which causes the object to be displayed on the screen l Example W.Open();

Ch 3 / Foil 24 Initialization l Non-fundamental objects may have several attributes to initialize l Syntax for initializing an object with multiple attributes Type Identifier(Exp 1, Exp 2,..., Exp n ); l SimpleWindow definitions can optionally specify attributes SimpleWindow W("Window Fun", 8, 4); n First attribute –Window banner n Second attribute –Width of window in centimeters n Third attribute –Height of window in centimeters

Ch 3 / Foil 25 An EzWindows Program #include using namespace std; #include "ezwin.h" int ApiMain() { SimpleWindow W("A Window", 12, 12); W.Open(); cout << "Enter a character to exit" << endl; char a; cin >> a; return 0; }

Ch 3 / Foil 26 Sample Display Behavior

Ch 3 / Foil 27 RectangleShape Objects EzWindows library also provides RectangleShape class for manipulating rectangles RectangleShape objects can specify the following attributes A SimpleWindow object that contains the rectangle (mandatory) Offset from left edge of the SimpleWindow Offset from top edge of the SimpleWindow –Offsets are measured in centimeters from rectangle center n Width in centimeters n Height in centimeters n Color –color is an EzWindows type

Ch 3 / Foil 28 RectangleShape Objects l Examples SimpleWindow W1("My Window", 20, 20); SimpleWindow W2("My Other Window", 15, 10); RectangleShape R(W1, 4, 2, Blue, 3, 2); RectangleShape S(W2, 5, 2, Red, 1, 1); RectangleShape T(W1, 3, 1, Black, 4, 5); RectangleShape U(W1, 4, 9);

Ch 3 / Foil 29 RectangleShape Objects Major RectangleShape member functions for processing messages Draw() –Causes rectangle to be displayed in its associated window GetWidth() –Returns width of object in centimeters GetHeight() –Returns height of object in centimeters SetSize() –Takes two attributes -- a width and height -- that are used to reset dimensions of the rectangle

Ch 3 / Foil 30 Another EzWindows Program #include using namespace std; #include "rect.h" int ApiMain() { SimpleWindow W("Rectangular Fun", 12, 12); W.Open(); RectangleShape R(W, 5.0, 2.5, Blue); R.Draw(); cout << "Enter a character to exit" << endl; char Response; cin >> Response; return 0; }

Ch 3 / Foil 31 Sample Display Behavior