C++ for Java Programmers

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

description

C++ for Java Programmers. Chapter 2. Fundamental Daty Types Timothy Budd. Integers. 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 C++ Integer Internal Representation - PowerPoint PPT Presentation

Transcript of C++ for Java Programmers

Page 1: C++ for Java Programmers

C++ for Java Programmers

Chapter 2. Fundamental Daty Types

Timothy Budd

Page 2: C++ for Java Programmers

Timothy BuddTimothy Budd 22

Integers

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

C++ Integer Internal Representation• long and/or short may have the same size as integer

Page 3: C++ for Java Programmers

Timothy BuddTimothy Budd 33

C++ Integer

An unsigned integer can only hold nonnegative values

int i = -3;

unsigned int j = i;

cout << j << endl; // will print very large positive integer

Assigning a negaitve value to an unsigned variable is confusing

Integer division involving negative numbers is platform dependent, but following equality must be preserved: a == (a / b) * b + a % b

Page 4: C++ for Java Programmers

Timothy BuddTimothy Budd 44

Integers

Never use the remainder operator with negative values.

unsigned long a; // can hold largest integer value

signed short int b;

C++ does not recognize the Byte data type in Java. Instead signed char is often used to represent byte-sized quantities.

Page 5: C++ for Java Programmers

Timothy BuddTimothy Budd 55

Characters

8 bit quatity - Legal to perform arithmatic on characters Character can be signed or unsigned. w_char - recent addition wide character

alias for another interger type such as short.

Page 6: C++ for Java Programmers

Timothy BuddTimothy Budd 66

Booleans

Recent addtion - bool Historical boolean representation

nonzero - true zero - false

Integer and pointer types can be used as boolean values.

Cannot be signed or unsigned.

Page 7: C++ for Java Programmers

Timothy BuddTimothy Budd 77

Examples of Booleans

int i = 10;while (i) { // will loop until i is zero

...i--;

}

while (*p++ = *q++) ;

bool test = true;int i = 2 + test; // i is now 3test = test - 1; // test is now 0, or false

Page 8: C++ for Java Programmers

Timothy BuddTimothy Budd 88

Booleans

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

Legacy code can contain different boolean abstractions.

Page 9: C++ for Java Programmers

Timothy BuddTimothy Budd 99

Bit Fields

Seldome used feature 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

};

Page 10: C++ for Java Programmers

Timothy BuddTimothy Budd 1010

Floating Point Values

float, double, long double

int i;

double d = 3.14;

i = d; // may generate a warning

Never use float; use double instead. math rountines will not throw an exception

on error

Page 11: C++ for Java Programmers

Timothy BuddTimothy Budd 1111

Floating Point Values

Always check errno

double d = sqrt(-1); // should generate error

if (errno == EDOM)

... // but only caught if checked

Java: Nan, NEGATIVE INFINITY, POSITIVE INFINITY

Page 12: C++ for Java Programmers

Timothy BuddTimothy Budd 1212

Enumerated Values

Nothing in commonwith Enumeration calss in Java

enum declaration in C++

enum color {red, orange, yellow};

enum fruit {apple, pear, orange};

// error: orange redefined

Page 13: C++ for Java Programmers

Timothy BuddTimothy Budd 1313

Enumeration Values

Can be converted into integers and can even have their own internal integer values explicitly specified.

enum shape {circle=12, square=3, triangle};

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

Page 14: C++ for Java Programmers

Timothy BuddTimothy Budd 1414

Enumeration Values

Cast operation can be written by type(value) Cast operation can be written by type(value) or older (type)value syntax.or older (type)value syntax.

Not legal to change a pointer type.Not legal to change a pointer type.

int * i;char * c;

c = char *(i); // error: not legal syntax

static_cast would be even better.

Page 15: C++ for Java Programmers

Timothy BuddTimothy Budd 1515

The void type

In Java, used to represent a method or function that does not yield a result.

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.

