0% found this document useful (0 votes)
10 views

Lectures 1&2

The document discusses an introduction to C++ programming. It covers: 1) The history and evolution of C++ from earlier languages like C and its capabilities for object-oriented programming. 2) Key concepts in C++ like objects, classes, functions, and the standard library. 3) The typical phases of a C++ program from editing code to compiling, linking, loading and executing it. 4) An example "Hello World" C++ program and an explanation of how it works.

Uploaded by

Zarak Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lectures 1&2

The document discusses an introduction to C++ programming. It covers: 1) The history and evolution of C++ from earlier languages like C and its capabilities for object-oriented programming. 2) Key concepts in C++ like objects, classes, functions, and the standard library. 3) The typical phases of a C++ program from editing code to compiling, linking, loading and executing it. 4) An example "Hello World" C++ program and an explanation of how it works.

Uploaded by

Zarak Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction

– In this course you will learn


– C++
– Object oriented programming

© 2000 Deitel & Associates, Inc. All rights reserved.


History of C and C++

• C++ evolved from C, which evolved from two


previous programming languages, BCPL and B
• ANSI C established worldwide standards for C
programming
• C++ “spruces up” C and provides capabilities for
object-oriented programming
– objects - reusable software components, model things in the real
world
– Object-oriented programs are easy to understand, correct and
modify

© 2000 Deitel & Associates, Inc. All rights reserved.


The Key Software Trend: Object Technology

• Objects
– reusable software components that model items in the
real world
– meaningful software units
• date objects, time objects, paycheck objects, invoice objects,
audio objects, video objects, file objects, record objects, etc.
• any noun can be represented as an object

– very reusable
– more understandable, better organized, and easier to
maintain than procedural programming
– Favor modularity

© 2000 Deitel & Associates, Inc. All rights reserved.


C++ Standard Library

• C++ programs consist of pieces called classes and


functions

• There are rich collections of existing classes and


functions in the C++ standard library available for
all programmers to use

© 2000 Deitel & Associates, Inc. All rights reserved.


Basics of a Typical C++ Environment

• Phases of C++ Programs: Editor Disk


Program is created in
the editor and stored
on disk.

1. Edit Preprocessor Disk


Preprocessor program
processes the code.

2. Preprocess Compiler Disk


Compiler creates
object code and stores
it on disk.

Linker links the object

3. Compile Linker Disk


code with the libraries,
creates a.out and
stores it on disk

4. Link
Primary
Memory
Loader

5. Load Disk
Loader puts program
in memory.

..
..

6. Execute ..

Primary
Memory
CPU CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
.. executes.
..
..

© 2000 Deitel & Associates, Inc. All rights reserved.


Introduction to C++ Programming

• The C++ language facilitates a structured and


disciplined approach to computer program design

• Following are several examples that illustrate


many important features of C++. Each example is
analyzed one statement at a time.

© 2000 Deitel & Associates, Inc. All rights reserved.


1 // Fig. 1.2: fig01_02.cpp
Outline
2 // A first program in C++
Comments
3 #include <iostream> Written between /* and */ or following a //
4 1.19
Improve program readability and do notPrinting
cause thea line of text
5 int main() computer to perform any action

6 {
preprocessor directive
7 std::cout << "Welcome to C++!\n";
Message to the C++ preprocessor
8
Lines beginning with # are preprocessor directives
9 return 0; // indicate that program ended
successfully
#include <iostream> tells the preprocessor to
10 } include
C++ the contents
programs containofone
theor
filemore
<iostream>, which
functions, exactly
includes
one input/output
of which operations (such as printing to
must be main
the screen).
Welcome to C++! Parenthesis used to indicate a function
int means that main "returns" an integer value.
Prints the string of characters contained between the
More in Chapter 3.
return is one a way toquotation
exit a marks.
function.
The entire line, including std::cout, the <<
return 0, in this case,operator,
means that
the string "Welcome to C++!\n" and
the program terminated normally.
the semicolon (;), is called a statement.

All statements must end with a semicolon.

© 2000 Deitel & Associates, Inc. All rights reserved.


1.19 A Simple Program:
Printing a Line of Text
• std::cout
– standard output stream object
– “connected” to the screen
– we need the std:: to specify what "namespace" cout belongs
to
• we shall remove this prefix with using statements
• <<
– stream insertion operator
– value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
std::cout << " Welcome to C++!\n"

