19
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo- Cordero August 27, 2008

Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

Embed Size (px)

Citation preview

Page 1: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

Department of Electrical and Computer Engineering

Introduction to C++: Primitive Data Types, Libraries and

Operations

By Hector M Lugo-CorderoAugust 27, 2008

Page 2: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

2

History

• In the beginning no programming languages existed.– 5 + 2 :- 000101010010

• Programming Languages appeared:– A language that both computers and humans

understand.– Able to write instructions to make the

computer make some actions.

Page 3: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

3

History (cont.)

• Low level programming (Assembly)– Reads and write directly on the hardware.– Each line is equivalent to a binary code.

• 5 + 2 :- 000101010010 add 5, 2

– Needs always to have registers in the instrucion• mov ax, 5• add ax, 2

• High level programming– Tries to get closer to the human language.– Each line is equivalent to one or more binary

codes.• int x = 5 + 2;

Page 4: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

4

History (cont.)

• High Level Programming Paradigms– Functional

• Breaks problems into sub problems• Each problem is solved by a

function/method/subroutine• Examples:

– FORTRAN (Formula Translator): first language that allowed algebraic notation

– Cobol– C

Page 5: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

5

History (cont.)

• High Level Programming Paradigms (cont.)– Object Oriented

• The problem can be written in objects• Each object (noun) has its attributes (adjectives) and

methods (verbs) (e.g. Person)• Examples:

– C++ (compiler translates into machine language (binary code))

– Java (runs with JVM)– C# (runs with .net framework)

• Note that all these are imperative languages (use of variables to control program flow)

Page 6: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

6

Outline

• Variables and Data types– Numeric– Char– Boolean

• Modifiers• Libraries

– iostream– cstdio– cstdlib– cmath– cstring

• Operations– Arithmetic– Logic

Page 7: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

7

Variables and Data Types

• Variable: just like in math a variable is something that stores information and changes its value if desired.– Type – Name– Memory Allocation (will be covered later)– e.g. int x = 5;

Page 8: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

8

Variables and Data Types (cont.)

• Name– Can not start with numbers– Can not contain white spaces– Typical notation

• Constant values are stored with names in capital• Two words may be written with _ or now with the

Java notation (first letter is capital)• Names should describe the function of the variable

– e.g. int age = 25; //The age of a person is 25

• You may start a comment with //comment or /* comment */ for multiple lines

• All instructions end with a ; (for now )

Page 9: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

9

Variables and Data Types (cont.)

• Types– Numeric

• short/long/int: represents integer numbers• float/double: represents real numbers

– char: stores a single alphanumeric character• Has an ASCII value• e.g. char letter = ‘A’; • int ascii = letter; //ascii = 65

– bool: can be either true or false. Use for making decisions (C must import #include<stdbool.h>)

• In C/C++ every non-zero number is taken as true while zero is taken as a false.

– void: has no value (will be used later in pointers)

Page 10: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

10

Variables and Data Types (cont.)

• Types– string: contains a character sequence also

known in C as char* (char pointer). • Need to import #include<string>

– time: represents time values• Need to import #include<ctime>

– FILE*: data type use to read/write files (will be discussed later)

Page 11: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

11

Modifiers

• unsigned: uses numbers with no sign (positive values only). As a consequence the range is extended.

• const: used to defined constant variables• extern:

– Keyword that tells the compiler that the variable or function is defined in another file.

– Also may be use to specify the compiler to be used.

Page 12: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

12

Libraries

• iostream– cout– cin– cerr– clog

• cstdio– Contains file handling functions– We will discuss this in full detail later

Page 13: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

13

Libraries (cont.)

• cstdlib– Contains memory management– System calls– Random functions (srand/rand)– Conversion funcions

• atof: converts a string to double• atoi: converts a string to integer• atol: converts a string to long

Page 14: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

14

Libraries (cont.)

• cmath– Contains math functions

• cos, sin, tan• acos, asin, atan• exp, log, log10• pow, sqrt• ceil, floor, fabs

– We can define PI and other constants with• #define PI 3.14159 …• #define EXP 2.718281 …

Page 15: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

15

Libraries (cont.)

• cstring (contains string related functions)– strlen: returns the length of a string

• int length = strlen(name);

– strtok: splits a string into tokens• char str[] ="- This, a sample string."; • char * pch; • printf ("Splitting string \"%s\" into tokens:\n",str); • pch = strtok (str," ,.-"); • while (pch != NULL) {

– printf ("%s\n",pch); – pch = strtok (NULL, " ,.-");

• }

Page 16: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

16

Operations

• Add: x + 3• Substract: y – 5• Mutiplication: x * 8.3• Division:

– If operands are integers• ½ = 0 with reminder of 1

– Remainder can be obtained 1%2

– If operands are real numbers• 1.0/2.0 = 0.5

Page 17: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

17

Operations

• Shifts:– Left: z << 2;– Right: z >> 3;

• Logical And: x & 5• Logical Or: y | 2• Logical Not: t = ~t• Logical Xor: x ^ y

Page 18: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

18

Operations

• Unary increment: ++a, a++• Unary decrement: --a, a—• x += 3 x = x + 3• x -= 3 x = x - 3• x *= 3 x = x * 3• x /= 3 x = x / 3• x <<= 3 x = x << 3• x >>= 3 x = x >> 3

Page 19: Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,

The Department of Electrical and Computer Engineering

19

Questions?