Page 16: C++ for Java Programmers

Timothy BuddTimothy Budd 1616

Arrays

An array need not be allocated by using new directive as in Java.

The number of element determined at compile time.

int data[100]; // create an array of 100 elements

The number of element can be omitted.char text[ ] = "an array of characters";

int limits[ ] = {10, 12, 14, 17, 0};

Page 17: C++ for Java Programmers

Timothy BuddTimothy Budd 1717

Arrays

Not legal to place the square brackets after type as in Java

double[ ] limits = {10, 12, 14, 17, 0}; // legal Java, not C++

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; }

Page 18: C++ for Java Programmers

Timothy BuddTimothy Budd 1818

Structure

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;

};

Page 19: C++ for Java Programmers

Timothy BuddTimothy Budd 1919

Unions

Similar to a structure, but the different data fields Similar to a structure, but the different data fields all sharre the same location in memory.all sharre the same location in memory.

// can hold an int, a double, OR a pointer

union myUnion {

int i;

double d;

anObject * p;

};

Object-orienteObject-oriented languages made d languages made unions unions unnecessary by introducing unnecessary by introducing polymorphic polymorphic variablesvariables

Page 20: C++ for Java Programmers

Timothy BuddTimothy Budd 2020

Object Values

Java uses reference semantics for assignmentclass box {class box { // Java box // Java box

public int value;public int value;}}

box a = new box();box a = new box();box b;box b;

a.value = 7; a.value = 7; // set variable a// set variable ab = a; b = a; // assign b from a// assign b from aa.value = 12; a.value = 12; // change variable a// change variable aSystem.out.println("a value " + a.value);System.out.println("a value " + a.value);System.out.println("b value " + b.value);System.out.println("b value " + b.value);

Page 21: C++ for Java Programmers

Timothy BuddTimothy Budd 2121

Object Values

C++ uses copy semantics.class box {class box { // C++ box// C++ boxpublic:public:

int value;int value;};};

box a; // note, explicit allocation not requiredbox a; // note, explicit allocation not requiredbox b;box b;

a.value = 7;a.value = 7;b = a;b = a;a.value = 12;a.value = 12;cout << "a value " << a.value << endl;cout << "a value " << a.value << endl;cout << "b value " << b.value << endl;cout << "b value " << b.value << endl;

Page 22: C++ for Java Programmers

Timothy BuddTimothy Budd 2222

Object Values

The concept of reference variable in C++, which is a variable declared as a direct alias.

box a = new box(); // java reference assignmentbox a = new box(); // java reference assignment

box b = a;box b = a;

b = new box();b = new box(); // reassignment of reference// reassignment of reference

box a; box a; // C++ example// C++ example

box & b = a;box & b = a; // reference assignment// reference assignment

box c;box c;

b = c;b = c; // error: not permitted to reassign reference// error: not permitted to reassign reference

Page 23: C++ for Java Programmers

Timothy BuddTimothy Budd 2323

Functions

C++ permits the definition of function that are not member of any class.

// define a function for the maximum // of two integer valuesint max (int i, int j) {

if (i < j) return j;return i;

}

int x = ...;int y = ...;int z = max(x, y);

Page 24: C++ for Java Programmers

Timothy BuddTimothy Budd 2424

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);

Page 25: C++ for Java Programmers

Timothy BuddTimothy Budd 2525

Order of Argument Evaluation

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);

}

In C++, order of argument evaluation is In C++, order of argument evaluation is undefined and implement dependent.undefined and implement dependent.

Page 26: C++ for Java Programmers

Timothy BuddTimothy Budd 2626

The function main

In C++, main is a function outside any class. 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

}

The first command line argument in C++ is always the application name.

Page 27: C++ for Java Programmers

Timothy BuddTimothy Budd 2727

Altenative main Entry points

Individual libraries may provide threir own version of main and then require a different entry point.

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.