CSCE 121:509-512 Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 3 Objects, Types, and Values Hartmut Kaiser
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Names Variables Type Checking Strong Typing Type Compatibility 1.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Lecture #5 Introduction to C++
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Chapter 3 Objects, types, and values Bjarne Stroustrup
Looping and Counting Lecture 3 Hartmut Kaiser
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
Copyright Curt Hill Variables What are they? Why do we need them?
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
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.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring Fall 2016 Set 10: Input/Output Streams 1 Based on slides created.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Chapter 3 Objects, types, and values
Documentation Need to have documentation in all programs
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.
Type Conversion, Constants, and the String Object
Chapter 3 Objects, Types, and Values
Variables with Memory Diagram
Chapter 3 Objects, types, and values
CSCE 121- Spring 2016 J. Michael Moore
Introduction to C++ Programming
Chapter 3 Objects, types, and values
Primitive Types and Expressions
Presentation transcript:

CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides created by Bjarne Stroustrup and Jennifer Welch

CSCE 121: Set 3: Objects, Types and Values  Object: region of memory with a type that specifies what kind of information can be stored there and what operations can be performed on it  Variable: object with a name  Declaration: statement that gives a name to an object  Definition: declaration that also sets aside memory for a variable  Value: data item put into a variable  Examples: int age = 42; string name = “Fred”; 2

CSCE 121: Set 3: Objects, Types and Values Common Built-in Types int num = 39; // integer double ratio = 4.1; // real number char qmark = ‘?’; // single character string name = “Fred”; // sequence of characters bool finished = false; // true or false 39, 4.1, ‘?’, “Fred”, and false are examples of literals. 3

CSCE 121: Set 3: Objects, Types and Values More Types C++ has a few more built-in types C++ programmers can define new types: “user-defined” types C++ standard library provides a set of types string, vector, complex,… (Technically, these are also user-defined types) C++ standard library provides a set of types string, vector, complex,… (Technically, these are also user-defined types) 4

CSCE 121: Set 3: Objects, Types and Values Types and Operations Strings cin >> reads a string cout <<writes a string + concatenates ++ error! - error! … Numbers (int, double,…) cin >> reads a number cout << writes a number + adds ++ increments by 1 - subtracts … Variable’s type determines which operations are valid and what they mean (“overloading”). Compiler makes sure you use each variable according to its type. 5

CSCE 121: Set 3: Objects, Types and Values Names Used for variables, functions, types,… Rules: Start with a letter Contain only letters, digits and underscores _ Cannot (re)use keywords (e.g., int, if, while, double) Good ideas: Don’t start with _ : reserved for low-level entities Choose meaningful names! Not too short, not too long, but just right. 6

CSCE 121: Set 3: Objects, Types and Values Initialization vs. Assignment Initialization gives a variable its initial value, when declared/defined int n = 10; Assignment gives an existing variable a new value n = n*2; 7

CSCE 121: Set 3: Objects, Types and Values Initialization Always initialize your variables! Beware that “debug mode” in your compiler may initialize for you Valid exception to this rule is an input variable 8

CSCE 121: Set 3: Objects, Types and Values Initialization with C++11 You can use the type of an initializer as the type of a variable with “auto”: auto x = 1; // 1 is an int, so x is an int auto y = ‘c’; // ‘c’ is a char, so y is a char auto d = 1.2; // 1.2 is a double, so d is a double auto s = “Howdy”; // s has same type as “Howdy” // (more later on that) auto sq = sqrt(2);// sq has right type for result of // sqrt(2), and you don’t have to // remember what it is 9

CSCE 121: Set 3: Objects, Types and Values Type Safety Definition: every object will be used only according to its type Variable is only used after it is initialized Only operations defined for the variables type will be applied Every operation defined for a variable results in a valid value Ideal: static type safety Compiler finds all type safety violations Ideal: dynamic type safety Run-time system finds all type safety violations not found by compiler 10

CSCE 121: Set 3: Objects, Types and Values Type Safety Type safety is very important! Try very hard not to violate it Compiler is your friend C++ is not completely statically type safe No widely-used language is Reduces ability to express ideas C++ is not completely dynamically type safe Many languages are, but… Being dynamically type safe can cause performance problems Almost everything in this class will be type safe 11

CSCE 121: Set 3: Objects, Types and Values Type Conversion 12 C++ allows for implicit conversions. One type converted to another type automatically Beware!!! Safe conversions No information lost char c = ‘x’; int i1 = c; int i2 = ‘x’; Unsafe conversions “Narrowing” conversions double x = 2.7; int y = x; int a = 1000; char b = a; How to ‘outlaw’ unsafe conversions???

CSCE 121: Set 3: Objects, Types and Values About Efficiency For now, don’t worry about “efficiency” Concentrate on correctness and simplicity of code C++ is derived from C, a systems programming language A char stored in a byte An int is stored in a word A double fits in a floating-point register C++ built-in operations map directly to machine instructions Integer + is implemented by integer add operation, etc. C++ helps programmers build safer, more elegant and efficient code using built-in types and operations 13

CSCE 121: Set 3: Objects, Types and Values A Bit of Philosophy Like other branches of engineering, programming involves tradeoffs Conflicting ideals for a program Type safety Run-time performance Portability across platforms Compatibility with other code Each of construction Ease of maintenance Don’t skimp on correctness or testing! Aim for type safety and portability 14

CSCE 121: Set 3: Objects, Types and Values Acknowledgments Photo on slide 1: “Broken Type” by Javier Garcia, licensed under CC BY-NC 2.0“Broken Type” Javier GarciaCC BY-NC 2.0 Slides are based on those for the textbook: 15