Download presentation
Presentation is loading. Please wait.
Published byDarcy Curtis Modified over 9 years ago
1
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
2
Review Unix What is Unix? Windows? Mac OS X? Etc
3
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.
4
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
5
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? 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
6
Review Unix What 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.
7
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.
8
Your first program - Hello, world! // Title: Hello, world! // Author: CSC317 // Description: Prints a greeting to the screen #include using namespace std; int main () { //Say Hello cout << endl; cout << “Hello, world!”; cout << endl; return 0; } File: HelloWorld.cpp
9
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
10
The structure of a simple C++ program #include 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.
11
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”.
12
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 ;
13
Functions Functions 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!
14
Functions Think 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. sqrt 82304 286.887 add 476 55 1031
15
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”
16
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)
17
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.
18
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 hair A 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.
19
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!
20
Alternate way of writing Hello, world! #include using namespace std; int main () { cout << endl << “Hello, world!” << endl; return 0; } File: HelloWorldOneLine.cpp
21
Printing text to the screen More Examples: PrintParagraphs.cpp PrintTabularData.cpp
22
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!”;
23
Hello, world! with Variables #include using namespace std; int main () { string hello = “Hello, world!”; cout << endl << hello << endl; return 0; } File: HelloWorldVar.cpp
24
Data Types Variables 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”
25
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
26
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;
27
Variable assignment – right or wrong? int dayOfWeek = “Monday”; int dayOfWeek = 1; string dayOfWeek = “Monday”; char dayOfWeek = 'M'; char dayOfWeek = “M”; int dayOfWeek = “1”;
28
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
29
Arithmetic Addition + Subtraction - Multiplication * Division /
30
Variables and Arithmetic #include 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
31
Variable Examples BoxVolume.cpp Arithmetic.cpp Multiply.cpp
32
Homework #2 Posted online
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.