Week1 Lecture Summary for CSC 309
These notes are not intended to be complete. They serve as an outline to the class and as a supplement to the text.
C++ vs Java
Java | C++ |
---|---|
Programs are compiled to byte code then run by an interpreter. | Programs are compiled to machine code. |
Java does not use a preprocessor. | C++ uses a preprocessor. |
Java is strongly typed. | C++ is not strongly typed. |
Methods and variables must be belong to a class. | Functions and variables can be global. |
Arguments to methods are passed by value | Arguments to functions are passed by value or by reference. To pass by reference you have to ask for it. |
Strings are objects | Strings can be objects or character arrays ending with the null terminator character '\0' |
Arrays are objects, dynamically allocated on the heap, keep track of their length and perform bounds checking. | Array are not objects, allocated on the stack, do not know their own length and do not perform bounds checking. |
Dynamic memory is managed by a garbage collector. | Dynamic memory is managed by the programmer. |
Conditionals (while, if-else, for) only accept boolean expressions. | Conditionals accept integer values. 0 is false, any non-integer is true. |
A boolean data type | A bool data type that evaluates to an integer. |
main does not return a value. | main returns an int. |
Single inheritance. | Multiple inheritance. |
Methods (except static methods) are dynamically bound. | Functions have to be tagged as virtual to be dynamically bound |
First Program
#include <iostream> using namespace std; int main(){ cout << "Welcome to C++ << endl; system("PAUSE"); return 0; }
- Before the code can be compiled into machine code it passes through the preprocessor
- The prepocessor performs the directives, commands that begin with the hash symbol
'#'
- The above program has only one prepocessor directive,
#include
- The
#include
directive instructs the preprocessor to paste the contents of the header fileiostream
, into the source code file at the place where the directive occurs. - The angle brackets <> around the header file
iostream
indicates that the file belongs to a C++ standard library not a user defined library. - These libraries come with your development environment. So your environment knows how to locate these files.
iostream
provides us with the facilities to perform I/O (objects, functions, variables).- A namespace is a C++ programming feature that allows us to group together related names.
- The line
using namespace std;
allows use to use features from the standard library. - C++ programs begin execution in a function called
main()
, that returns an integer. - Code belonging to main is placed in braces { }
- To output to the console we use an object named,
cout
- To send data to
cout
we use the insertion or output operator<<
. endl
is called a manipulator. It is used to flush the buffer and put subsequent output on the next line.- The statement
system("PAUSE")
is needed to keep the terminal window displayed after the program executes.
Numeric Data Types
int, short, long, char, bool, float, double and long double
- Integers can be tagged as signed or unsigned
- Type
bool
can be assigned an integer ortrue
orfalse
- 1 byte is used to represent characters
See your text for complete description.
int age = 21; unsigned int round_max = 5000; bool flag = true; // assigns the value 1 to flag const int MAX = 100; long double galaxy = 6700000000000000000000000000000000000000000000;
Conditional Statements
Conditional statements like if-else, while, for etc..
behave very similar to Java conditionals with one major difference. The conditions in C++ can use an integer, 0 means false any non-zero integer is true.
Java | C++ |
---|---|
if(1){ do something; }Won't compile in Java |
if(1){ do something; }Will compile in C++ and execute the body of if. 1 translates to true. |
int data = 0; if(data){ do something; }Won't compile in Java |
int data = 0; if(data){ do something; }Will compile in C++ but 0 is false, so the body of if will not execute. |
int data = 0; if(data = 4){ do something; }Won't compile in Java. The condition has to be a boolean expression |
int data = 0; if(data = 4){ do something; }Will compile in C++. Here is what happens.
|
int data = 0; if(data == 4){ do something; }Compiles in Java. data == 4 is a boolean expression
|
int data = 0; if(data == 4){ do something; }Same as the Java case. |
Average Program
Input: A sequence of integers followed by any non-integer character to signal end of input.
Output: The arithmetic average.
To get input from the console into our programs we use the object cin
defined in header file iostream
Example
int input_value; cin >> input_value;
The symbol >>
is called the extraction or input operator.
The extraction operator will keep reading the stream until it encounters a space, new line or a type of data invalid for the type of variable you are assigning.
To read in an undetermined number of integers we can write:
int inputValue; while(cin >> inputValue){ do something; }
See mean.cpp
The vector
Class
- A vector is a container that holds a sequence of values.
- The vector can grow during program execution to accommodate more values. C++ arrays are static.
- All the objects in the vector have the same data type .
int, double, Point, Employee ...
- The
vector
type is an example of a template class. - A vector is an object, therefore a vector provides methods and operators for interacting with the vector.
- The vector class is part of the Standard Template Library
- The Standard Template Library, or STL, is a collection of classes, algorithms, and iterators.
#include <vector> using namespace std; int main(){ vector<double> temperatures; temperatures.push_back(45.67); // add 45.67 to the end of the vector temperatures.push_back(56.67); int size = temperatures.size(); // returns 2 double first = temperatures[0]; // returns 45.67 temperatures.clear(); // empties the vector. }
See vector1.cpp and median.cpp for more examples.