Introduction to Programming Two popular approaches to program

  • Slides: 16
Download presentation
Introduction to Programming Two popular approaches to program design: • Structured (or Procedural) Programming

Introduction to Programming Two popular approaches to program design: • Structured (or Procedural) Programming • Object Oriented Programming (OOP) Professor John Carelli Kutztown University Computer Science Department

Structured/Procedural Programming • Earlier programming approach • intuitive • task based • Problem is

Structured/Procedural Programming • Earlier programming approach • intuitive • task based • Problem is broken into manageable sub-tasks • Code (software) is written to address and solve each sub-task • Referred to as functions or procedures • Resulting functions are assembled together to solve overall problem • Like a recipe Professor John Carelli Kutztown University Computer Science Department

Object Oriented Programming (OOP) • Newer programming approach • Data based rather than task

Object Oriented Programming (OOP) • Newer programming approach • Data based rather than task based • Abstract objects are created • representing the data elements to be worked with • Each object is self-contained • Storing both data and procedures for manipulating the data • Details hidden from the user – i. e. a “black-box” • Objects are manipulated to solve a given problem • Advantage: “black-box” localizes software changes • Large software projects are more manageable and supportable Professor John Carelli Kutztown University Computer Science Department

C and C++ C programming language: • Written at AT&T’s Bell Laboratories in the

C and C++ C programming language: • Written at AT&T’s Bell Laboratories in the 1960’s • Supports structured/procedural programming C++ programming language: • Introduced by Bjarne Stroustrup of Bell Labs in mid-1980’s • An extension of C that supports object-oriented programming • Original C functionality remains available under C++ This course will use C++, but will focus on structured programming • For convenience, it will use some objects developed for C++ Professor John Carelli Kutztown University Computer Science Department

C++ Language Elements • Compiler directives • Function main • Declaration statements • Executable

C++ Language Elements • Compiler directives • Function main • Declaration statements • Executable statements /* miles. cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; • Example: miles. cpp // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; } Professor John Carelli return 0; Kutztown University Computer Science Department

Comments • // symbols indicate a line comment apply to just the rest of

Comments • // symbols indicate a line comment apply to just the rest of the line • Block comments start with /* and end with */ - apply to as many lines as you like • Used to describe the code in English or provide non-code information • E. g. to include the name of the program or the author’s name /* miles. cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; } Professor John Carelli return 0; Kutztown University Computer Science Department

#include • Compiler directive • Includes previously written code from a library into your

#include • Compiler directive • Includes previously written code from a library into your program • Libraries allow for code reuse /* miles. cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; • E. g. • #include <iostream> • has operators for performing input and output within the program • The compiler knows where to locate libraries enclosed in <> • iostream will be needed in our programs in order to use cin and cout, as shown … Professor John Carelli // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; } return 0; Kutztown University Computer Science Department

using namespace std; • Indicates to compiler that this program uses objects defined by

using namespace std; • Indicates to compiler that this program uses objects defined by a namespace called std. • namespace: i. e. a section of the iostream library /* miles. cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers • Ends with a semicolon • Follows #include directives in the code // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; } Professor John Carelli return 0; Kutztown University Computer Science Department

Function main() /* miles. cpp - convert distance in miles to kilometers. */ •

Function main() /* miles. cpp - convert distance in miles to kilometers. */ • Exactly one main function per program #include <iostream> // class for stream input/output using namespace std; // use the standard namespace • A function is a collection of related statements that perform a specific set of tasks • int indicates the return type of the function • ( ) any input to the function would normally go here • empty parenthesis indicate that no special information is being passed to the function by the operating system Professor John Carelli int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } // end of main function Kutztown University Computer Science Department

Types of Statements • All C++ statements end with “; ” • A single

Types of Statements • All C++ statements end with “; ” • A single statement may span multiple lines • Two main types of statements • Declaration statements • describe the data the function needs: const float KM_PER_MILE = 1. 609; /* miles. cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; float miles, kms; (note: this is all one line!) // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; • Executable statements • specify actions the program will take cout << “Enter the distance in miles: “; cin >> miles; Professor John Carelli // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; } return 0; Kutztown University Computer Science Department

Reserved Words (Keywords) • Have special meaning in C++ • Cannot be used for

Reserved Words (Keywords) • Have special meaning in C++ • Cannot be used for other purposes /* miles. cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; } Professor John Carelli return 0; Kutztown University Computer Science Department

Identifiers (a. k. a. variables) • Names for data and objects (variables) to be

Identifiers (a. k. a. variables) • Names for data and objects (variables) to be manipulated by the program • Must begin with a letter or underscore (not recommended) • Consist only of letters, digits, and underscores • Cannot be reserved word • Upper and lower case significant /* miles. cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() { // start of main function const float km_per_mile = 1. 609; // 1. 609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i. e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; • miles. cpp cin, cout, kms, km_per_mile, miles, std, endl Professor John Carelli // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; } return 0; Kutztown University Computer Science Department

“Compiling” • A generic term referring to the process of converting the “source code”

“Compiling” • A generic term referring to the process of converting the “source code” program into an “executable” that can be run on the computer • A compiler is a program that does that conversion • Typically, there are different compilers for different computer types and operating systems • This course will use the GNU C++ compiler running on Unix • GNU – an organization that provides “open source” software (free) • The compiler is called g++ Professor John Carelli Kutztown University Computer Science Department

Running the compiler Program development: 1. Write the program Create Source Code: miles. cpp

Running the compiler Program development: 1. Write the program Create Source Code: miles. cpp Example program run: 2. “compile” the program g++ -c miles. cpp generates an Object File: miles. o 3. “link” Object Files, as needed g++ miles. o generates an Executable File: a. out $. /a. out Enter the distance in miles: 2 The distance in miles is: 2 The distance in kilometers is: 3. 218 4. “load” (run) the Executable File. /a. out Professor John Carelli Kutztown University Computer Science Department

More on compiling Compiling vs. linking • Compiling produces machine code object files •

More on compiling Compiling vs. linking • Compiling produces machine code object files • Linking combines multiple object files into one executable • object files may come from a pre-compiled library or from multiple files written for one program For simple programs (one file), compiling and linking can be done in one step: $ g++ miles. cpp produces: a. out (miles. o is not produced) Sometimes the combined process is referred to as “compiling” (…don’t let it confuse you) Note: In this course, we will be writing one file programs and compiling in one step Professor John Carelli Kutztown University Computer Science Department

Compiling/Linking – final notes • Compiling is actually a 2 step process • Pre-compiler:

Compiling/Linking – final notes • Compiling is actually a 2 step process • Pre-compiler: resolves ‘#’ statements, like #include • More on this later…. • Compiler: converts the pre-compiler result to a machine code object file • Linker allows for the executable to be given a user-supplied name • By default the executable is a. out • Use ‘-o’ option to change the name g++ -o miles. cpp Produces an executable named: miles To run it: . /miles Professor John Carelli Kutztown University Computer Science Department