lecture 1

13
8/25/2014 1 Craig Frey, Lecturer Fall 2014 2 credit, S/U course Meets all semester, Tu-Th from 12:20pm-1:10pm Main goal is to teach as much of the C++ language as is possible in the time given Emphasis on learning by doing 12 Assignments 2 Prelims

Upload: sdggsdrgfths-sadfdsf

Post on 20-Jul-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

8/25/2014

1

Craig Frey, LecturerFall 2014

2 credit, S/U course Meets all semester, Tu-Th from 12:20pm-1:10pm Main goal is to teach as much of the C++ language

as is possible in the time given Emphasis on learning by doing 12 Assignments 2 Prelims

8/25/2014

2

CMS (Course Management System) Home grown system used in Computer Science

Department All assignments distributed and collected electronically http://cms.csuglab.cornell.edu

Course Web Site Basic information http://courses.cs.cornell.edu/cs2024/2014fa

Book – “C++: How To Program”

You are required to have access to the book Some assignments given from book

Piazza Question and answer forum Ask here first! Be polite. Do answer questions! https://piazza.com/cornell/fall2014/cs2024/home

Course Staff Craig Frey, Lecturer ([email protected]) Worked full time at Cornell for 7 years. Previously taught this class, Intro to Python, and Object

Oriented Programming (Java) Will hold office hours in 347 Gates Hall Why C++???

CS Administrative Assistant is Maria Witlox.

8/25/2014

3

S/U (Pass/Fail) 12 Assignments graded 0-100

Drop lowest two Average the rest -- AsAvg

2 Prelims (25 question short answer) Graded 0-100 (P1, P2)

Grade Formula: (AsAvg * 0.5) + (P1 * 0.25) + (P2 * 0.25) If formula yields a number 70 or higher, you get an “S”

Microsoft Visual Studio VS Express 2013 version is free Watch Microsoft-isms! (Use “vanilla” C++) I’ll mostly use this one in class

You may use gcc (g++, GNU Compiler Collection) Cygwin MinGW

You may use Xcode on a Mac You may use Eclipse/C++ (if you already do) Any other compilers, please check with me.

8/25/2014

4

Lecture #1: Introduction to C++

Consider our first program

#include <iostream>

int main(int argc, char *argv[]){std::cout << “Hello World!” << std::endl;return 0;

}

Let’s look at what this does, line by line

The following line…

#include <iostream>

Tells the compiler that we would like to load definitions from a header file named “iostream”which is found somewhere in the system includedirectory. The system include directory is usually already known

to the compiler and you do not need to worry about where it actually is.

We’ll need this for our use of cout and endl.

8/25/2014

5

The following line…

int main(int argc, char *argv[])

Is used to define the main entry point into our program. Every C and C++ program needs a main() function. The int before the main() function is the return type. The argc and argv are arguments (we’ll describe what

they do in a later lecture)

The following line…

{

Is called a left curly brace or, more appropriately, a scope delimeter. For now, just know that it is used to mark the

beginning of our function definition.

8/25/2014

6

The following line…

std::cout << “Hello World!” << std::endl;

Causes the text “Hello World” to be printed. std:: identifies a namespace (like a Java package) cout is a stream that represents the screen << is an operator that directs content from the right to

the stream to the left. endl is a special keyword that represents a newline. << can be “chained”, evaluates left to right.

The following line…

return 0;

Specifies the return value of our main function. In some cases, used by operating system to determine

whether or not the program returned successfully or with some error condition.

Pseudo-standard is that applications returning a 0 value have “succeeded”

The following line…

}

Is called a right curly brace or, more appropriately, a scope delimeter. For now, just know that it is used to mark the end of

our function definition.

8/25/2014

7

“Hello World” in C++

#include <iostream>// don’t use the older .h version#include <iostream.h>

int main (){

std::cout << ‘Hi’ << std::endl; return 0;

}

#include <iostream>// explicit string works on all compilers

#include <string>

int main (){

std::string myName; return 0;

}

8/25/2014

8

Like most typed programming languages, C++ allows for the creation of variables to hold values.

Variables are declared by specifying a data typefollowed by whitespace, followed by a variable name.

int k; // an integer type, usually 32-bitschar c; // a character type, 8-bitlong l; // a long type, 32-bits float f; // a floating point number, 32-bitsdouble d;// a bigger floating point, 64-bits

