14
# include <iostream> using namespace std; int Square ( int ); int Cube ( int ); int main () { cout<<“The square of 27 is “<< Square(27) << endl; cout<<“The cube of 27 is “ << Cube(27) << endl; return 0; }

# include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

Embed Size (px)

Citation preview

Page 1: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

# include <iostream>

using namespace std;

int Square ( int );

int Cube ( int );

int main ()

{

cout<<“The square of 27 is “<< Square(27) << endl;

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

return 0;

}

Page 2: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

SYNTAX – defines exactly what combination of letters, numbers and symbols can be used

SEMANTICS – a set of rules that determines the meaning of instructions

METALANGUAGE – a language that is used to write the syntax rules for another language

OLDEST METALANGUAGE – BNF (Backus-Naur Form)

SYNTAX TEMPLATE – a generic example of the c++ construct

Page 3: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

IDENTIFIERS

Identifier – A name associated with a function or data object and used to refer to that function or data object

Rules – a) must start with a letter or an underscore

b) identifiers that begin with underscore have special meaning

Invalid Identifier Explanation

40Hours Identifiers cannot begin with a digit

Get Data Blanks are not allowed

Box-22 hyphen is a math symbol

Cost_in_$ ($) special symbols are not allowed

int reserved word

Page 4: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

Valid Identifier How it is used

MAX_HOURS Maximum normal work hours

OVERTIME Overtime pay rate factor

payRate An employee’s hourly pay rate

hours number of hours an employee worked

wages employee’s weekly wages

empNum An employee’s identification number

CalcPay a function for computing an employee’s wages

Page 5: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

C++ IS CASE SENSATIVE

EmpNum is not the same as empnum

Emp Num is not the same as EmpNum

empNum is not the same as EmpNum

Page 6: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

DATA TYPES

TWO CATEGORIES OF DATA TYPES

1) STANDARD OR BUILT IN – use often so C++ has them predefined

int float char

2) Programmer or user defined data types – see chapter 10.

Page 7: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

HOW DATA IS STORED IN MEMORY

1) Memory is divided into CELLS

2) Each CELL has a unique address

3) This address is a binary number

4) We use identifiers to name cells

5) The compiler translates the identifier into the address in memory

Cell 1101101101 Cell 1101101111 Cell 1101101000

EmpNum EmpPay EmpAddress

Page 8: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

Charchar – consists of one alphanumeric character – a letter, digit or special symbol

char is enclosed is a singe quote ‘8’

‘8’ ≠ 8

‘8’ is an alphanumeric – cannot be used in mathematical equations

8 is a numeric – can be used in mathematical equations

‘ ‘ is a blank char it consists of a ‘ press the space bar ‘

‘A’ < ‘B’ due to a predefined collating sequence but we cannot do math on chars

Page 9: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

StringString – a sequence of characters – word, name, sentence

Strings are enclosed in double quotes.

“I am happy”

Strings must be typed on one line – don’t split.

“I am

Happy” will give you an error.

“” is the empty string or null string – no space between double quotes

Page 10: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

DATA OBJECTS

CONSTANTS – a name of a memory location whose contents are not allowed to change

VARIABLES – a name of a memory location whose contents can change

A 1001 1002

const char EMPCODE int EmpNum

Page 11: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

DECLARATION

Declaration – a statement that associates an identifier with a data object, a function or a data type so that the programmer can refer to the item by name

int EmpNum;

int EmpPay;

const char EMPCODE = ‘A’;

int Square( int);

Page 12: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

int EmpNum int EmpPay string EmpAddress

Variables – a location in memory, referenced by an identifier, that contains a data value that can be changed

Declaring a variable – specify data type and name;

int EmpNum;

int EmpPay;

string EmpAddress;

OR

int EmpNum, EmpPay;

string EmpAddress;

Page 13: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

‘A’

const char EMPCODE

Constants – a location in memory, referenced by an identifier, that contains a data value that CANNOT be changed

Declaring a constant – specify constant, data type, name and data value

const char EMPCODE = ‘A’;

Page 14: # include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout

LITERAL VS CONSTANT

LITERAL – any constant value written in a program

cout<<“I am a c++ programmer”;

NAMED CONSTANT

const char EMPCODE = LITERAL VALUE;