C++ - Chapter 2 - C++ Programming Basics

Embed Size (px)

Citation preview

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    1/62

    OBJECT-ORIENTED

    PROGRAMMING IN C++

     Ahmed Mohamed Sayed AhmedComputer lecturer

    Cisco Certified Academy Instructor

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    2/62

    Contents

    Chapter Content

    Chapter 1 The Big Picture

    Chapter 2 C++ Programming Basics

    Chapter 3 Loops and Decisions

    Chapter 4 Structures

    Chapter 5 Functions

    Chapter 6 Objects and Classes

    Chapter 7 Arrays and StringsChapter 8 Operator Overloading

    Chapter 9 Inheritance

    Chapter 10 Pointers

    Chapter 11 Virtual Functions and Polymorphism

    2

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    3/62

    The Chapter Contents

    1. Getting Started

    2. Basic Program Construction

    3. Output Using cout

    4. Directives

    5. Comments

    6. Integer Variables

    7. Character Variables

    8. Input with cin 

    3

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    9. Floating Point Types

    10.Type bool  

    11.The setw  Manipulator

    12.Variable Type Summary 

    13.Type Conversion 

    14.Arithmetic Operators

    15.Library Functions

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    4/62

    1. Getting Started

    We will use a compiler from Visual Studio 6.0 program.

    We can download the Visual Studio 6.0 program, for free,from these links:

    http://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.html 

    (Free Register required)

    https://rapidshare.com/files/3614028324/Visual%20Studio%206.0.zip 

    (Register not required)

    Open the program by choose:

    4

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    http://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttps://rapidshare.com/files/3614028324/Visual%20Studio%206.0.ziphttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.htmlhttp://www.4shared.com/zip/jxr5H2F-/Visual_Studio_60.html

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    5/62

    1. Getting Started5

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    The Program Interface:

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    6/62

    1. Getting Started

    Open new Project by choose: File  New

    6

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    1

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    7/62

    1. Getting Started

    From Projects tab, choose “Win32 Console Application”. 

    7

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    2

    3

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    8/62

    1. Getting Started

    In Project name box, write a name for your project, for example“My Project”. Then press OK button.

    8

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    4

    5

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    9/62

    1. Getting Started

    Press on Finish button to continue… 

    9

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    6

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    10/62

    1. Getting Started

    Press on OK button to finish creating the project.

    10

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    7

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    11/62

    1. Getting Started

    Open new File by choose: File  New

    11

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    8

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    12/62

    1. Getting Started

    From Files tab, choose “C++ Source File”. 

    12

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    9

    10

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    13/62

    1. Getting Started13

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    In File name box, write a name for your source code file, for example“FIRST”. Then press OK button.

    11

    12

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    14/62

    1. Getting Started

    The compiler is ready now to write your source code.

    14

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    15/62

    1. Getting Started15

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    Compilers take source code and transform it intoexecutable files, which your computer can run as it doesother programs.

    Source files are text files (extension .CPP) that we willlearn how to write it through our course.

    Executable files have the .EXE extension, and can beexecuted either from within your compiler, or directlyalone.

    A DSW file contains the text of a program beingdeveloped in the Microsoft C++ language. You will usethe file with DSW (Microsoft Developer StudioWorkspace) extension to open the project you created.

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    16/62

    2. Basic Program Construction

    Write the following code that will simply prints a

    sentence on the screen.

    16

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    #include

    using namespace std;int main( ){

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    17/62

    2. Basic Program Construction

    There are two steps to complete building our first

    project:

    1. Press F7, or from Build menu choose Build FIRST.exe tofind out errors in our code.

    2. Press Ctrl+F5, or from Build menu choose ExecuteFIRST.exe to build and execute our project. 

    17

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    18/62

    1. Getting Started18

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    main function

    The parentheses following the word main are the

    distinguishing feature of a function. Without the

    parentheses the compiler would think that main refers toa variable.

    We’ll put parentheses following the function name. Later

    on we’ll see that the parentheses aren’t always empty. 

    They’re used to hold function arguments: values passed

    from the calling program to the function.

    The word int preceding the function name indicates that

    this particular function has a return value of type int.

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    19/62

    1. Getting Started19

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    Braces and the Function Body

    The body of a function is surrounded by braces.

    These braces play the same role as the BEGIN and ENDkeywords in some other languages: They surround or delimit

    a block of program statements.Always start with main ( )

    When you run a C++ program, the first statement executedwill be at the beginning of a function called main ( ).

    The program may consist of many functions, classes, andother program elements, but on startup, control always goesto main ( ).

    If there is no function called main ( ) in your program, anerror will be reported when you run the program.

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    20/62

    1. Getting Started20

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    Program Statements

    A semicolon signals the end of the statement. If youleave out the semicolon, the compiler will often(although not always) signal an error.

    The last statement in the function body is return 0; . Thistells main ( ) to return the value 0 to whoever called it,in this case the operating system or compiler.

    In older versions of C++ you could give main ( ) the

    return type of void  and dispense with the return statement, but this is not considered correct in StandardC++.

    You can put several statements on one line, separatedby any number of spaces or tabs.

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    21/62

    2. Basic Program Construction21

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    #includeusing namespace std;int main( ){ cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    22/62

    3. Output Using cout 

    As you have seen, the statement:

    causes the phrase in quotation

    marks to be displayed on thescreen.

    22

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    23/62

    3. Output Using cout 

    As you have seen, the endl  in statement:

    causes the next text output to be displayed on a new line.

    We use it here so that the phrases such as “Press any key tocontinue …” , inserted by some compilers for display after

    the program terminates, will appear on a new line.

    We can use another way to display the same output:

    or

    23

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    24/62

    4. Directives

    The two lines that begin the FIRST program are directives.

    The first is a preprocessor directive, and the second is a usingdirective. They’re not part of the basic C++ language, but

    they’re necessary anyway. 

    The preprocessor directive #include tells the compiler to insert

    another file into your source file. In effect, the #include directive is replaced by the contents of the file indicated.

    The type file usually included by #include is called a header

    file.

    24

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    #includeusing namespace std;

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    25/62

    4. Directives

    In the FIRST example, the preprocessor directive #include 

    tells the compiler to add the source file iostream to the

    FIRST.CPP source file before compiling. iostream is an example of a header file. It’s concerned

    with basic input/output operations, and contains

    declarations that are needed by the cout identifier and

    the

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    26/62

    4. Directives

    The using directive says that all the program statements

    that follow are within the std  namespace.

    If we didn’t use the using directive, we would need to addthe std  name to many program elements. For example:

    To avoid adding std:: dozens of times in programs we use

    the using directive instead.

    26

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    using namespace std;

    std::cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    27/62

    5. Comments27

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    Comments are an important part of any program. Theyhelp the person writing a program, and anyone else who

    must read the source file, understand what’s going on. 

    The compiler ignores comments, so they do not add to thefile size or execution time of the executable program.

    A comment can start at the beginning of the line or on the

    same line following a program statement.

    We can write the comments using any languages we caneasily understand it.

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    28/62

    5. Comments

    The first comment style start with a double slash symbol // and terminate at the end of the line.

    28

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    // comments.cpp

    // demonstrates comments#include //preprocessor directiveusing namespace std; //”using” directive int main( ) //function name “main” { //start function body

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    29/62

    5. Comments

    There’s a second comment style available in C++. This

    type of comment (the only comment originally available in

    C) begins with the /* character pair and ends with */ (not

    with the end of the line).

    This is a good approach to making a comment out of a

    large text passage, since it saves inserting the // symbol

    on every line.

    29

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    /* this is an old-style comment */

    /* this is a

    potentiallyvery longmultilinecomment

    */

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    30/62

    5. Comments

    You can also insert a /* …. */ comment anywhere withinthe text of a program line:

    If you attempt to use the // style comment in this case, the

    closing brace won’t be visible to the compiler. Since a // 

    style comment runs to the end of the line, and the code

    won’t compile correctly. 

    30

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    func1( )

    { /* empty function body */ }

    func1( ){ // empty function body }

    √ 

    х 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    31/62

    6. Integer Variables

    Variables are the most fundamental part of any language.  A variable has a symbolic name and can be given a

    variety of values.

    Variables are located in particular places in thecomputer’s memory.

    When a variable is given a value, that value is actuallyplaced in the memory space assigned to the variable.

    Most popular languages use the same general variabletypes, such as integers, floating-point numbers, andcharacters, so you are probably already familiar with theideas behind them.

    31

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    32/62

    6. Integer Variables

    Variable Names

    You can use uppercase and lowercase letters, and the digits

    from 1 to 9. You can also use the underscore ( _ ).

    The first character must be a letter or underscore. The compiler distinguishes between uppercase and

    lowercase letters, so Var  is not the same as var  or VAR.

    You can’t use a C++ keyword as a variable name. A

    keyword is a predefined word with a special meaning. int,

    class, if , and while are examples of keywords.

    A variable’s name should make clear to anyone reading the

    listing the variable’s purpose and how it is used. 

    32

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    33/62

    6. Integer Variables

    Integer variables represent integer numbers like 1, 30,000, and – 27. Such numbers are used for counting discrete numbers ofobjects, like 11 pencils or 100 students.

    Unlike floating-point numbers, integers have no fractional part.

    Integer variables exist in several sizes, but the most commonlyused is type int. The amount of memory occupied by the integertypes is system dependent. On a 32-bit system such as Windows,an int occupies 4 bytes (byte = 8 bits) of memory. This allows anint to hold numbers in the range from – 2,147,483,648 to2,147,483,647.

    Type int occupies only 2 bytes in 16-bit systems such as MS-DOSand earlier versions of Windows.

    33

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    34/62

    6. Integer Variables

    There are several numerical integer types besides type int.The two most common types are long and short. We noted

    that the size of type int is system dependent. In contrast,

    types long and short have fixed sizes no matter what system

    is used.

    Type long always occupies four bytes. Thus it has range,

    from – 2,147,483,648 to 2,147,483,647.

    Type short always occupies two bytes. Thus it has range,from – 32,768 to 32,767.

    If you want to create a constant of type long, use the letter L 

    following the numerical value, as in:

    34

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    long var = 7678L;

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    35/62

    6. Integer Variables35

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    #includeusing namespace std;int main( ){

    int var1; //define var1int var2; //define var2var1 = 20; //assign value to var1var2 = var1 + 10; //assign value to var2

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    36/62

    6. Integer Variables

    The statement, ,is called declaration.

    You must declare a variable before using it.

    However, you can place variable declarations anywhere in

    a program. It’s not necessary to declare variables before the first

    executable statement (as was necessary in C).

    However, it’s probably more readable if commonly-used

    variables are located at the beginning of the program.

    36

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    int var1; 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    37/62

    6. Integer Variables

    Output Variations

    The statement displays a string constant.

    The next statement displays the value of

    the variable var2. The output of the program is

    As you can see, the output of the two cout statements

    appears on the same line on the output screen. No linefeed

    is inserted automatically. If you want to start on a new line,

    you must insert a linefeed yourself.

    or

    37

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    var1+10 is 30

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    38/62

    7. Character Variables

    Type char  stores integers that range in value from – 128 to 127.Variables of this type occupy only 1 byte (eight bits) of memory.

    Character variables are much more commonly used to store

    ASCII characters.

    The ASCII character set is a way of representing characters such

    as ‘a’, ‘B’, ‘$’, ‘3’, and so on, as numbers. 

    Character constants use single quotation marks around a

    character, like ‘a’ and ‘b’. (Note that this differs from string

    constants, which use double quotation marks).

    When the C++ compiler encounters such a character constant, it

    translates it into the corresponding ASCII code. The constant ‘a’ 

    appearing in a program, for example, will be translated into 97.

    38

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    39/62

    ASCII Chart 39

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    ASCIIChart 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    40/62

    ASCII Chart 40

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    41/62

    ASCII Chart 41

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    42/62

    7. Character Variables42

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    // charvars.cpp: demonstrates character variables#include  //for cout, etc.using namespace std;int main( ){

    char ch1 = 'A'; //define char variable as character

    char ch2 = '\t'; //define char variable as tab

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    43/62

    7. Character Variables43

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    Escape Sequence Character

    \ n  Newline

    \ t Tab

    \ ' Single quotation mark

    \ " Double quotation marks

    \ \ Backslash

    \ xdd Hexadecimal notation

    Escape Sequences 

    The character constant ‘ \ t’  is an example of an escape sequence.

    The name reflects the fact that the backslash causes an “escape”

    from the normal way characters are interpreted.

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    44/62

    7. Character Variables44

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    Sometimes you need to represent a character constant thatdoesn’t appear on the keyboard, such as the graphics

    characters above ASCII code 127.

    To do this, you can use the ‘\xdd ’ representation, where each

    d stands for a hexadecimal digit.

    If you want to print a solid rectangle, for example, you’ll

    find such a character listed as decimal number 178, which is

    hexadecimal number B2 in the ASCII table. This characterwould be represented by the character constant ‘\xB2’ .

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    45/62

    8. Input with cin45

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    // fahren.cpp// demonstrates cin, newline#includeusing namespace std;int main( ){

    int ftemp; //for temperature in fahrenheitcoutftemp;

    int ctemp = (ftemp-32) * 5 / 9;cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    46/62

    8. Input with cin 

    The statement causes the program to wait forthe user to type in a number.

    The resulting number is placed in the variable ftemp.

    The keyword cin (pronounced “C in”) is an object,

    predefined in C++ to correspond to the standard inputstream.

    This stream represents data coming from the keyboard.

    The >> is the extraction or get from operator. It takes thevalue from the stream object on its left and places it in thevariable on its right.

    46

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    cin>>ftemp;

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    47/62

    8. Input with cin 

    Expressions

    Any arrangement of variables, constants, and operators that

    specifies a computation is called an expression.

    Thus, alpha+12 and (alpha-37)*beta/2 are expressions.

    Precedence

    Any Arithmetic operation proceeds according to an

    established precedence of operations. Thus, Precedence is

    the order in which operators are evaluated.

    47

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    48/62

    Precedence Summary 48

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    #includeusing namespace std;int main( ){

    int a=4, b=6, c=3, d, e, f, g;d = –a + b * c;e = (a + b) * c;f = a * b / c;g = a * (b / c);

    cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    49/62

    Precedence Summary 

    The operators higher on the list have higher precedencethan those lower down.

    Operators with higher precedence are evaluated before

    those with lower precedence.

    Operators on the same row have equal precedence.

    49

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    50/62

    9. Floating Point Types

    Floating-point variables represent numbers with a decimalplace like 3.1415927, 0.0000625, and – 10.2.

    They have both an integer part, to the left of the decimal

    point, and a fractional part, to the right.

    Floating-point variables represent what mathematicians call

    real numbers, which are used for measurable quantities such

    as distance, area, and temperature. They typically have a

    fractional part. There are three kinds of floating-point variables in C++:

    type float, type double, and type long double.

    50

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    51/62

    9. Floating Point Types

    Type float

    Type float stores numbers in the range of about 3.4x10 – 38 

    to 3.4x1038, with a precision of seven digits. It occupies 4

    bytes in memory.

    Type double

    The larger floating point types, double, is similar to float 

    except that it requires more memory space and provide a

    wider range of values and more precision.  Type double requires 8 bytes of storage and handles

    numbers in the range from 1.7x10 – 308 to 1.7x10308 with a

    precision of 15 digits.

    51

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    52/62

    9. Floating Point Types 52

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    //circarea.cpp//demonstrates floating point variables

    #include //for cout, etc.using namespace std;int main( )

    {float rad; //variable of type floatconst float PI = 3.14159F; //type const floatcoutrad; //get radius

    float area = PI * rad * rad; //find areacout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    53/62

    9. Floating Point Types

    Exponential Notation You can write floating-point constants using exponential notation.

    Exponential notation is a way of writing large numbers withouthaving to write out a lot of zeros.

    For example, 1,000,000,000 can be written as 1.0E9 inexponential notation. Similarly, 1234.56 would be written1.23456E3. (This is the same as 1.23456 *103).

    The number following the E is called the exponent. It indicateshow many places the decimal point must be moved to change thenumber to ordinary decimal notation.

    The exponent can be positive or negative. The exponentialnumber 6.35239E – 5 is equivalent to 0.0000635239 in decimalnotation. This is the same as 6.35239 *10 – 5.

    53

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    54/62

    9. Floating Point Types

    The const Qualifier

    The keyword const (for constant) precedes the data type of avariable. It specifies that the value of a variable will not change

    throughout the program. Any attempt to alter the value of a variable defined with this

    qualifier will elicit an error message from the compiler.

    The #define Directive

    The line appearing at the beginning of yourprogram specifies that the identifier PI will be replaced by thetext 3.14159 throughout the program.

    However, you can’t specify the data type of the constant using

    #define.

    54

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    const float PI = 3.14159F;

    #define PI 3.14159

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    55/62

    10. Type bool  

    Variables of type bool  can have only two possible values:true and false.

    In theory a bool type requires only one bit (not byte) of

    storage, but in practice compilers often store them as bytes 

    because a byte can be quickly accessed, while an individual

    bit must be extracted from a byte, which requires additional

    time.

    As we’ll see, type bool  is most commonly used to hold theresults of comparisons.

    The true/false values are often called Boolean values.

    55

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    56/62

    11. The setw  Manipulator

    You can think of each value displayed by cout as occupying afield: an imaginary box with a certain width.

    The default field is just wide enough to hold the value. That is, the

    integer 567 will occupy a field three characters wide, and the

    string “pajamas” will occupy a field seven characters wide.

    The setw  manipulator causes the number (or string) that follows it

    in the stream to be printed within a field n characters wide,

    where n is the argument to setw( n ).

    The declarations for the manipulators (except endl ) are not in the

    usual iostream header file, but in a separate header file called

    iomanip. When you use these manipulators you must #include this

    header file in your program.

    56

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    57/62

    11. The setw  Manipulator 57

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    // width1.cpp// demonstrates need for setw manipulator

    #includeusing namespace std;int main( )

    {long sd1=83, sd2=78, sd3=91;cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    58/62

    11. The setw  Manipulator 58

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    // width1.cpp// demonstrates need for setw manipulator

    #include#include // for setw using namespace std;

    int main( ){

    long sd1=83, sd2=78, sd3=91;cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    59/62

    11. The setw  Manipulator 59

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    #include#include // for setwusing namespace std;int main( ){

    long sd1=83, sd2=78, sd3=91;cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    60/62

    11. The setw  Manipulator 60

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

    #include#include // for setwusing namespace std;int main( ){

    long sd1=83, sd2=78, sd3=91;cout

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    61/62

    12. Variable Type Summary61

     Ahmed Mohamed Sayed Ahmed  - Com uter lecturer Thursday, November 22, 2012

    Chapter 2: C++ Programming Basics 

  • 8/18/2019 C++ - Chapter 2 - C++ Programming Basics

    62/62

    62

    Thanks for

    Listening