0% found this document useful (0 votes)
82 views25 pages

2003 Prentice Hall, Inc. All Rights Reserved

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 25

Outline

 2003 Prentice Hall, Inc.


All rights reserved.
What is C++?

• C++ is object oriented languages


• As an object oriented language, C++ has
power and extensibility to write large scale
programs.

 2003 Prentice Hall, Inc. All rights reserved.


3

Basics of a Typical C++ Environment

– C++ Language definition


– Program-development environment (tools)
• compiler, linker, editor, debugger
– C++ Standard Library (software)
• precompiled routines you can use

 2003 Prentice Hall, Inc. All rights reserved.


4
Basics of a Typical C++ Environment
Program created in the editor
Edit Editor foo.cpp and stored on disk.
(vi, emacs)
Preprocessor strips out comments,
Preprocessor expands macros
Compile (and Link)
(CC , g++)

Compiler
foo.o Compiler creates object code and
stores it on disk (.o file)
C++ Library Linker foo Linker links the object code with the
(a.out) libraries, stores in a.out on disk.
Loader
Loader puts program
Run in memory (RAM)
..
(load and execute) ..
..

./foo, ./a.out

CPU CPU takes each instruction and


 
executes it, possibly storing
new data values in memory
(RAM) as the program executes.
..
..
..

 2003 Prentice Hall, Inc. All rights reserved.


5
Basics of a Typical C++ Environment
• Input/output streams in C++
– cin (pronounce “see in”)
• Standard input stream
• Normally keyboard
– cout(pronounce “see out”)
• Standard output stream
• Normally computer screen
cout << variable-name;
Meaning: print the value of variable <variable-name> to the user
cout << “any message “;
Meaning: print the message within quotes to the user
cout << endl;
Meaning: print a new line

 2003 Prentice Hall, Inc. All rights reserved.


I/O Streams and Standard I/O Devices
(continued)
• Use iostream header file to extract (receive)
data from keyboard and send output to the screen
– Contains definitions of two data types:
• istream - input stream
• ostream - output stream
– Has two variables:
• cin - stands for common input
• cout - stands for common output

 2003 Prentice Hall, Inc. All rights reserved.


I/O Streams and Standard I/O Devices
(continued)
• To use cin and cout, the preprocessor directive
#include <iostream> must be used
• Variable declaration is similar to:
– istream cin;
– ostream cout;
• Input stream variables: type istream
• Output stream variables: type ostream

 2003 Prentice Hall, Inc. All rights reserved.


8
General Notes About C++
and This Book
• Book geared toward novice programmers
– Stress programming clarity
– C and C++ are portable languages
• Portability
– C and C++ programs can run on many different
computers
• Compatibility
– Many features of current versions of C++ not
compatible with older implementations

 2003 Prentice Hall, Inc. All rights reserved.


9

Introduction to C++ Programming

• Structured programming
– Chapters 1-5
• Object-oriented programming
– Chapters 6-10
• C++ language examples
– Next few slides contain several examples
– Illustrate many important features of C++
– Each analyzed one statement at a time

 2003 Prentice Hall, Inc. All rights reserved.


10
A Simple Program:
Printing a Line of Text
• Comments
– Document programs
– Improve program readability
– Ignored by compiler
– Single-line comment
• Begin with //
• Preprocessor directives
– Processed by preprocessor before compiling
– Begin with #

 2003 Prentice Hall, Inc. All rights reserved.


11
1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++.
Single-line comments. Outline
3 #include <iostream>Function main returns an
4 integer
Left brace { begins Preprocessor
value. function directive to
fig01_02.cpp
5 // function main body.program
begins Function include input/output Statements
main appears
execution stream end with a(1 of 1)
6 int main() header
exactly once file <iostream>.
in every C++ semicolon ;.
7 { program..
8 std::cout << "Welcome to C++!\n"; fig01_02.cpp
9 Corresponding right brace } output (1 of 1)
10 return 0; // ends function
indicate thatbody.
program ended successfully
11 Name coutStream insertion
belongs to operator.
12 } // end function main namespace std.

Keyword return is one of


Welcome to C++!
several means to exit
function; value 0 indicates
program terminated
successfully.

 2003 Prentice Hall, Inc.


All rights reserved.
12
A Simple Program:
Printing a Line of Text
• Standard output stream object
– std::cout
– “Connected” to screen
– <<
• Stream insertion operator
• Value to right (right operand) inserted into output stream
• Namespace
– std:: specifies using name that belongs to “namespace”
std
– std:: removed through use of using statements
• Escape characters
– \
– Indicates “special” character output

 2003 Prentice Hall, Inc. All rights reserved.


