lecture 5: computer languages. programming environments (ide) cos120 software development using c++...

63
Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

Upload: blake-lynch

Post on 23-Dec-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

Lecture 5: Computer Languages.Programming Environments

(IDE)

COS120 Software Development Using C++ AUBG, COS dept

Page 2: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

2

Lecture Contents:

Programming Environments. Integrated Development Environments (IDE)

Microsoft Visual Studio: C++ IDE Introduction to project concept

– Solution and project(s) Sample programs in C++

Page 3: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

3

Previous lecture reminder

Title:

A Tutorial Introduction to C++

Source: Friedman/Koffman, Chapter 02

Have a quick look at next approx 40 slides to refresh your knowledge on previous lecture

Page 4: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

Overview of C++

Chapter 2

Page 5: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

5

2.1 C++ Language Elements

Comments make a program easier to understand

// Used to signify a comment on a single line

/* Text text */ use if comments on multi lines

Don’t embed comments within /* */ comments

Page 6: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

6

Compiler Directives

#include– Compiler directive– Processed at compilation time– Instructs compiler on what you want in the program

#include <iostream>– Adds library files to program– Used with < >– Also “ “ user defined

Page 7: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

7

Compiler Directives

Stream data type– Object that is a stream of characters– Defined in iostream– Entered on the keyboard (cin)– Displayed on monitor (cout)

Page 8: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

8

Declarations

Based on data needs (data identifiers) Each identifier needed must be declared Comma used to separate identifiers cin and cout are standard, undeclared identifiers

– Special elements called streams– cin - input stream– cout - output stream– Included with the iostream, not declared

Page 9: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

9

Executable Statements

cout get outputcout << "\nEnter the fabric size in square meters:";

cin get inputcin >> sizeInSqmeters;

AssignmentsizeInSqyards = metersToYards * sizeInSqmeters;

Page 10: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

10

2.2 Reserved Words and Identifiers

Reserved words have special meanings– Can NOT be used for other purposes (const, float and void are some examples)

Identifiers (variables)– Used to store data by the program (user

defined)– Valid identifiers - letter, letter1, _letter– Invalid identifiers - 1letter, const, hell o

Page 11: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

11

Reserved Words and Identifiers

Special symbols– C++ has rules for special symbols – = * ; { } ( ) // << >>

Page 12: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

12

Upper and Lower Case

C++ - case sensitive language– Compiler differentiates upper & lower case– Identifiers can be either – Be careful though (cost != Cost)

Blank spaces– Use space to make program readable– Use care in placing spaces

Page 13: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

13

2.3 Data Types and Declarations

Predefined data types– int (integer numbers)

• Positive or negative whole numbers• 1000 12 199 100000• INT_MAX - largest int allowed by compiler

– float or double (real numbers)• Positive or negative decimal numbers• 10.5 1.2 100.02 99.88

Page 14: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

14

Data Types and Declarations

Predefined data types– bool (Boolean)

• true• false

– char (Characters)• Represent single characters

Page 15: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

15

Data Types and Declarations

The basic integer type is int– The size of an int depends on the machine

and the compiler• On PC’s it is normally 16 or 32 bits

Other integers types– short: typically uses less bits– long: typically uses more bits

Page 16: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

16

Data Types and Declarations

Floating-point types represent real numbers– Integer part– Fractional part

The number 108.1517 breaks down into the following parts– 108 - integer part– 1517 - fractional part

Page 17: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

17

Data Types and Declarations

C++ provides three floating-point types:

– float– double– long double

Page 18: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

18

Data Types and Declarations

Predefined data types– char (characters)

• Individual character value (letter or number)• Character literal enclosed in single quotes ‘A’

– bool (true / false) Ordinal types

– int bool char– Values can be listed

Page 19: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

19

Data Types and Declarations

Character type char is related to the integer types

Characters are encoded using a scheme where an integer represents a particular character

Page 20: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

20

Data Types and Declarations

ASCII is the dominant encoding scheme– Examples

• ' ' encoded as 32• '+' encoded as 43• 'A' encoded as 65• 'Z' encoded as 90• 'a' encoded as 97 • 'z' encoded as 122

Page 21: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

21

string Class

String object data type– A literal string constant is a sequence of zero or

more characters enclosed in double quotes– "Are you aware?\n"– Individual characters of string are stored in

consecutive memory locations– The null character ('\0') is appended to

strings so that the compiler knows where in memory string ends

Page 22: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

22

string Class

String literal– "A"– "1234"– "Enter the distance:"

Additional data types included in library#include <string>– Various operations on strings

Page 23: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

23

Declarations

Identifiers should be – Short enough to be reasonable to type (single word is

norm)• Standard abbreviations are fine (but only standard

abbreviations)

– Long enough to be understandable• When using multiple word identifiers capitalize the first

letter of each word

Page 24: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

24

Declarations

Examples– char response;– int minelement;– float score;– float temperature;– int i;– int n;– char c;– float x;

Page 25: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

25

Constant Declarations

Types of named constants:– integer – float– char– bool– string objects

Associate meaningful terms– const float PAYRATE = 10.25;

Page 26: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

26

Hello.cpp –full text of program

// FILE: Hello.cpp// DISPLAYS A USER'S NAME#include <iostream>#include <string>using namespace std;int main(){

char letter1, letter2;string lastName;

// Enter letters and print message. cout << "Enter 2 initials and last name: "; cin >> letter1 >> letter2 >> lastName; cout << "Hello " << letter1 << ". " << letter2 << ". " << lastName << "! ";

cout << "We hope you enjoy studying C++." << endl; return 0;}

Page 27: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

27

