Download Program entry point

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
C++
As compared to Java
Program entry point
• In Java, the main method [program
entry point] belongs to a specific class
and every class
• In C++ the main [program entry point]
function is a global function
– Does not belong to any class
Command line arguments
• In Java the main method takes a single argument of
type String[]
– If no arguments are supplied the length of the array is 0
• In C++ the main method takes two optional
arguments, an int and a char*
– argc is always at least 1
– First member of argv is always a char* holding the name of
the program
• In Java the main method returns void and is declared
public static
• In C++ the main function returns an int
– Sometimes the OS does something with this
Functions and methods
• In Java every (method) belongs to a
class
• In C++ functions (methods) may belong
to a class or functions may be global
Comments
• In Java single line comments are
designated with //
• In C++ single line comments are
designated with //
• In Java multi-line comments are started
with /* and terminated with */
• In C++ multi-line comments are started
with /* and terminated with */
Compilation
• Java is a multi-pass compiler but the
compilation process is the only
translation need from source code to
machine (JVM) code
• C++ is a single-pass compiler but
requires a pre-compiler (with it’s own
language) and a linker to translate
source code to machine (native) code
Modules
• Java uses packages to modularize code
• C++ uses namespace to modularize
code
– You’ve seen this with the std::cout
statement where std is the namespace and
:: is the namespace operator
– We won’t be doing much with namespace
Primitive data types
• Java has byte, char, short, int, long,
double, float, boolean
• C++ has unsigned char, char, short,
unsigned short, int, unsigned int, long,
unsigned long, float, double, long
double, bool, and there maybe more
that I missed (but you get the idea)
Reference types
• In Java all objects (variables of class
type) are references and therefore are
created on the heap
• In C++ objects may be created in main
memory through direct declaration or on
the heap through pointer declaration
Statements
• Statements in Java and C++ are
basically the same
Expressions
• Expressions in Java and C++ are mostly the
same with one glaring (and troublesome)
difference
• In Java a conditional is defined as an
expression that evaluates to true or false
• In C++ a conditional is defined as an
expression that evaluates to non-zero (true)
or zero (false)
• if (x = 1) causes a compiler error in Java
• if (x = 1) always evaluates to true in C++
Operators
• Operators in Java are basically the same as
operators in C++ with the exception of &
• In Java the & and && used in a conditional
affect the way compound conditional
expressions are handles
– & is also used as a bitwise operation
• In C++ & is always a bitwise operation (which
can be used in a conditional expression – see
previous slide)
Strings
• There is no string datatype in the Java
language (String is technically part of
the Java API)
• There is no string datatype in C++ (use
and array of char or the STL std::string,
#include <string>)
Arrays
• In Java arrays must be allocated on the
heap and when creating arrays of
objects each individual object must be
declared on the heap
• In C++ arrays can reside in main
memory (compile time allocation) or on
the heap (runtime allocation via the
heap)
Basic input/output
• Java uses Scanner for standard
(keyboard) input and
System.out.println() (and others) for
standard (monitor) output
• C++ uses std::cin >> variable for
standard (keyboard) input and std::cout
<< variable for standard (monitor)
output
That’s about it (for now)
• Sure, there are other differences but
we’ll deal with them when they come up
Assignment
• 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 100.0 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;