Modifying Objects Assignment operation Assignment conversions

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

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, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
Chapter 2: Basic Elements of C++
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';
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Programming Languages
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.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
Beginning C++ Through Game Programming, Second Edition
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Fundamental Programming: Fundamental Programming Introduction to C++
1 CS161 Introduction to Computer Science Topic #3.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 3: Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
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.
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
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
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.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Programming Fundamentals
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Basics (Variables, Assignments, I/O)
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Variables, Identifiers, Assignments, Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
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 4 File Processing.
What Actions Do We Have Part 1
COMS 261 Computer Science I
C++ Programming Basics
COMS 261 Computer Science I
The Fundamentals of C++
Presentation transcript:

Modifying Objects Assignment operation Assignment conversions Assignment precedence and associativity Extraction operations CONST declarations Compound assignment operations Input with cin Increment and decrement operation strings 5/6/2019 Y K Choi

Memory Depiction float y = 12.5; memory

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;

Assignment -- = The operator is = The operator stores a value into an object Example: int x = 3; int i = 4; //4 bytes for this variable i float weight = 120.4; //floating point (4 bytes for single precision) unsigned int i = 4; //no negative (4 bytes) Note that it is better to define unsigned for looping such as for unsigned i = … as it is faster.

Assignment Conversions #include <iostream> #include <string> using namespace std; void main() { short s1 = 0; long i2 = 65536; s1 = i2; cout<<"i2 is " << i2 <<endl; cout <<"s1 is " << s1 <<endl; } S1 is 0 instead of 65536, interesting, as 16 bits are reserved for s1, the highest 16 bits (16 out of 32) is chopped off.

Screen dump – note the value of s1

It means pi is always 3.1416 within the program. CONST definition The CONST definition is: const Type Identifier = Expression; const float light_speed = 3.0e8; //the speed of light const float pi = 3.1416; //the pi It means pi is always 3.1416 within the program.

Input statements Cin (pronounced as C in) and operator >> Example int x; //define i as an integer cin >> i; //get the value from keyboard and load into I char cvalue; //define cvalue as char cin >> cvalue; //load a single byte (8 bits) into cin

Screen dump

Note the operators used, it optimises the programs Compound assignments int j = 7; int i = 5; int k = 3; i /=k; //i becomes i = i/k j *= i; // j = j *i j %= i; //j = j %i Note the operators used, it optimises the programs

Increment and Decrement Operator ++ and – Example After the operation, i becomes 4 Before this operation, i becomes 4 int i = 3; ++i; cout <<“i is “ <<i <<endl; int i = 3; i++; cout <<“i is “ <<i <<endl; Both will display the values of 4.

The operation is j * (i +1) How about j *i++; Screen dump The operation is j * (i +1)

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

Examples Standard Template Library (STL) provides class string EzWindows library provides several graphical types and classes SimpleWindow is a class for creating and manipulating window objects RectangleShape is a class for creating and manipulating rectangle objects

Class string Class string Some definitions 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

Nonfundamental Types To access a library use a preprocessor directive to add its definitions to your program file #include <string> The using 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";

Screen dump

Summary Assignment conversions Assignment precedence and associativity Extraction operations CONST declarations Compound assignment operations Input with cin >> x Increment and decrement operation ++ and -- Strings