Upload
kathleen-powers
View
253
Download
2
Embed Size (px)
Citation preview
# 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;
}
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
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
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
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
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.
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
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
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
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
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);
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;
‘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’;
LITERAL VS CONSTANT
LITERAL – any constant value written in a program
cout<<“I am a c++ programmer”;
NAMED CONSTANT
const char EMPCODE = LITERAL VALUE;