Presentation is loading. Please wait.

Presentation is loading. Please wait.

M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.

Similar presentations


Presentation on theme: "M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants."— Presentation transcript:

1 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants. u Operators, expressions and statements. u Simple input and output. u The structure of a C++ program. u Topics for further reading. u Practical Exercises.

2 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 2 Data types u Integer u Floating point u Character

3 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 3 Integer data types u Whole numbers u Keyword is int u Requires 2 bytes of RAM u Range of an integer: -32,768 to 32,767 u int can be modified by: u unsigned int ( 0 to 65,535 ) u long int ( -2,147,483,648 to 2,147,483,647 ) u unsigned long int ( 0 to 4,294,967,295 )

4 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 4 Floating point data types u Numbers having a decimal part (e.g. 12.34) u Keyword is float u Requires 4 bytes of RAM u float can be replaced by: u double (double precision floating point data) u long double (long double precision fp data)

5 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 5 Floating point data ranges Data TypeNegative rangeZeroPositive range float-3.4E38 to -3.4E-380 3.4E-38 to 3.4E38 double-1.7E308 to -1.7E-308 0 1.7E-308 to 1.7E308 long double -3.4E4932 to -3.4E-4932 0 3.4E-4932 to 3.4E4932

6 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 6 Character data types u Used to hold ASCII codes of characters u Keyword is char u Requires 1 byte of RAM u Range : -128 to 127 u char can be modified by: u unsigned char ( range 0 to 255 )

7 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 7 Variables u Named memory elements. u Used to store data values. u A variable is defined having: u a unique name (the identifier) u a data type (the kind of data held in it)

8 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 8 Variable definitions u Examples int count ; float temperature ; char YesNoResponse ; intz = 10 ; This variable has been initialised to hold the value 10.

9 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 9 Memory Model of variables count temperature YesNoResponse 2 bytes 4 bytes 1 byte R.A.M Z 10 2 bytes

10 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 10 Constants u A named data item that is unchanged u Two methods of defining a constant u Using the compiler directive #define u E.g. #define VAT 17.5 u Defining and initialising a ‘constant variable’ u E.g. const float VAT = 17.5 ;

11 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 11 Operators, Expressions & Statements u Arithmetic Operators Addition+9 + 615 Subtraction-9 - 6 3 Multiplication*9 * 654 Division/9 / 6 1 Remainder%9 % 6 3 u Assignment Operator = This is not the sign for ‘is equal to’. It is used to assign a value to a variable: Eg. count = 0

12 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 12 Operators continued... u Assignment operators cont... +=, -=, *=, /=, %= E.g.x += y means x = x + y ; u Incrementing/decrementing operators E.g.++w ; x++ ; --y ; z-- ; u Logical operators !, >, >=, <, <=, ==, &&, || u Bitwise operators See later slides

13 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 13 Expressions and statements u Consider the following integer variables definitions: int x = 2 ; int y = 4 ; int z ; u Variables x and y have been given initial values. u The variables can now be used with the arithmetic operators to make expressions which give a resultant value, for example:2 * x + 3 * y u A complete line of C++ code is referred to as a statement often comprising of expressions; e.g. z = 2 * x + 3 * y ; u Note how the semicolon is used to end the statement.

14 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 14 Simple input and output u Values can be entered into variables in a number of ways: u By assignment at the time of variable definition: E.g.int x = 2, y = 4, z ; u 2.By assigning the result of an expression E.g.z = 2 * x + 4 * y ; u 3.By reading a value typed in at the keyboard E.g.cin >> x ;

15 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 15 Simple input and output u Outputting information (to the screen) is performed using cout <<, for example: u cout << “Welcome to Bolton Institute” ; u cout << “\nFaculty of Technology” ; u cout << “\nEngineering Division”; and to output a sentence containing the data held in variables... u cout << “\nThe value of x = ” << x << “ and y = ” << y ; u The compiler directive #include must be used when using cout and cin. u \n is the ‘control sequence’ for a n ew line.

16 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 16 The structure of a C++ program // Name: M.Stanhope // Date: 01.10.97 // File : A:\tempcnv1.cpp // Notes: A simple temperature conversion program // Status: Not yet tested #include // Required for cin and cout void main(void)// Start of main function { float degF, degC ;// Declare variables cout << “Temperature conversion program” ; cout << “\n\nPlease enter temperature in degrees F ” ; cin >> degF ; degC = 5.0 / 9.0 * ( degF - 32 ) ; cout << degF << “ degrees F = ” << degC << “degrees C.” ; } // End of main function

17 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 17 Topics for further reading. u Operator precedence. u Casting. u Other useful header files: conio.h, math.h

18 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 18 Further Practice

19 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 19 Further Practice

20 M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 20


Download ppt "M.T.Stanhope Oct. 1999 Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants."

Similar presentations


Ads by Google