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

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

Embed Size (px)

Citation preview

Page 1: 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

CSCE 121:509-512Introduction to Program Design and Concepts, HonorsDr. J. Michael MooreSpring 2015Set 3: Objects, Types, and Values

1Based on slides created by Bjarne Stroustrup and Jennifer Welch

Page 2: 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

CSCE 121:509-512 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 nameDeclaration: statement that gives a name to an

objectDefinition: declaration that also sets aside memory

for a variableValue: data item put into a variableExamples:

int age = 42;string name = “Fred”;

2

Page 3: 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

CSCE 121:509-512 Set 3: Objects, Types and Values

Common Built-in Typesint 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

Page 4: 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

CSCE 121:509-512 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)

4

Page 5: 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

CSCE 121:509-512 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 whatthey mean (“overloading”).Compiler makes sure you use each variable according to its type.

5

Page 6: 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

CSCE 121:509-512 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

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

CSCE 121:509-512 Set 3: Objects, Types and Values

Initialization vs. Assignment

Initialization gives a variable its initial value, when declared/definedint n = 10;

Assignment gives an existing variable a new valuen = n*2;

7

Page 8: 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

CSCE 121:509-512 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

Page 9: 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

CSCE 121:509-512 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 intauto y = ‘c’; // ‘c’ is a char, so y is a charauto d = 1.2; // 1.2 is a double, so d is a doubleauto 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

Page 10: 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

CSCE 121:509-512 Set 3: Objects, Types and Values

Type SafetyDefinition: 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

Page 11: 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

CSCE 121:509-512 Set 3: Objects, Types and Values

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

Page 12: 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

CSCE 121:509-512 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???

Page 13: 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

CSCE 121:509-512 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

Page 14: 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

CSCE 121:509-512 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