Вы находитесь на странице: 1из 31

Ron DiNapoli, Lecturer

Fall 2010
  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
  One (potential) final project
  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/2010fa

  Book – “C++: How To Program”


  You are required to have access to the book
  Some assignments given from book
  Course Staff
  Ron DiNapoli, Lecturer (rd29@cornell.edu)
  Full time CIT staff, no office in Computer Science dept.
  Will hold office hours in public area (Duffield Atrium?) and/or
virtually over AIM.
  How did I learn C++? How do I teach it???
  Jeff Lebovici, Consultant
  Duties likely limited to grading at first, but may hold office hours.
  No administrative assistant this year.
  S/U (Pass/Fail)
  12 Assignments graded 0-100
  Drop lowest two
  Average the rest -- AAve!
  2 Prelims (25 question short answer)
  Graded 0-100 (P1, P2)
  1 Final Project
  Graded 0-100 (FP)
  Grade Formula:
  (AAve * 0.4) + (P1 * 0.2) + (P2 * 0.2) + (FP * 0.2)
  If formula yields a number 70 or higher, you get an “S”
  Book comes with Visual C++ Express
  Watch Microsoft-isms! (Use “vanilla” C++)
  You may use gcc (g++)
  You may use XCode on a Mac
  You may use Visual Studio
  You may use Eclipse/C++
  I have the least experience with this, there may be problems.
  Any other compilers, please check with me.
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 file named “iostream” which is
found somewhere in the system include directory.
  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.
  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.
  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.
“Hello World” in C++
  Like most typed programming languages, C++ allows for
the creation of variables to hold values.
  Variables are declared by specifying a data type followed by
whitespace, followed by a variable name.

int k; // an integer type, usually 32-bits


char c; // a character type, 8-bit
long l; // a long type, 32-bits
float f; // a floating point number, 32-bits
double 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 values


j = 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.
  Add two numbers:

#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.
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 k
std::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 almost anywhere!

int intVar; // This is a global variable


int 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;
}
  Consider the following legal expressions:

int k,a = 6,b = 3; // You can initialize variables


k = a + b; // simple addition
k = a * b; // multiplication
k = a – b; // subtraction
k = a / b; // division
k = 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 sees
k = 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 boolean expression 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 (condition)

execute a line of code


if (condition)

{

execute multiple

lines of code

}!
  Note the use of the scope delimeters when using multiple
lines of code.
  The “condition” 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

else

execute this other line(s) of code!

  For example:

if (age >= 18)



std::cout << “You can vote!” << std::endl;

else

std::cout << “Sorry, you can’t vote.” << std::endl;
  What are some other relational operators?

a < b! -- True if a is less than b



a > b ! -- True if a is greater than b

a <= b! -- True if a is less than or equal to b

a >= b! -- True if a is greater than or equal to b

a == b! -- True if a is equal to b

a != b ! -- True if a is not equal to b!

  Let’s see some of these in action…


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:: prefix

cout << “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)
  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!

Вам также может понравиться