agenda review unix review algorithms your first program compiling programs what are functions? what...

32
Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types Reading: 1.5-2.6 Homework #2: Program using variables and arithmetic

Upload: darcy-curtis

Post on 12-Jan-2016

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Agenda

Review Unix

Review Algorithms

Your first program

Compiling programs

What are functions?

What is Object Oriented Programming?

Variables

Data Types

Reading: 1.5-2.6

Homework #2: Program using variables and arithmetic

Page 2: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Review Unix

What is Unix?

Windows?

Mac OS X?

Etc

Page 3: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Review Unix

Unix, Linux, Windows, OS X (Apple/Mac), and others are all operating systems.

An operating system is a program in itself, which contains other programs for managing the computer's hardware and common tasks.

The 'common tasks' part is vague because the people who design the OS decide what they 'commonly' need. For example, Windows always contains a GUI (graphical user interface), but Unix does not. In Unix, the GUI is a program that can be optionally installed.

Page 4: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Review Unix – Command Line Programs

Note that Unix commands are programs, just like the ones we will be writing in C++. The commands themselves are the executable.

What do each of these mean?

ls list all files in your current directory

ls -l list all files in your current directory the long way (with full details)

cd change directory; with no argument it takes you to your home directory

~ a short-hand way of referring to your home directory

/ the root directory (top level)

/home/path/to/your/name the full path to your home directory

Page 5: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Review Unix – Command Line ProgramsNote that Unix commands are programs, just like the ones we will be writing in

C++. The commands themselves are the executable.

What do each of these mean?

cp – Copy files

mv – Move file(s) to a directory or to a new file name (rename)

rm – Remove a file

chmod – Change permissions on a file or directory

pine – Command line email program

nano – Text editor

Page 6: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Review UnixWhat is fang?

fang is a UNIX server in the SUNYIT CS Department. This means that it is a powerful computer that can be used by many people at the same time to do specific tasks that it is specially setup for.

A computer that is not a server (a laptop or desktop like you may have at home) is usually only used by one person at a time for a larger variety of tasks (writing documents, playing games, browsing the internet, etc)

fang is primarily used for students to remotely login and access their email, files, and work on programming for intro CS classes (like this one). Other servers on the CS network may: host websites, handle network traffic, host databases, and provide specialized applications for certain CS classes.

Page 7: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Review Unix

What is this lab?

C012 is a lab with desktop computers running a Linux operating system. Students can use these computers for everyday tasks (word processing, internet) and also login to CS servers, like fang.

Page 8: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Your first program - Hello, world!

// Title: Hello, world!// Author: CSC317// Description: Prints a greeting to the screen

#include <iostream>using namespace std;

int main (){ //Say Hello

cout << endl; cout << “Hello, world!”; cout << endl;

return 0;}

File: HelloWorld.cpp

Page 9: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Compiling and Executing

Save your text file (source code) as HelloWorld.cpp

To compile your program, type this at the command line:

g++ HelloWorld.cpp -o HelloWorld.o

This will compile the source code saved in the file HelloWorld.cpp and save the executable to the file HelloWorld.o

If you got 0 errors while compiling, you can execute your program by typing this:

HelloWorld.o

Page 10: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

The structure of a simple C++ program

#include <iostream>Input/Output header file. Header files put the code you commonly use across all

of your programs in a separate location that you can refer to when you need it. This header has the ostream class and istream class in it.

using namespace std;Namespaces are used to separate code. The objects and functions in the

iostream header are in the standard namespace, or “std”. We want our program to use that same namespace so that we do not have to type “std” before everything. There are cases when programmers would not want to use the std namespace, but we won't worry about that.

Page 11: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

The structure of a simple C++ program

int main (){

return 0;}

All C++ programs have a main function. This is a special function that the compiler understands and will execute first.

The curly braces, { and }, are where the main function begins and ends.

The parenthesis () are for holding function parameters. There are none for main, so leave it empty.

int is the return data type for main. The main functions returns a number indicating success or failure. return 0 means “success” and return 1 means “error”.

Page 12: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

The structure of a simple C++ program

// Say Hello

This a comment. It is for programmers only. The compiler ignores it.

cout << “Hello, world!” << endl;

cout a special object that represents the output stream (the computer screen). This line of code prints the string “Hello, world!” followed by an end line character (endl) to the screen.

<< and << are special operators used with streams

All statements end in a semi-colon ;

Page 13: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

FunctionsFunctions are a programming construct that let you encapsulate a

task. This way, you can perform the same task as many times as you like, whenever you like, and with whatever parameters you like...all without having to repeat the same steps each time.

For example, to calculate the square root of 82304, a number of steps have to be typed in code.

