C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.

Slides:



Advertisements
Similar presentations
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
C++ Basics Variables, Identifiers, Assignments, Input/Output.
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.
Chapter 9 Imperative and object-oriented languages 1.
Informática II Prof. Dr. Gustavo Patiño MJ
Dale/Weems/Headington
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Data Types.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Basic Elements of C++ Chapter 2.
Programming Languages
C++ for Java Programmers Chapter 2. Fundamental Data Types Timothy Budd.
Ch 7. Operator Overloading Timothy Budd. Ch 7. Operator Overloading2 Introduction Almost all operators in C++ can be overloaded with new meanings. Operators.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
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.
1 C++ Syntax and Semantics, and the Program Development Process.
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.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
CS 261 – Data Structures Introduction to C Programming.
Data Types Declarations Expressions Data storage C++ Basics.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented Software Development 4. C# data types, objects and references.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Object Oriented Programming Lecture 2: BallWorld.
Java Programming Language Lecture27- An Introduction.
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 Topics The Basics of a C++ Program Data Types
The Machine Model Memory
Chapter 7: Expressions and Assignment Statements
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
C Basics.
Pointers and References
Basic Elements of C++ Chapter 2.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Introduction to Abstract Data Types
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Java Basics Data Types in Java.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd

2 Integers n n Java Integer Internal Representation long - 32 bit integer -32 bit long - 64 bit short int x; // declare x as a small integer long y; // declare y as long integer n n C++ Integer Internal Representation long and/or short may have the same size as integer

Timothy Budd3 C++ Integer n n An unsigned integer can only hold nonnegative values int i = -3; unsigned int j = i; cout << j << endl; // will print very large positive integer n n Assigning a negaitve value to an unsigned variable is confusing n n Integer division involving negative numbers is platform dependent, but following equality must be preserved: a == (a / b) * b + a % b

Timothy Budd4 Integers n n Never use the remainder operator with negative values. unsigned long a; // can hold largest integer value signed short int b; n n C++ does not recognize the Byte data type in Java. Instead signed char is often used to represent byte-sized quantities.

Timothy Budd5 Characters n n 8 bit quatity - n n Legal to perform arithmatic on characters n n Character can be signed or unsigned. n n w_char - recent addition wide character alias for another interger type such as short.

Timothy Budd6 Booleans u u Recent addtion - bool u u Historical boolean representation u u nonzero - true u u zero - false u u Integer and pointer types can be used as boolean values. u u Cannot be signed or unsigned.

Timothy Budd7 Examples of Booleans

Timothy Budd8 Booleans n. n Even pointer value can be used as booleans. False if it is null, true otherwise. aClass * aPtr; // declare a pointer variable... if (aPtr)// will be true if aPtr is not null n n Legacy code can contain different boolean abstractions.

Timothy Budd9 Bit Fields n n Seldome used feature n n Programmer can specify explicitly the number of bits to be used. struct infoByte { int on:1; // one-bit value, 0 or 1 int :4; // four bit padding, not named int type: 3; // three bit value, 0 to 7 };

Timothy Budd10 Floating Point Values n n float, double, long double int i; double d = 3.14; i = d; // may generate a warning n n Never use float; use double instead. n n math rountines will not throw an exception on error

Timothy Budd11 Floating Point Values n n Always check errno double d = sqrt(-1); // should generate error if (errno == EDOM)... // but only caught if checked n n Java: Nan, NEGATIVE INFINITY, POSITIVE INFINITY

Timothy Budd12 Enumerated Values n n Nothing in commonwith Enumeration calss in Java n n enum declaration in C++ enum color {red, orange, yellow}; enum fruit {apple, pear, orange}; // error: orange redefined

Timothy Budd13 Enumeration Values u uCan be converted into integers and can even have their own internal integer values explicitly specified. enum shape {circle=12, square=3, triangle}; n n Can be assigned to an integer and incremented, but the resulting value must then be cast back into the enumrated data type before fruit aFruit = pear; int i = aFruit; // legal conversion i++; // legal increment aFruit = fruit(i); // fruit is probably now orange i++; aFruit = fruit(i); // fruit value is now undefined

Timothy Budd14 Enumeration Values n Cast operation can be written by type(value) or older (type)value syntax. n Not legal to change a pointer type. int * i; char * c; c = char *(i); // error: not legal syntax n n static_cast would be even better.

Timothy Budd15 The void type n n In Java, used to represent a method or function that does not yield a result. n n In C++, type can also be uses as a pointer type to describe a “universal” pointer that can hold a pointer to any type of value.

Timothy Budd16 Arrays n n An array need not be allocated by using new directive as in Java. n n The number of element determined at compile time. int data[100]; // create an array of 100 elements n n The number of element can be omitted. char text[ ] = "an array of characters"; int limits[ ] = {10, 12, 14, 17, 0};

Timothy Budd17 Arrays n n Not legal to place the square brackets after type as in Java double[ ] limits = {10, 12, 14, 17, 0}; // legal Java, not C++ n n The limit can be omitted when arrays are passed as arguments to a function. // compute average of an array of data values double average (int n, double data[ ] ) {double sum = 0; for (int i = 0; i < n; i++) { sum += data[i];} return sum / n;}

Timothy Budd18 Structure n n The major differences in C++ between a strunct and a class is that the access is by default public rather than private as in classes. // holds an int, a double, AND a pointer struct myStruct { int i; double d; anObject * p; };

Timothy Budd19 Unions n Similar to a structure, but the different data fields all sharre the same location in memory. // can hold an int, a double, OR a pointer union myUnion { int i; double d; anObject * p; }; n Object-oriented languages made unions unnecessary by introducing polymorphic variables

Timothy Budd20 Object Values n n Java uses reference semantics for assignment class box {// Java box public int value; } box a = new box(); box b; a.value = 7; // set variable a b = a; // assign b from a a.value = 12; // change variable a System.out.println("a value " + a.value); System.out.println("b value " + b.value);

Timothy Budd21 Object Values n n C++ uses copy semantics. class box {// C++ box public: int value; }; box a; // note, explicit allocation not required box b; a.value = 7; b = a; a.value = 12; cout << "a value " << a.value << endl; cout << "b value " << b.value << endl;

Timothy Budd22 Object Values n n The concept of reference variable in C++, which is a variable declared as a direct alias. box a = new box(); // java reference assignment box b = a; b = new box();// reassignment of reference box a; // C++ example box & b = a;// reference assignment box c; b = c;// error: not permitted to reassign reference

Timothy Budd23 Functions n n C++ permits the definition of function that are not member of any class. // define a function for the maximum // of two integer values int max (int i, int j) { if (i < j) return j; return i; } int x =...; int y =...; int z = max(x, y);

Timothy Budd24 Functions Prototypes are necessary in C++ as every function name with its associated parameter types must be known to the compiler. // declare function max defined elsewhere int max(int, int);

Timothy Budd25 Order of Argument Evaluation n n In Java, argument is evaluated from left to right. String s = "going, "; printTest (s, s, s = "gone "); void printTest (String a, String b, String c) { System.out.println(a + b + c); } n In C++, order of argument evaluation is undefined and implement dependent.

Timothy Budd26 The function main n n In C++, main is a function outside any class. n n Always return zero on successful completion of the main program. int main (int argc, char *argv[ ]) { cout << "executing program " << argv[0] << '\n'; return 0; // execution successful } n n The first command line argument in C++ is always the application name.

Timothy Budd27 Altenative main Entry points n n Individual libraries may provide threir own version of main and then require a different entry point. n n Many Windows graphical systems come with their own main routine already written, which will perform certain initializations before invoking a different function such as WinMain.