•\
– escape character
– indicates that a “special” character is to be output
© 2000 Deitel & Associates, Inc. All rights reserved.
1.19 A Simple Program:
Printing a Line of Text (II)

• There are multiple ways to print text


– Following are more examples

© 2000 Deitel & Associates, Inc. All rights reserved.


1 // Fig. 1.4: fig01_04.cpp

2 // Printing a line with multiple statements


Outline
3 #include <iostream>

4
1. Load <iostream>
5 int main()

6 { 2. main
7 std::cout << "Welcome ";

8 std::cout << "to C++!\n";


2.1 Print "Welcome"

9
2.2 Print "to C++!"
10 return 0; // indicate that program ended
successfully
11 } 2.3 newline

2.4 exit (return 0)


Welcome to C++!

Unless new line '\n' is specified, the text continues


on the same line.

© 2000 Deitel & Associates, Inc. All rights reserved.


1 // Fig. 1.5: fig01_05.cpp

2 // Printing multiple lines with a single statement


Outline
3 #include <iostream>

4 1. Load <iostream>
5 int main()
2. main
6 {

7 std::cout << "Welcome\nto\n\nC++!\n"; 2.1 Print "Welcome"


8
2.2 newline
9 return 0; // indicate that program ended
successfully
10 }
2.3 Print "to"

Welcome
2.4 newline
to

C++!
2.5 newline

Multiple lines can be printed with one 2.6 Print "C++!"


statement
2.7 newline

2.8 exit (return 0)


© 2000 Deitel & Associates, Inc. All rights reserved.
Another Simple Program:
Adding Two Integers

• Variables
– location in memory where a value can be stored for use by a
program
– must be declared with a name and a data type before they can be
used
– Must appear before variable is used
– Some common data types are:
• int - integer numbers
• char - characters
• double - floating point numbers
– Example: int myVariable;
• Declares a variable named myVariable of type int
Example: int variable1, variable2;
• Declares two variables, each of type int

© 2000 Deitel & Associates, Inc. All rights reserved.


Another Simple Program:
Adding Two Integers (II)
• >> (stream extraction operator)
– When used with std::cin, waits for user to input a value and
stores the value in the variable to the right of the operator.
– user types number, then presses the Enter (Return) key to send the
data to the computer
– Example:
int myVariable;
std::cin >> myVariable;
• waits for user input, then stores input in myVariable

• = (assignment operator )
– assigns value to a variable
– binary operator (has two operands)
sum = variable1 + variable2;

Addition operator
© 2000 Deitel & Associates, Inc. All rights reserved.
1 // Fig. 1.6: fig01_06.cpp
2 // Addition program Outline
3 #include <iostream>
4 1. Load <iostream>
5 int main()
6 { 2. main
7 int integer1, integer2, sum; //
declaration
8 2.1 Initialize variables
9 std::cout << "Enter first integer\n"; // prompt integer1, integer2, and
Notice how std::cin issum
used to get user
10 std::cin >> integer1; // read an
integer input.
11 std::cout << "Enter second integer\n"; // prompt
2.2 Print "Enter first
12 std::cin >> integer2; // read an
integer integer"
13 sum = integer1 + integer2; //
assignment14
of sum
2.2.1 Get input
std::cout << "Sum is " << sum << std::endl; //
print sum 15
2.3 Print "Enter second
16 return 0; std::endl
// indicate that program ended flushes the buffer and
integer"
successfully
17 } prints a newline. 2.3.1 Get input

Enter first integer 2.4 Add variables and put


45 Variables can be output using std::cout << variableName
result into sum
Enter second integer
72
Sum is 117 2.5 Print "Sum is"
2.5.1 Output sum

2.6 exit (return 0)


Program Output
© 2000 Deitel & Associates, Inc. All rights reserved.
Memory Concepts

• Variable names correspond to locations in the


computer's memory.
– Every variable has a name, a type, a size and a value.
– Whenever a new value is placed into a variable, it replaces the
previous value - it is destroyed
– reading variables from memory does not change them
• A visual representation

integer1 45

© 2000 Deitel & Associates, Inc. All rights reserved.


