C++ Loose ends from last time. Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10);

Slides:



Advertisements
Similar presentations
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Advertisements

CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
True or false A variable of type char can hold the value 301. ( F )
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Basic Elements of C++ Chapter 2.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Systems Programming Concepts
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Beginning C++ Through Game Programming, Second Edition
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.
Looping and Counting Lecture 3 Hartmut Kaiser
C++ Programming Lecture 10 Functions – Part II
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
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.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Unit 2 Test: Tues 11/3. ASCII / Unicode –Each letter, symbol, etc has a # value –See ascii table (in folder) –To convert a char into its ascii value,
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 9: Value-Returning Functions
LESSON 06.
Chapter 1.2 Introduction to C++ Programming
Chapter 4 Assignment Statement
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Objectives In this chapter, you will:
Computing and Statistical Data Analysis Lecture 2
Completing the Problem-Solving Process
Programming Fundamentals
C Programming Tutorial – Part I
Basic Elements of C++.
Multiple variables can be created in one declaration
COSC 220 Computer Science II
Chapter 3 Assignment Statement
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Instructor: Ioannis A. Vetsikas
بسم الله الرحمن الرحيم.
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Arrays Kingdom of Saudi Arabia
More About Objects and Methods
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
Chapter 1 c++ structure C++ Input / Output
Seoul National University
Presentation transcript:

C++ Loose ends from last time

Variable initialization You can do the usual things int x; x = 10; int y = 20; And you can do an unusual thing int x(10); –While this looks like a call to a constructor, it isn’t. –This strange thing is necessary under certain situations (inheritance) which we will see later Uninitialized variables are just that, uninitialized! –Remember how Java deals with this?

Primitive data types Table on page 58 of the text book

Literals Table on page 59 of the text book Gotchas is a double F is a float ‘a’ is a char L‘a’ is a wide char (unicode) – some API functions may want this 27 is an int (native architecture word length) 27U is an unsigned int 0x9FC is a hexidecimal int

Synonyms When the names of primitive data types aren’t good enough for you long int x; –can be replaced by typedef long int LONGINT; LONGINT x; Most of the time this is cosmetic but there are times when you must use it (some parameter passage mechanisms)

User defined data types (enum) enum (Java has this too) enum Week {Mon, Tues, Wed, Thurs, Fri, Sat, Sun}; –Defines a datatype called Week Week thisWeek; –Instantiates a variable of type Week thisWeek = Fri ; Assigns a value to the variable Only values Mon, Tues, Wed, Thurs, Fri, Sat, Sun are legal assignments

User defined data types (enum) Values in an enum are ordered as specified in the list with the first element equal to 0 –e.g. Mon=0, Tues=1, Wed=2, … Sun=6 But, values can be changed enum Week {Mon = 1, Tues = 7, Wed = 7…}; and don’t even have to be unique

Constants You can define constant values within your program using the const type modifier const double PI = ; Remember how it’s done in Java?

Operator precedence Pretty much as expected Table on page 77 of the text book USE PARENTHESIS!!!

Type casting Narrowing (e.g. assigning a double to a float) is only a compiler warning, not an error as in Java This is bad… int i = 257; unsigned char uc = i; std::cout << (int)uc << std::endl; Prints out 1 (not 257)

Type casting int x = static_cast (47.3); Performs the cast at compile time – no runtime safety checks are performed There are other, runtime safe casts to be discussed sometime in the future dynamic_cast<> const_cast<> reinterpret_cast<> The old way (like Java uses) works too int x = (int)(47.3); As does this abomination (under some cases) int x = int(47.3);

Static variables Similar to Java when in a class definition Dissimilar (totally different) than Java when –defined at the global (no class) level – Java doesn’t even allow this Makes it global within the file only –Defined within a function – Java doesn’t allow this either Makes the variable hold it’s value for the life of the program, even when the function exits!

Namespace A way of creating separate spaces within a program to avoid naming clashes namespace mySpace { int x = 27; } namespace yourSpace { int x = 32; } –There is no conflict if these exist in the same program (or even the same file) To access the values use the namespace operator mySpace::x yourSpace::x

Assignment check Read chapter 2 Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell you what the operator is, then tells the user if they were correct or incorrect and, if incorrect, gives the correct answer. The program should loop asking the user if they want another problem to solve and stop when they don’t. It should also keep track of the number of correct and incorrect answers and report the score when the user is finished. Example on next page

Example execution Expression: 12 ? 4 = 3 Your answer: / You are correct! Again? Y Expression 3 ? 3 = 9 Your answer: + You are incorrect! Correct answer is * Again? N 1 correct, 1 incorrect Bye C:>

To generate a random number // -- read the seed for the random number generator int seed; std::cout << "random seed: "; std::cin >> seed; // -- only seed one time per program run if you don’t change the // seed then the program will generate the same random numbers // each time you run it (pseudo random number generator) srand(seed); // -- rand() generates an integer between 0 and RAND_MAX // (Microsoft defined constant) so we need to scale it from // 0.0 to 1.0 (by dividing) then multiple by and truncate // to get a value from 0 to 100 float y = (float)rand() / RAND_MAX; int rn = (int)(y * 100.0); // -- print it out (just for this example) std::cout << rn << std::endl;