13
A Simple Program:
Printing a Line of Text

Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

 2003 Prentice Hall, Inc. All rights reserved.


14
1 // Fig. 1.4: fig01_04.cpp
2 // Printing a line with multiple statements.
Outline
3 #include <iostream>
4
fig01_04.cpp
5 // function main begins program execution Multiple stream insertion (1 of 1)
6 int main() statements produce one line of
7 { output.
8 std::cout << "Welcome "; fig01_04.cpp
9 std::cout << "to C++!\n"; output (1 of 1)
10
11 return 0; // indicate that program ended successfully
12
13 } // end function main

Welcome to C++!

 2003 Prentice Hall, Inc.


All rights reserved.
15
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single statement
Outline
3 #include <iostream>
4
Using newline characters to fig01_05.cpp
5 // function main begins program execution
print on multiple lines. (1 of 1)
6 int main()
7 {
8 std::cout << "Welcome\nto\n\nC++!\n"; fig01_05.cpp
9 output (1 of 1)
10 return 0; // indicate that program ended successfully
11
12 } // end function main

Welcome
to
 
C++!

 2003 Prentice Hall, Inc.


All rights reserved.
16

Arithmetic

* Multiplication
/ Division
Integer division truncates remainder
7 / 5 evaluates to 1
% Modulus operator returns remainder
7 % 5 evaluates to 2

 2003 Prentice Hall, Inc. All rights reserved.


17

Arithmetic

• Rules of operator precedence


– Operators in parentheses evaluated first
• Nested/embedded parentheses
– Operators in innermost pair first
– Multiplication, division, modulus applied next
• Operators applied from left to right
– Addition, subtraction applied last
Operator(s) • Operators applied fromOrder
Operation(s) leftoftoevaluation
right (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.

 2003 Prentice Hall, Inc. All rights reserved.


18
Decision Making: Equality and Relational
Operators
• if structure
– Make decision based on truth or falsity of condition
• If condition met, body executed
• Else, body not executed
• Equality and relational operators
– Equality operators
• Same level of precedence
– Relational operators
• Same level of precedence
– Associate left to right

 2003 Prentice Hall, Inc. All rights reserved.


19
Decision Making: Equality and Relational
Operators
Sta nd a rd a lg e b ra ic C ++ e q ua lity Exa m p le Me a ning o f
e q ua lity o p e ra to r o r o r re la tio na l o f C ++ C ++ c o nd itio n
re la tio na l o p e ra to r o p e ra to r c o nd itio n

Relational operators
> > x > y x is greater than y
< < x < y x is less than y

 >= x >= y x is greater than or equal to y

 <= x <= y x is less than or equal to y

Equality operators
= == x == y x is equal to y

 != x != y x is not equal to y

 2003 Prentice Hall, Inc. All rights reserved.


Characteristics of oop
20
• Object: Consist of both data and functions
that operate on that data
• Class: Specification for number objects
• Inheritance: Process of creating new
classes called derived classes from existing
or base class.
• Reusability: One a class has been written
created and debugged, it can be distributed
to other programmers for use in their own
program. This known as Reusability.

 2003 Prentice Hall, Inc. All rights reserved.


C and C++
21

• C++ is derived from c language


• It is superset of c
• Every correct statement in c is also a correct
statement in C++
• The reverse is not true
• The most important elements added to C++ are
concerned with Classes, object and OOP
• C++ Was originally called “C with Classes”

 2003 Prentice Hall, Inc. All rights reserved.


Details For C
22

• C is Middle level language


• As middle level language, C allow the
manipulation of bits and bytes and
addresses, the basic elements with which
computer function.

 2003 Prentice Hall, Inc. All rights reserved.


Details For C “Cont.'s”
23

• C Code is portable
• Portability means that is easy to adapt
software written for one type of computer
or operating system to another.
• For example, if you can easy convert a
program written for DOS so that it runs
under window . The program is portable.

 2003 Prentice Hall, Inc. All rights reserved.


Levels of languages
24
High level languages
•Pascal
•Fortan
•Cobal
•Basic
Middle level languages
•Java
•C++
•C
Low level languages
•Micro-assembler
•Assembler
 2003 Prentice Hall, Inc. All rights reserved.
25

•Let’s
practice
 2003 Prentice Hall, Inc. All rights reserved.

You might also like