Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

1 © 1998, 2001, 2002Calvin College, Keith Vander Linden, Jeremy D. Frens, Larry R. Nyhoff Objects (Chap. 2) Variables and Constants.
Data Types and Expressions
Types and Variables. Computer Programming 2 C++ in one page!
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
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
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
C-Language Keywords(C99)
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Operations Making Things Happen. Our Scuba Program #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
COSC1557: Introduction to Computing Haibin Zhu, PhD. Professor Department of Computer Science Nipissing University (C) 2014.
Objects Types, Variables, and Constants 1 Chapter 3.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
PROCESSING Types. Objectives Be able to convert between binary and decimal numbers. Be able to declare and initialize character variables and constants.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 2 Variables.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
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.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1 Objects Types, Variables, and Constants Chapter 3.
A Sample Program #include using namespace std; int main(void) { cout
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2 Variables.
Chapter 2: Introduction to C++
ECE Application Programming
Documentation Need to have documentation in all programs
Wel come.
Computing Fundamentals
Chapter 2: Introduction to C++
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Chapter 2: Introduction to C++.
Chapter 2 Variables.
Variables and Constants
The Fundamentals of C++
Programming Fundamental-1
Presentation transcript:

Objects Variables and Constants

Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM = 14.7; cout << "\nScuba pressure calculator!!" << "\n Enter the depth (feet): "; double depth; cin >> depth; double pressure = ((depth / FEET_PER_ATM) + 1) * LBS_PER_SQ_IN_PER_ATM; cout << "\nThe pressure at " << depth << " feet is " << pressure << "lbs/sq.in." << endl; }

Expressions In a C++ program, any sequence of objects and operations that combine to produce a value is called an expression. Here is an example from our scuba problem: double pressure = ((depth / FEET_PER_ATM) + 1) * LBS_PER_SQ_IN_PER_ATM; Today, we’re going to focus on C++ objects...

Object Categories There are three kinds of objects: Literals: unnamed objects having a value (... )Literals: unnamed objects having a value ( 0, -3, 2.5, 2.998e8, 'A', "Hello\n",... ) Variables: named objects whose values can change during program executionVariables: named objects whose values can change during program execution Constants: named objects whose values do not change during program executionConstants: named objects whose values do not change during program execution

Literals – literals are whole numbers: –int literals are whole numbers: -27, 0, 4, +4 – literals are real numbers, and can be: –double literals are real numbers, and can be: fixed-point:,...fixed-point: , 0.5, 1.414,... floating-point:,...floating-point: 2.998e8, e9,... –There are just two literals: –There are just two bool literals: false, true – literals are single ASCII characters:... –char literals are single ASCII characters: 'A', 'a', '9', '$', '?',... – literals are ASCII character sequences:,... –string literals are ASCII character sequences: "Hello", "Goodbye", "Goodbye\n",...

Variable Declarations Variables are used to store values, and can be either initialized or uninitialized... Examples: int age = 18; double GPA = 3.25, credits; char letterGrade = 'A'; bool ok, done = false; Pattern: Pattern: Type Name [ = Expression ] ;

Assignment Statements The value of a variable can be changed using an assignment statement... Examples: age = 19; credits = hours * 3.0; letterGrade = 'B'; done = true; Pattern: Pattern: Name = Expression;

Constant Declarations Constants are used to represent a value with a meaningful name, and must be initialized. Examples: const int MAX_SCORE = 100; const double PI = ; const char MIDDLE_INITIAL = 'A'; const string PROMPT = "Enter a number: "; Pattern: Pattern: const Type Name = Expression;

Identifiers Technically, the name of an object is called an identifier (it identifies the object). C++ identifiers must begin with a letter (underscores are permitted, but discouraged) followed by zero or more letters, digits or underscores. Valid:,... Valid: age, r2d2, myGPA, MAX_SCORE,... Invalid:,... Invalid: 123go, coffee-time, sam’s, $name,...

Conventions To keep variable and constant objects distinct: Constant names are all uppercase, with multiple words separated by underscores (e.g., )Constant names are all uppercase, with multiple words separated by underscores (e.g., MAX_SCORE ) Variable names are all lowercase, with the first letter of each word after the first capitalized (e.g., )Variable names are all lowercase, with the first letter of each word after the first capitalized (e.g., lastName )

Objects char Objects... are represented in memory by a code –ASCII code uses 8 bits to represent a character, allowing for 2 8 = 256 different characters. –Unicode uses 16 bits to represent a character, allowing for 2 16 = 65,536 different characters. ASCII is the most commonly used code: '0' = 48 = 'A' = 65 = 'a' = 97 =

Escape Characters C++ provides a number of escape characters: '\n' newline character '\t' horizontal tab '\v' vertical tab '\f' form feed '\a' alert/bell '\\' backslash char '\'' apostrophe '\"' double quote '\ooo' char with octal code ooo '\hhh' char with hex code hhh

Objects int Objects Three forms: –decimal (base-10): begin with a non-zero or sign (-45, -2, 0, +21, 36, 65536,...) –octal (base-8): a zero followed by digits (01, 02, 03, 04, 05, 06, 07, 010, 011, 012,...) –hexadecimal (base-16): zero-x followed by digits with a, b, c, d, e, f = 10, 11, 12, 13, 14, 15 (0x1, 0x2,..., 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11,...)

Representation int Representation Integers are often represented in the twos-complement format, where the high- order bit indicates the number’s sign: 2 10 = = = = = We show 16 bits, but 32 or 64 are common.

Twos-Complement To find twos-complement representation of a negative number: Select your number (e.g., -12)Select your number (e.g., -12) Represent its absolute value in binary: ( )Represent its absolute value in binary: ( ) Invert the bits ( )Invert the bits ( ) Add 1 ( )Add 1 ( )

Objects unsigned Objects For objects whose values are never negative, C++ provides the type: For objects whose values are never negative, C++ provides the unsigned type: = = = = = With no sign bit, numbers can be twice as big.

vs. int vs. unsigned Using 32 bits, values range from ( ) to ( ), whereas values range from 0 to ( ). Using 32 bits, int values range from ( ) to ( ), whereas unsigned values range from 0 to ( ). An value “loses” one of its bits to the sign, and so the maximum value is about half of the maximum value. An int value “loses” one of its bits to the sign, and so the maximum int value is about half of the maximum unsigned value.

Objects double Objects Real values are often represented in 64 bits using the IEEE floating point standard: sign (1 bit) mantissa (52 bits) exponent (11 bits)

Summary C++ provides three kinds of objects: literals, variables and constants. Literals have a “built-in” type; a declaration statement is the means by which a type is associated with a variable or constant. The C++ fundamental types include,,,,,,,, and. The C++ fundamental types include bool, char, int, short, long, unsigned, float, double, and long double.