26
CHAPTER 1: INTRODUCTION C++ Programming

Chapter 1: Introduction

  • Upload
    tarak

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 1: Introduction. C++ Programming. CS 241. Course URL: http ://241cs.wordpress.com / Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition or seventh Edition C++ Without Fear A Biggener's Guide That Makes You Feel Smart, Brian Overland. GRADES. Grading - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 1: Introduction

CHAPTER 1: INTRODUCTIONC++ Programming

Page 2: Chapter 1: Introduction

CS 241

Course URL: • http://241cs.wordpress.com/

Text Book: • C++ How to Program, DETITEL & DEITEL, eighth Edition

or seventh Edition• C++ Without Fear A Biggener's Guide That Makes You

Feel Smart, Brian Overland

Page 3: Chapter 1: Introduction

GRADES

Grading• First Midterm: 10• Second Midterm:15• Lab quize:5• Homework:5• Participation and Evaluations:5• Final: 40• Final Lab: 20

Page 4: Chapter 1: Introduction

Course SyllabusTOPICS

Introduction

Programming basics(data types, variables, assignment, operators)

Program flow (control statements and looping)

Functions

Arrays

Pointers

Recursive functions

Introduction to mathematical software packages

Simple mathematical problem solving using the chosen software package

Page 5: Chapter 1: Introduction

What is a Computer?Computer• is a device that can perform computations and make logical decisions

billions of times faster than human beings can.

Computer programs• Sets of instructions that control computer’s processing of data

Hardware• Various devices comprising computer

• Keyboard, screen, mouse, disks, memory, CD-ROM, processing units,

Software• Programs that run on computer

*Computers (often referred to as hardware) are controlled by software

Page 6: Chapter 1: Introduction

Programming Languages

Computer languages may be divided into three general types:

1. Machine languages2. Assembly languages3. High-level languages

Page 7: Chapter 1: Introduction

Programming Languages

1- Machine language:

• Only language computer directly understands

• Consist of strings of numbers 1s and 0s.

• Machine language is often referred to as object code.

Page 8: Chapter 1: Introduction

Programming Languages

2- Assembly language

• English-like abbreviations that represent elementary operations

• Clearer to humans

• Translator programs called assemblers convert assembly-language programs to machine language.

• Programmers still had to use many instructions to accomplish even the simplest tasks.

• Example: MOV AL , #61h

Page 9: Chapter 1: Introduction

Programming Languages

3- High-level languages

• Similar to everyday English

• Single statements accomplish substantial tasks

• Translator programs called compilers convert high-level language programs into machine language

Page 10: Chapter 1: Introduction

Terms

Source code• is a program in a form suitable for reading and writing by a human

being

Executable program (executable)• is a program in a form suitable for running on a computer

Compilation• is the process of translating source code into object code

Compiler• is a program that performs compilation as defined above

The object code file• contains a sequence of instructions that the processor can

understand but that is difficult for a human to read or modify.

Page 11: Chapter 1: Introduction

Typical C++ Development Environment

C++ systems generally consist of three parts:• a program development environment, the language and the C++

Standard Library.

C++ programs typically go through six phases:

1. edit2. preprocess3. compile4. link 5. load 6. execute

Page 12: Chapter 1: Introduction

Loader

PrimaryMemory

Program is created inthe editor and stored

on disk.

Preprocessor programprocesses the code.

Loader puts programin memory.

CPU takes eachinstruction and

executes it, possiblystoring new data

values as the programexecutes.

CompilerCompiler creates

object code and storesit on disk.

Linker links the objectcode with the libraries,

creates a.out andstores it on disk

Editor

Preprocessor

Linker

 CPU

PrimaryMemory

.

.

.

.

.

.

.

.

.

.

.

.

Disk

Disk

Disk

Disk

Disk

Page 13: Chapter 1: Introduction

More details…

Edit

• Programmer writes program (and stores source code on disk)

Pre-process

• Perform certain manipulations before compilation

Compile

• the compiler translates the C++ program into machine-language

code (also referred to as object code).

Page 14: Chapter 1: Introduction

More details…

Link• A linker perform the link operation to produce an executable

program.

Load• Before a program can be executed, it must first be placed in memory.• This is done by the loader, which takes the executable image from

disk and transfers it to memory.

Execute• the computer, under the control of its CPU, executes the program one

instruction at a time

Page 15: Chapter 1: Introduction

First Program in C++: Printing a Line of

Text

Page 16: Chapter 1: Introduction

First Program in C++: Printing a Line of Text (cont.)

Comments• Document programs• Improve program readability• Ignored by compiler• Single-line comment

Begin with //• Multiple-line comment

• Begin with /* end with */

Page 17: Chapter 1: Introduction

First Program in C++: Printing a Line of Text (cont.)

preprocessor directive• Lines that begin with # are processed by the preprocessor

before the program is compiled.

• #include <iostream> notifies the preprocessor to include in the program the contents of the input/output stream• Must be included for any program that outputs data to the

screen or inputs data from the keyboard

Page 18: Chapter 1: Introduction

Standard Library

Rich collections of existing code that can be reused in your applications

1. Common math calculations e.g. sqrt

2. String manipulations

3. Character manipulations

4. Input/output

5. Error checking

• *Provided as part of the C++ development environment

Page 19: Chapter 1: Introduction

iostream library

• part of the C++ Standard Library

• provides a uniform way of handling input from (and output to)predefined sources

iostream library commands:

•Standard Input Stream (cin) - Normally keyboard

• Standard Output Stream (cout)- Normally computer screen

• Standard Error Stream (cerr) - Display error messages

Page 20: Chapter 1: Introduction

First Program in C++: Printing a Line of Text (cont.)• main is a part of every C++ program.• The parentheses after main indicate that main is a program

building block called a function.• C++ programs typically consist of one or more functions and

classes.• Exactly one function in every program must be named main.• C++ programs begin executing at function main, even if main is

not the first function in the program.• The keyword int to the left of main indicates that main “returns”

an integer value.• A keyword is a word in code that is reserved by C++ for a

specific use.• For now, simply include the keyword int to the left of main in

each of your programs.

Page 21: Chapter 1: Introduction

First Program in C++: Printing a Line of Text (cont.)• A left brace, {, must begin the body of every function.• A corresponding right brace, }, must end each function’s body.• A statement normally ends with a semicolon (;), also known as

the statement terminator.• Preprocessor directives (like #include) do not end with a

semicolon.

Page 22: Chapter 1: Introduction

First Program in C++: Printing a Line of Text (cont.)

• The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std.

• The names cin (the standard input stream) and cerr (the standard error stream) also belong to namespace std.

•The << operator is referred to as the stream insertion operator

Page 23: Chapter 1: Introduction

First Program in C++: Printing a Line of Text (cont.)

• The characters \n are not printed on the screen.

• The escape sequence \n means newline.• Causes the cursor to move to the beginning of the next line

on the screen.

• When the return statement is used at the end of main the value 0 indicates that the program has terminated successfully.

• According to the C++ standard, if program execution reaches the end of main without encountering a return statement, it’s assumed that the program terminated successfully—exactly as when the last statement in main is a return statement with the value 0.

Page 24: Chapter 1: Introduction
Page 25: Chapter 1: Introduction

Modifying Our First C++ Program

Page 26: Chapter 1: Introduction

Modifying Our First C++ Program