Hello.cpp

Program output

Enter first 2 initials and last name and press return: EBKoffman

Hello E. B. Koffman! We hope you enjoy studying C++.

Page 28: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

28

2.4 Executable Statements

Memory status– Before and after

Assignments– Form: result = expression;– sizeInSqyards = metersToYards * sizeInSqMeters;– sum = sum + item;

Page 29: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

29

Arithmetic Operators

+ Addition - Subtraction * Multiplication / Division % Modulus

Page 30: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

30

Input / Output Operations

Input– #include <iostream> library– cin >> sizeInSqmeters;

Extracted from cin (input stream) >> Directs input to variable cin associated with keyboard input (stdin) Used with int, float, char, bool and strings

Page 31: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

31

Data Types and cin

Don’t mix types with cinint x;cin >> x;

Keyboard input

16.6

Value placed in x would be 16

Page 32: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

32

Other Characteristics of cin

Leading blanks ignored (floats, int, char, bool and

strings) Char read 1 at a time (1 non blank) Case issues int or float will read until space Strings same as int and float

Page 33: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

33

General Form for cin

Form: cin >> dataVariable;

cin >> age >> firstInitial;

Page 34: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

34

Program Output

Output stream cout << Output operator (insertion operator)

– cout << “my height in inches is: “ << height; Blank lines

– endl or “\n” or ‘\n’

Form: cout << dataVariable;

Page 35: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

35

2.5 General Form of a C++ Program

General program form– Function basic unit (collection of related statements)– A C++ program must contain a main function

void main() { . . . }

– int - function returns integer value– void - function does not return value– main - lower case with ()– { } - Braces define the function body

Page 36: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

36

General Form of a C++ Program

General form of function body parts

–Declaration statements• Variables and constants

–Executable statements• C++ statements

Page 37: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

37

General Form of a C++ Program

General form// File: filename// Program description:#include directivesint main(){

Declarations section

Executable statements section

}

Page 38: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

38

General Form

// Name: Mike Hudock

// Date: March 10, 2012

// Files: file1.cpp

// Changes :

// Program description:

Page 39: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

39

General Form

Use comments throughout code to highlight points of interest

Strange identifiers Function explanations Algorithm definitions

Page 40: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

40

2.6 Arithmetic Expressions

int data type– + - * /, Assignment, input and output on int– % Only used with int

Examples of integer division15 / 3 = 515 / 2 = 70 / 15 = 0

15 / 0 undefined

Page 41: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

41

Modulus and Integer

Used only with integer and yields remainder

Examples of integer modulus7 % 2 = 1

299 % 100 = 99

49 % 5 = 4

15 % 0 undefined

Page 42: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

42

Mixed-type Assignments

Expression evaluated Result stored in the variable on the left side C++ can mix types

float a, b, x;int m, n;a = 10;b = 5;x = m / n;

Page 43: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

43

Expressions With Multiple Operators

Operator precedence tells how to evaluate expressions

Standard precedence order– () Evaluated first, if nested innermost

done first– * / % Evaluated second. If there are several,

then evaluate from left-to-right– + - Evaluate third. If there are several,

then evaluate from left-to-right

Page 44: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

44

Mathematical Formulas in C++

a = bc not valid C++ syntax* Operator a = b * c;

m =

( ) And / m = (y - a) / (x + b);

Page 45: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

45

2.8 Common Programming Errors

Syntax– Programs rarely compile– Something always goes wrong– Systematic solutions

Compiler not descriptive– Look at line number before and after error– Watch missing ; and }

Page 46: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

46

Common Programming Errors

Run-time errors– Illegal operation (divide by 0)

Logic errors– Program functions differently than you expect

Page 47: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

47

IDE

MS Visual Studio

Page 48: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

48

Introduction toMS Visual C++ 2010

When you activate IDE for the first time, it’s time for you to make your choice:

General development settings Or C++ development settings Or …

Page 49: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

49

Intro to MS Visual C++Creating Unmanaged Application

To build a console application that will run without .NET framework, follow these steps:1. Open Visual Studio.2. On the Start Page, click New Project… . If the Start Page isn’t visible, Choose File,

New, Project.3. Select the Visual C++\Win32 from Installed Project Templates on the left and select

Win32 Console Application Project type on the right.4. Enter project name, for example ConsoleApplication1, project location, for

example Q: drive and click OK.5. Click Finish from the wizard that appears.The wizard generates the skeleton of an unmanaged C++ application.

To build the project, follow these steps:6. Choose Build, Build Solution or press F7

To run the project, follow these steps:7. Choose Debug, Start Without Debugging or press Ctrl+F5

Page 50: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

50

Page 51: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

51

Page 52: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

52

Page 53: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

53

Page 54: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

54

Page 55: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

55

Page 56: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

56

Page 57: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

57

Exercise 5.1

Build and run a program: To compute and display the volume of a pool.

Page 58: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

58

Exercise 5.4

Build and run a program: To convert Celsius degrees to Fahrenheit degrees

Fahr = 9./5.*Cel+32

Page 59: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

59

Exercise 5.5

Build and run a program: To convert Fahrenheit degrees to Celsius degrees

Cel = 5./9.*(Fahr-32)

Page 60: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

60

Exercise 5.3

Build and run a program: To add, subtract, multiply and divide two numeric values.

Page 61: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

61

Exercise 5.2

Build and run a program: To convert feet and inches to meters.

Page 62: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

62

Before lecture end

Lecture:Computer Languages.

Programming environments (IDE)

More to read:Friedman/Koffman, Chapter 02

Page 63: Lecture 5: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept

63

Thank You

For

Your Attention!