A reference is an alias it sets up another name for

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: able int a = 5; int & b = a; // b is now an alias for a a: b: 5 •  Note: this picture is impossible in Java, which does not allow multiple names for the same variable •  Java does allow multiple pointer variables to point to the same object, but that is not the same thing at all! Scott B. Baden / CSE 100, Lec 2 / Spring 2013 35 Reference function parameters •  References are most useful in application to function parameters, where they permit true pass-by-reference semantics, avoiding any argument copying •  For example, you can easily write a true swap function using reference parameters: void swap(int& a, int& b) { int tmp = a; a = b; b = tmp; } -----------int x = 5, y = 10; swap(x,y); std::cout << x << ", " << y << std::endl; // prints 10, 5 •  Note that the same effect can be had by using pointers, but with messier syntax: void swap(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } -----------int x = 5, y = 10; swap(&x,&y); std::cout << x << ", " << y << std::endl; // prints 10, 5 Scott B. Baden / CSE 100, Lec 2 / Spring 2013 36 Today’s Lecture •  An introduction to C++ •  Comparing Java and C++ •  Basic C++ programming •  Pointers and pointer arithmetic •  Pragmatics – quickly quickly, read the book & slides •  C++ primitive types and operators •  Reading: Weiss Ch 1, section 1.4 to 1.7 Scott B. Baden / CSE 100, Lec 2 / Spring 2013 37 Writing, compiling and running programs in Java and C++ •  The traditional first example when learning a programming language is the simplest possible program that performs some output: a console application that says “Hello world!” •  Let’s look at and compare Java and C++ “Hello world” programs Scott B. Baden / CSE 100, Lec 2 / Spring 2013 38 Compiling and running “Hello World” in Java •  In Java, all code must be inside the body of a class definition; in this case, there is just one class, a public class named HelloWorld •  A public class definition has to appear in a file with the same name as the class, and a .java extension; in this case, HelloWorld.java •  The public class HelloWorld contains a public static method named main that takes an array of Strings as argument •  That main method is the entry point when HelloWorld is run as a program •  From the command line, compile a Java source code file using the javac compiler: javac HelloWorld.java •  ... which will produce a file HelloWorld.class of Java Virtual Machine bytecodes •  Run the program by running the java interpreter, telling it the name of the class you want to run as a program: java HelloWorld •  ... which will print to standard output: Hello World! public class HelloWorld { public static void main (String args) { System.out.println("Hello World!"); } } Scott B. Baden / CSE 100, Lec 2 / Spring 2013 39 “Hello World” in C++ •  A...
View Full Document

Ask a homework question - tutors are online