Lectures 1&2
Lectures 1&2
• 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
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.
..
..
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.
•\
– 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)
4
1. Load <iostream>
5 int main()
6 { 2. main
7 std::cout << "Welcome ";
9
2.2 Print "to C++!"
10 return 0; // indicate that program ended
successfully
11 } 2.3 newline
4 1. Load <iostream>
5 int main()
2. main
6 {
Welcome
2.4 newline
to
C++!
2.5 newline
• 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
• = (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
integer1 45
• 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
_>
_<
=
• These operators all have lower precedence than arithmetic
operators
37
2.3 exit (return 0)
38 return 0; // indicate that program ended
successfully
39 }
• 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