Now, if we want to calculate the square root of 7798, should we type out those steps again?

No, we can write a function and call it twice.

Better yet, we could use a function someone else already wrote and call it twice!

Page 14: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

FunctionsThink of a function as a black box. It takes 0 or more inputs and produces a

single output. We call it a black box because you don't need to know all the steps it performs, just what it will ultimately accomplish.

sqrt82304 286.887

add476

55

1031

Page 15: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Functions

Note: It is possible to have a function that doesn't produce any value, at least in the traditional sense. These functions usually do something – modify a file, print something to the screen, etc.

In many languages, functions that don't produce any value are called subroutines.

SaySomething“Hello”“Hello”

Page 16: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Functions

If you did your homework, the Hour of Code, and looked at the code you “wrote”, you may have noticed that it used functions like these:

moveForward()turnRight()

Functions in programming are typically represented with a name followed by parenthesis. The only differences are if the function accepts parameters, you specify those between the parenthesis:

sqrt(82304)

and if the function returns a value, you assign it to a variable:

x = sqrt(82304)

Page 17: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

cout and ostream

Not all objects are so easily related to real-world objects.

In C++, we will often use the object called cout. This object is of type ostream (this means ostream is a class).

ostream is a class for “output streams”. For our purposes, an output stream can be the computer screen or a file.

Instead of the programmer having to create the object of type ostream, like we talked about with our made up Person class, there is already an object called cout in C++ because it is so frequently used.

Page 18: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Objects

Some languages, like C++, are Object Oriented. That means, we can represent objects in a way that makes more sense to us (humans).

We could have a class in our program called Person.

A Person has properties – their name, the color of their hairA Person can also perform functions – walk, talk, listen

If we then want to create a specific person, John Smith, we would create an object of type Person and assign values to the different properties a Person can have.

Page 19: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Documentation

Always put a block of comments at the top of your program with your name and a description of the program

Put a comment with every major component of your program that says, in plain English, what it does.

Comments make your code easier to read for me and for you when you look at it later. Remember, English is much easier to read than code!

Page 20: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Alternate way of writing Hello, world!

#include <iostream>using namespace std;

int main (){ cout << endl << “Hello, world!” << endl;

return 0;}

File: HelloWorldOneLine.cpp

Page 21: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Printing text to the screen

More Examples:

PrintParagraphs.cppPrintTabularData.cpp

Page 22: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Variables and Values

In our program, “Hello, world!” is a string. Anything enclosed in double quotes is a string.

You could also store the value of the string in a variable:

string hello = “Hello, world!”;

Page 23: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Hello, world! with Variables

#include <iostream>using namespace std;

int main (){ string hello = “Hello, world!”; cout << endl << hello << endl;

return 0;}

File: HelloWorldVar.cpp

Page 24: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Data TypesVariables are stored in memory. Different types of variables require

different amounts of memory to store, so you must specify a type. These are some examples that you will use in this class:

Character (char) – A single character enclosed in single quotes like 'A'

Integer (int) – A positive or negative whole number

Double (double) – A signed number with a decimal component

Boolean (bool) – true or false

String (string) – A series of characters enclosed in double quotes like “This is a string”

Page 25: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Variables

int x;

Declares a variable called x of type integer int x = 0;

Declares a variable called x of type integer and assigns it the value 0.

int x, y;

Declares a variable called x of type integer AND declares another variable called y of type integer; Does not assign a value

Page 26: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Variables Give your variables good names Case matters No spaces in variable names Some good naming patterns:

Use a variable name with an uppercase letter for each word. This is called camel case:

string camelCaseVar; Use a variable name that is lowercase and has

underscores between words

string underscore_var;

Page 27: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Variable assignment – right or wrong?

int dayOfWeek = “Monday”;

int dayOfWeek = 1;

string dayOfWeek = “Monday”;

char dayOfWeek = 'M';

char dayOfWeek = “M”;

int dayOfWeek = “1”;

Page 28: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Variable assignment examples

int dayOfWeek = “Monday”; // Wrong

int dayOfWeek = 1; // Right

string dayOfWeek = “Monday”; // Right

char dayOfWeek = 'M'; // Right

char dayOfWeek = “M”; // Wrong

int dayOfWeek = “1”; // Wrong

Page 29: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Arithmetic

Addition +

Subtraction -

Multiplication *

Division /

Page 30: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Variables and Arithmetic

#include <iostream>using namespace std;

int main (){ int x = 2;

int y = 3; int sum = x + y;cout << x << “ + “ << y << “ = “ << sum << endl;return 0;

}

File: Variables.cpp

Page 31: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Variable Examples

BoxVolume.cppArithmetic.cppMultiply.cpp

Page 32: Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types

Homework #2

Posted online