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

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

Numeric Data Types

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.

JavaC++
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.
  • First, data is assigned the value 0.
  • In the condition to the if, data is assigned the value 4.
  • The operator = returns data to the condition.
  • The if statement evaluates data, which has value 4.
  • Integer 4 means true in C++, so the body of the if executes.
Note: This is a bug.
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

#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.