+ All Categories
Home > Documents > 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to...

1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to...

Date post: 01-Jan-2016
Category:
Upload: dwight-harvey-strickland
View: 215 times
Download: 0 times
Share this document with a friend
43
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation Output Statements C++ Program Comments
Transcript
Page 1: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

1

Programs Composed of Several FunctionsSyntax TemplatesLegal C++ IdentifiersAssigning Values to VariablesDeclaring Named ConstantsString ConcatenationOutput StatementsC++ Program Comments

Page 2: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

2

there must be a function called main( )execution always begins with the first statement in function main( )any other functions in your program are subprograms and are not executed until they are called

Page 3: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

3

Program With Several Functions

main function

square function

cube function

Page 4: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

4

Program With Three Functions

#include <iostream>

int Square( int ); // declares these twoint Cube( int ); // value-returning functions

using namespace std ;

int main( ){ cout << “The square of 27 is “

<< Square(27) << endl; // function call

cout << “The cube of 27 is “ << Cube(27) << endl; // function call

return 0;}

Page 5: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

5

Rest of Program

int Square( int n ){

return n * n;}

int Cube( int n ){

return n * n * n;}

Page 6: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

6

The square of 27 is 729The cube of 27 is 19683

Page 7: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

7

int main ( )

{

return 0;

}

type of returned value name of function

Page 8: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

int main ( )

type of returned valuename of function says no parameters

Page 9: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

9

a block is a sequence of zero or more statements enclosed by a pair of curly braces { }

SYNTAX

{

Statement (optional)...

}

Page 10: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

int main ( ) heading{

body block

return 0;}

Page 11: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

11

An identifier is the name used for a data object (a variable or a constant), or for a function, in a C++ program.C++ is a case-sensitive language.

using meaningful identifiers is a good programming practice

Page 12: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

12

an identifier must start with a letter or underscore, and be followed by zero or more letters

(A-Z, a-z), digits (0-9), or underscores

VALID age_of_dog taxRateY2KPrintHeading ageOfHorse

NOT VALID (Why?)age# 2000TaxRate Age-Of-Cat

Page 13: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

13

some C++ compilers recognize only the first 32 characters of an identifier as significant

then these identifiers are considered the same:

age_Of_This_Old_Rhinoceros_At_My_Zooage_Of_This_Old_Rhinoceros_At_My_Safari

consider these:

Age_Of_This_Old_Rhinoceros_At_My_Zooage_Of_This_Old_Rhinoceros_At_My_Zoo

Page 14: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

14

C++ Data Types

structured

array struct union class

address

pointer reference

simple

integral enum

char short int long bool

floating

float double long double

Page 15: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

15

C++ Simple Data Types

simple types

integral floating

char short int long bool enum float double long double

unsigned

Page 16: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

16

Integral Typesrepresent whole numbers and their negativesdeclared as int, short, or long

Floating Typesrepresent real numbers with a decimal pointdeclared as float, or double

Character Typesrepresent single charactersdeclared as char

Page 17: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

17

int sample values 4578 -4578 0

float sample values95.274 95. .265

char sample values ‘B’ ‘d’ ‘4’ ‘?’ ‘*’

Page 18: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

18

A variable is a location in memory which we can refer to by an identifier, and in which a data value that can be changed is stored.

declaring a variable means specifying both its name and its data type

Page 19: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

19

A declaration tells the compiler toallocate enough memory to hold a value of this data type, and to associate the identifier with this location.

int ageOfDog;float taxRateY2K;char middleInitial;

4 bytes for taxRateY2K 1 byte for middleInitial

Page 20: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

20

a string is a sequence of characters enclosed in double quotes

string sample values “Hello” “Year 2000” “1234”

the empty string (null string) contains no characters and is written as “”

Page 21: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

21

string is not a built-in (standard) typeit is a programmer-defined data typeit is provided in the C++ standard library

string operations includecomparing 2 string valuessearching a string for a particular characterjoining one string to another

Page 22: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

22

A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed is stored.

VALID CONSTANT DECLARATIONS

const string STARS = “****” ;

const float NORMAL_TEMP = 98.6 ; const char BLANK = ‘ ’ ;

const int VOTING_AGE = 18 ; const float MAX_HOURS = 40.0 ;

Page 23: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

23

Giving a Value to a VariableYou can assign (give) a value to a variable by using the assignment operator =

VARIABLE DECLARATIONS

string firstName ;char middleInitial ;char letter ;int ageOfDog;

VALID ASSIGNMENT STATEMENTS

firstName = “Fido” ;middleInitial = ‘X’ ;letter = middleInitial ;ageOfDog = 12 ;

Page 24: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

24

An expression is a valid arrangement of variables, constants, and operators.

