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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
CMT Programming Software Applications
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2: Introduction to C++.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
COMPUTER SCIENCE I C++ INTRODUCTION
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
A First Program Using C#
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Creating your first C++ program
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Fundamental Programming: Fundamental Programming Introduction to C++
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introduction to Programming Using C An Introduction to Operating Systems.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Topics – Programs Composed of Several Functions – Syntax Templates – Legal C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
2.1 First C Program. First Program Open visual studio, click new file Save as “programName.c” – Program must start with letter and have no spaces – Must.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Chapter 2: Introduction to C++
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
A First Program.
Capitolo 1 – Introduction C++ Programming
Presentation transcript:

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

Review Unix What is Unix? Windows? Mac OS X? Etc

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.

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

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 program nano – Text editor

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

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.

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

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

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.

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

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 ;

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!

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 add

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”

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)

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.

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.

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!

Alternate way of writing Hello, world! #include using namespace std; int main () { cout << endl << “Hello, world!” << endl; return 0; } File: HelloWorldOneLine.cpp

Printing text to the screen More Examples: PrintParagraphs.cpp PrintTabularData.cpp

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!”;

Hello, world! with Variables #include using namespace std; int main () { string hello = “Hello, world!”; cout << endl << hello << endl; return 0; } File: HelloWorldVar.cpp

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”

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

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;

Variable assignment – right or wrong? int dayOfWeek = “Monday”; int dayOfWeek = 1; string dayOfWeek = “Monday”; char dayOfWeek = 'M'; char dayOfWeek = “M”; int dayOfWeek = “1”;

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

Arithmetic Addition + Subtraction - Multiplication * Division /

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

Variable Examples BoxVolume.cpp Arithmetic.cpp Multiply.cpp

Homework #2 Posted online