Consider the following program that uses variables to calculate a simple mathematical expression…

Add two numbers:

#include <iostream>

int main(int argc, char *argv[]){

int k,j; // shorthand for declaring 2 variables

k = 5; // use = for assigning valuesj = 6; // store 6 in variable j

std::cout << “k+j=“ << k+j << std::endl;return 0;

}

Note the use of an arithmetic expression when printing out the result.

Keyboard Input:

#include <iostream>

int main(int argc, char *argv[]){

int k;

std::cin >> k; // read value into k

std::cout << “k=“ << k << std::endl;return 0;

}

std::cin is an input stream that represents the keyboard.

8/25/2014

9

Putting it Together…Read in two integers and add them!

A variable must be declared or defined before it may be used in a program.

We’ll cover the difference between declarations and definitions later.

For now, just know that the compiler must know what type a variable is before it can compile it in any expression:int main(int argc, char *argv[]){int k;

std::cin >> k; // Compiler knows about kstd::cin >> j; // We haven’t seen j before, ERROR!int j; // No, we can’t declare a variable after

// we’ve attempted to use it!return 0;

}

Variables can be declared (and defined) almost anywhere!

int intVar; // This is a global variableint main(int argc, char *argv[]){

int k;

std::cin >> k; // Compiler knows about k

int j; // j is declared in the middle of // the main() function

std::cin >> j;

return 0;}

8/25/2014

10

Consider the following legal expressions:

int k,a = 6,b = 3; // You can initialize variablesk = a + b; // simple additionk = a * b; // multiplicationk = a – b; // subtractionk = a / b; // divisionk = a % b; // modulus

What about compound expressions?k = a + b * c;k = a / b + c;

How are these evaluated?

Every operator in C++ has a precedence associated with it.

Precedence is an integer value that allows the compiler to decide which operators to evaluate first in a compound expression.

So, when the compiler seesk = a + b * c;

It knows to evaluate b*c before evaluating a+b, like this:k = a + (b * c)

Because of this, we say that the * operator binds tighter than the + operator.

See Figure 2.10 on pg 52 of the book for more info.

A conditional statement is one where a booleanexpression is evaluated and some amount of code is executed if the expression evaluates to “true”.

The simplest form of this is the “if” statement

if (expression)execute a line of code

if (expression){

execute multiplelines of code

}

Note the use of the scope delimiters when using multiple lines of code.

8/25/2014

11

The “expression” is, again, a boolean expression. In C++, a boolean expression is simply an integer

expression where the value “0” represents “false” and any other value represents “true”

Consider the following:

int x = 5;if (x > 0)

std::cout << “x is greater than 0” << std::endl;

The expression between the parenthesis contains a relational operator (>). It evaluates to a non –zero value if the value to the left is greater than the value to the right.

The full form of the “if” statement allows for an “else clause” Code executed if the condition is false.

if (condition)execute this line(s) of code

elseexecute this other line(s) of code

For example:

if (age >= 18)std::cout << “You can vote!” << std::endl;

elsestd::cout << “Sorry, you can’t vote.” << std::endl;

What are some other relational operators?

a < b -- True if a is less than ba > b -- True if a is greater than ba <= b -- True if a is less than or equal to ba >= b -- True if a is greater than or equal to ba == b -- True if a is equal to ba != b -- True if a is not equal to b

Let’s see some of these in action…

8/25/2014

12

Relational Operators and Conditionals

You may have noticed that we’ve used the std::prefix in many places…

int main(int argc, char *argv[]){

std::cout << “Hello World” << std::endl;}

Consider this alternative…

using std::cout;using std::endl;

int main(int argc, char *argv[]){

// No longer need to use the std:: prefixcout << “Hello World” << endl;

}

The using statement is very similar to the Java import statement.

It tells the compiler that if a symbol is encountered that is not otherwise defined to see if it is found in a particular system (or other) library.

This allows us to have shorter, simpler code But also can lead to some confusion if—as

someone reading the code—you expect such symbols defined elsewhere in the code (as opposed to a library)

8/25/2014

13

Today’s lecture was a very basic review of some standard programming concepts.

Assignment #1 is posted Not a difficult assignment, but designed to give you

practice using your compiler of choice and the CMS submission system.

We’ll dive right into classes and objects next time!