in C++ each expression can be evaluated to compute a value of a given type

the value of the expression 9 + 5 is 14

Page 25: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

Variable = Expression

First, Expression on right is evaluated.Then the resulting value is stored in the memory

location of Variable on left.

NOTE: An automatic type coercion occurs after evaluation but before the value is stored if the types differ for Expression and Variable

Page 26: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

26

concatenation is a binary operation that uses the + operator

at least one of the operands must be a string variable or named constant--the other operand can be string type or char type

Page 27: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

27

const string WHEN = “Tomorrow” ; const char EXCLAMATION = ‘!’ ; string message1 ;

string message2 ;

message1 = “Yesterday “ ; message2 = “and “ ; message1 = message1 + message2 +

WHEN + EXCLAMATION ;

Page 28: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

28

variable cout is predefined to denote an output stream that goes to the standard output device (display screen)

the insertion operator << called “put to” takes 2 operands

the left operand is a stream expression, such as cout. The right operand is an expression of simple type or a string constant

Page 29: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

29

SYNTAX

These examples yield the same output:

cout << “The answer is “ ;cout << 3 * 4 ;

cout << “The answer is “ << 3 * 4 ;

cout << Expression << Expression . . . ;

Page 30: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

30

No. Before your source program is compiled, it is first examined by the preprocessor to

remove all comments from source codehandle all preprocessor directives--they begin with the # character such as #include <iostream>

tells preprocessor to look in the standard include directory for the header file called iostream and insert its contents into your source code

Page 31: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

Instead, a library provides an output stream

Screenexecutingprogram

ostream

Page 32: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

32

A library has 2 partsInterface (stored in a header file) tells what items are

in the library and how to use them.Implementation (stored in another file) contains the

definitions of the items in the library.

#include <iostream> Refers to the header file for the iostream library

needed for use of cout and endl.

Page 33: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

33

f ( x ) = 5 x - 3

When x = 1, f ( x ) = 2 is the returned value.When x = 4, f ( x ) = 17 is the returned value.Returned value is determined by the function definition

and by the values of any parameters.

Name of function

Parameter of function

Function definition

Page 34: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

34

// ****************************************************** // PrintName program// This program prints a name in two different formats// ******************************************************

#include <iostream> // for cout and endl#include <string> // for data type string

using namespace std;

const string FIRST = “Herman”; // Person’s first nameconst string LAST = “Smith”; // Person’s last nameconst char MIDDLE = ‘G’; // Person’s middle initial

Page 35: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

35

int main( ){ string firstLast; // Name in first-last format string lastFirst; // Name in last-first format

firstLast = FIRST + “ “ + LAST ; cout << “Name in first-last format is “ << endl

<< firstLast << endl;

lastFirst = LAST + “, “ + FIRST + ’ ’ ; cout << “Name in first-last format is “ << endl

<< lastFirst << MIDDLE << ’.’ << endl;

return 0; }

Page 36: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

36

Name in first-last format is Herman Smith

Name in last-first-initial format is Smith, Herman G.

Page 37: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

Chapter 1 2301373: Introduction 37

What is a Compiler?What is a Compiler?A compiler is a computer program that translates a program in a source language into an equivalent program in a target language.

A source program/code is a program/code written in the source language, which is usually a high-level language.

A target program/code is a program/code written in the target language, which often is a machine language or an intermediate code.

compilerSource program

Target program

Error message

Page 38: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

Slide 38

// Comment line// The following line is required#include <stdio.h>#include <math.h> //if math library is used// define macro constants#define PI 3.14159#define MY_MESSAGE “Good Morning!”

Page 39: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

cinStandard input streamNormally keyboard

coutStandard output streamNormally computer screen

cerrStandard error streamDisplay error messages

Page 40: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

CommentsDocument programsImprove program readabilityIgnored by compilerSingle-line comment

Begin with //Preprocessor directives

Processed by preprocessor before compilingBegin with #

Page 41: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

Slide 41

#include <math.h> //if math library is used// The following file yourdeclaration.h // is in your current directory#include “yourdeclaration.h”

// define macro constants#define PI 3.14159#define MY_MESSAGE “Good Morning!”

Page 42: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

Slide 42

// declare named global constantsconst number = 4;

// declare typestypedef char myCharacter;

// declare global variablesint a;char b=‘a’, c=‘\n’;myCharacter e;long d = 14000L; // this means long int (default int)float x=-2.5F;double y=2.5;

Page 43: 1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.

Slide 43

// declare function prototypeint sub1(int);

// define main functionint main (void){ // declare local variables int lb, lc;

lc = 2; // call function lb = sub1(lc); printf(“%d\n”,lb);

return 0; // no error exit}


Recommended