Arithmetic
• Arithmetic calculations are used in most programs
– special notes:
– use * for multiplication and / for division
– integer division truncates remainder
7 / 5 evaluates to 1
– modulus operator returns the remainder
7 % 5 evaluates to 2

• Operator precedence
– some arithmetic operators act before others (i.e., multiplication
before addition)
• be sure to use parenthesis when needed
– Example: Find the average of three variables a, b and c
• do not use: a + b + c / 3
• use: (a + b + c ) / 3

© 2000 Deitel & Associates, Inc. All rights reserved.


Arithmetic (II)
• Arithmetic operators:

• Rules of operator precedence:

© 2000 Deitel & Associates, Inc. All rights reserved.


Decision Making: Equality and Relational
Operators
• if structure - decision based on truth or falsity of
condition. If condition met execute, otherwise ignore.
• Equality and relational operators:

_>

_<

=
• These operators all have lower precedence than arithmetic
operators

© 2000 Deitel & Associates, Inc. All rights reserved.


using statement
• using statements
– eliminate the need to use the std:: prefix
– allow us to write cout instead of std::cout
– at the top of the program write
using std::cout;
using std::cin;
using std::endl;
to use those functions without writing std::

© 2000 Deitel & Associates, Inc. All rights reserved.


1 // Fig. 1.14: fig01_14.cpp
2 // Using if statements, relational Outline
3 // operators, and equality operators
4 #include <iostream>
5 1. Load <iostream>
6 using std::cout; // program uses cout
7 using std::cin; // program Notice the using statements
uses cin
8 using std::endl; // program uses endl 2. main
9
10 int main()
2.1 Initialize num1 and
11 {
12 int num1, num2;
num2
13 2.1.1 Input data
14 cout << "Enter two integers, and I will tell you\
n" 15 << "the relationships they satisfy: ";
16 cin >> num1 >> num2; // integers,
read two integers 2.2 if statements
Enter two and I will tell you
17
the relationships they satisfy: 3 7
18 if ( num1 == num2 )
19 cout << num1 << " is equal to " << num2 <<
endl; 20
The if statements test the truth
21 if ( num1 != num2 ) of the condition. If it is true,
22 cout << num1 << " is not equal to " << 3num2 body of if
<<
is not statement
equal to 7 is
endl; 23 executed. If not, body is
24 if ( num1 < num2 ) skipped.
25 cout << num1 << " is less than " << num2 3 <<
is less than 7
endl; 26 To include multiple statements
27 if ( num1 > num2 ) in a body, delineate them with
28 cout << num1 << " is greater than " << num2 braces<<{}
endl; 29
30 if ( num1 <= num2 )
31 cout << num1 << " is less than or equal3to is"less than or equal to 7
32 << num2 << endl;
© 2000 Deitel33
& Associates, Inc. All rights reserved.
34 if ( num1 >= num2 )

35 cout << num1 << " is greater than or equal to Outline


"
36 << num2 << endl;

37
2.3 exit (return 0)
38 return 0; // indicate that program ended
successfully
39 }

Enter two integers, and I will tell you Program Output


the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7

Enter two integers, and I will tell you


the relationships they satisfy: 22 12
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12

Enter two integers, and I will tell you


the relationships they satisfy: 7 7
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7

© 2000 Deitel & Associates, Inc. All rights reserved.


Thinking About Objects: Introduction to Object
Technology and the Unified Modeling Language

• Object orientation
– natural way to think about world and writing computer programs
– Attributes - properties of objects
• size, shape, color, weight, etc.
– Behaviors - actions
• a ball rolls, bounces, inflates and deflates
• objects can perform actions as well
– inheritance
• new classes of objects absorb characteristics of existing classes
– information hiding
• objects usually do not know how other objects are implemented
• Abstraction - view the big picture
– See a photograph rather than a group of colored dots
– Think in terms of houses, not bricks

© 2000 Deitel & Associates, Inc. All rights reserved.


Thinking About Objects: Introduction to Object
Technology and the Unified Modeling Language
(II)
• class - unit of programming
– "blueprint" of the objects
• objects are created from the class
– contain functions
• implement behaviors
– contain data
• implement attributes
– Classes are reusable
• Unified Modeling Language (UML)
– used to model object-oriented systems and aid with their design
– complex, feature-rich graphical language

© 2000 Deitel & Associates, Inc. All rights reserved.

You might also like