01 28 11
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Basic Elements of a C ++ Program PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on
  • Presentation posted in: General

01/28/11. Basic Elements of a C ++ Program. Quiz 1. Monday Chapter 1, Linux and vim On your own p. 24 #1-8, 13, 14, 16 odd answers in text Define Memory address Memory contents Machine language. Objectives. State the basic parts of a C++ program. Create legal C++ names.

Download Presentation

Basic Elements of a C ++ Program

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


01 28 11

01/28/11

Basic Elements of a C ++ Program


Quiz 1

Quiz 1

  • Monday

  • Chapter 1, Linux and vim

  • On your own

    • p. 24 #1-8, 13, 14, 16

    • odd answers in text

  • Define

    • Memory address

    • Memory contents

    • Machine language


Objectives

Objectives

  • State the basic parts of a C++ program.

  • Create legal C++ names.

  • Write statements to output strings.


A sample c program

A Sample C++ Program

  • travel.cpp


A sample c program1

A Sample C++ Program

#include <iostream>

  • Preprocessor directive

  • Must have to do input/output

  • cout, cin, <<, >>

    using namespace std;

  • Required

  • Used to prevent name conflicts in large projects


Travel cpp

travel.cpp

int main()‏

  • Beginning of first function executed

  • Only one main

  • Must be in every program

    { }

  • Brackets delimit executable statements


Travel cpp1

travel.cpp

double dist, time, speed;

  • Declare variables

  • Set aside locations in memory

  • Gives the locations names

  • double tells the data's type


Travel cpp2

travel.cpp

cout<< "Miles traveled?";

cin >>dist; //input

  • cout puts message on monitor

  • cin inputs data and stores it in variable, dist.

  • // begins a comment


Travel cpp3

travel.cpp

speed = dist/time; //find speed

  • Assignment Statement

  • speed gets dist/time

  • Does computation, stores answer in "speed"


Travel cpp4

travel.cpp

cout << speed << " mph\n";

  • Outputs result to monitor

  • \n is newline character

    return 0;

  • Ends program


Names

Names

  • Examples:

    • rate, time, distance

    • R2D2, k9, K9, _acceration_gravity

  • Contain letters, digits, underscore

  • Start with letter or underscore


Names1

Names

  • Are these legal?

    Yr2007, 10_year_average, Y2K, form-99, M_PI


Keywords

Keywords

  • Names C++ reserves

  • Cannot as use variable names.

  • E.G. double, return, namespace

  • See appendix F


Output

Output


Output1

Output

  • Example -- out.cpp

  • cout

    • Console output

    • Output stream

      • destination for stream of characters

    • Goes to standard output, usually monitor screen


Output2

Output

  • <<

    • insertion operator

    • inserts characters in output stream

  • String

    • "September 7th is"

    • inserted into stream


Output3

Output

cout << “Fire”; //Leaves cursor after “e”

cout << “fighter”;

Output :

Firefighter


Output4

Output

cout << “Fire\n”; //Cursor on new line

cout << “fighter”;

Output :

Fire

fighter

  • \n is an escape sequence


Output5

Output

  • Program out1.cpp

  • several \n's


Example

Example


Output6

Output


Output7

Output

  • endl

    • stream manipulator function.

    • Another way to output a newline character.

    • out2.cpp


Basic elements of a c program

Next

  • Algorithms

  • Data Types 2.4

  • Input 2.3


  • Login