Professional Documents
Culture Documents
02-What Is OOP
02-What Is OOP
Applied Unviversity
WHAT IS OBJECT-ORIENTED
PROGRAMMING?
2. All objects have attributes such as size, shape, color, weight, etc.
5. Different objects can have many of the same attributes and exhibit
similar behaviors.
a) Comparisons can be made between babies and adults, and
between humans and chimpanzees.
b) Cars, trucks, little red wagons, and roller blades have
much in common.
1
OOP Course Al-Balqa
Applied Unviversity
2
OOP Course Al-Balqa
Applied Unviversity
int main()
{
Method instance i1;
1 i1.Method1();
i1.Method2();
Method Data Method i1.Method3();
4 2 return 0;
}
Method
3
instance
i1
3
OOP Course Al-Balqa
Applied Unviversity
4
OOP Course Al-Balqa
Applied Unviversity
5
OOP Course Al-Balqa
Applied Unviversity
WHAT IS C++?
a) As a better C.
6
OOP Course Al-Balqa
Applied Unviversity
COMPUTER SYSTEMS
3. Components of a computer:
a) Input device(s) - Any device that allows a person to
communicate information to the computer.
i) Keyboard.
ii) Mouse.
b) Output device(s) - Any device that allows the computer
to communicate information to the user.
i) Video display screen (aka monitor).
ii) Printer.
c) Central processing unit (CPU) - the "brain" of the
computer.
i) Executes the instructions in a program.
ii) Performs the calculations specified by the
program.
d) Main memory.
i) Used only while the program is running.
ii) Divided into a series of numbered locations
called bytes.
a) A byte consists of eight binary
digits, each either a zero or one.
A digit that can only be zero or
one is called a bit.
b) The number associated with one
of these bytes is called the
address of the byte.
(1) A data item, such
as a number or a
letter, can be
stored in one of
these bytes.
(2) The address of the
byte is used to find
7
OOP Course Al-Balqa
Applied Unviversity
8
OOP Course Al-Balqa
Applied Unviversity
9
OOP Course Al-Balqa
Applied Unviversity
10
OOP Course Al-Balqa
Applied Unviversity
1. Levels:
i) Methods of translation:
a) Interpretation - interpreter
translates each statement into
machine language and executes
it before processing the next
statement.
b) Compilation and linking -
compiler translates program into
machine language.
(1) Preprocessing -
strips comments,
expands macros
and handles all the
preprocessor
directives (e.g.
constant
definitions,
inclusion of other
source code files,
and defining
macros).
(2) Translation -
translates the
source code to
assembly language.
(3) Optimization -
makes the
11
OOP Course Al-Balqa
Applied Unviversity
assembly language
more efficient.
(4) Assembly -
translates from
assembly language
to machine
language.
(5) Link editing -
brings in the
necessary parts of
other libraries or
object code and
creates an
executable file.
b) Intermediate level
12
OOP Course Al-Balqa
Applied Unviversity
C++ IMPROVEMENTS TO C
1. Comment style
2. Stream input and output
3. Avoiding the preprocessor
a) Using inline functions instead of preprocessor macros
b) Using const rather than #define
4. Declarations
5. Function prototypes
6. Reference declarations and call-by-reference
7. Default arguments
8. Overloading functions
9. Free store operators "new" and "delete"
10. Structs and classes
a) Member variables and member functions
b) Encapsulation
c) Public and private members
d) Scope resolution operator
e) Constructors
i) Default
ii) Conversion
iii) Copy
f) Destructors
g) Initializer lists
h) The "this" pointer
i) The assignment operation
11. Overloading operators
12. Friend functions
13
OOP Course Al-Balqa
Applied Unviversity
COMMENT STYLE
Comments are portions of text that annotate a program. They fulfill any or all of the
following functions:
Comments may be added anywhere throughout the program, including to the right of
any C or C++ statement, on a separate line, or over several lines. They may begin
with the two-character special symbol /* and close with the corresponding symbol
*/.
An alternate form for comments is to use the // before the text (available in C++
only). Such a comment may appear on a line by itself or at the end of a line:
// -------------------------------------------------------------
// This is a complete C++ program that does nothing
// -------------------------------------------------------------
int main( )
{ // These types of comments terminate at the end of the line
float distance; // Store travel to the nearest tenth of a kilometer
// ... Indicates additional statements
return 0; // Tell the operating system everything is okay
14
OOP Course Al-Balqa
Applied Unviversity
}
OUTPUT IN C++
C++'s output statements use cout (common output) and the operator << (known as
the put to or insertion operator) to generate screen output. The general form is
given as:
where cout represents the computer screen object and expression-1 through
expression-N may take the form of int or float objects, string constants, etc.
// output.cpp
//
// Synopsis - Displays a message on the terminal screen.
//
// Objective - Illustrate the cout statement for output in C++.
//
int main( )
{
cout << "Welcome to C++" << endl; // output statement
return 0;
}
15
OOP Course Al-Balqa
Applied Unviversity
INPUT IN C++
C++'s input statements use cin (common input) and the operator >> (known as the
get from or extraction operator) to generate screen output. The general form is
given as:
The cin and cout objects are examples of streams--abstractions referring to the
flow of data from some source to some destination. Characters are inserted into
output streams and extracted from input streams.
/* input1.cpp
*
* Synopsis - A variable of type int is declared. Its address
* is output. The program then prompts for and
* accepts input of an integer value. The input
* value is echoed to the terminal screen.
*
* Objective - Illustrates input of an integer value with cin.
*/
#include <iostream.h>
int main( )
{
int intvar;
cout << "The address of intvar is " << &intvar << endl;
16
OOP Course Al-Balqa
Applied Unviversity
Typing in programs:
1. File New
a) Type in your source code.
b) Use Help for explanation of edit keys.
c) When done, File Save and type in file name.
17
OOP Course Al-Balqa
Applied Unviversity
d) Repeat the File New process for each source file. You
can have more than one source file window open at a
time.
18
OOP Course Al-Balqa
Applied Unviversity
19
OOP Course Al-Balqa
Applied Unviversity
20
OOP Course Al-Balqa
Applied Unviversity
21
OOP Course Al-Balqa
Applied Unviversity
22
OOP Course Al-Balqa
Applied Unviversity
2) Select File | New | Project Workspace. Use the browse button to position to
the Visual_C directory. Choose type "Console Application" and name the
project, e.g. proj1. Accept all other default settings. You do not need to add
the extension .mdp to the project name (Visual C++ will do it). Click the
Create button. You will create a proj1 subdirectory as well as the proj1.mdp
project file within the subdirectory.
3) When you first create the project, there probably won't be any source code
files to put into it yet. However, if you do have source code files, you can
add them with the Insert | Files into Project menu option.
4) Select File | New | Text File. Type in your source code and save it, e.g. save
it as Hello.cpp in the proj1 directory.
5) Select Insert | Files into Project. Select the file(s) you want to add into the
project and click on the Add button. Note: You can add multiple files to the
project. Only add the .c or .cpp files. Do not put header (.h) files into the
project.
23
OOP Course Al-Balqa
Applied Unviversity
9) To close the project, click File | Close Workspace. After a project has been
built, you can open it by selecting File | Open Workspace and selecting the
.mdp file (the project file) in the directory in which the project was originally
created.
24
OOP Course Al-Balqa
Applied Unviversity
2) Select File | New and click the Projects tab. Click on Win32 Console
Application and in the Location textbox position to the directory in which
you'll create the project. In the Project Name textbox type the name of the
project, e.g. proj1 and set the radio button to "create new workspace."
Accept all other default settings. You do not need to add the extension .dsw
to the project name (Visual C++ will do it). Click OK. You will create a
proj1 subdirectory as well as the proj1.dsw project workspace file within the
subdirectory.
3) To create source code files and insert them into the project, select File | New,
click the Files tab and select the file type. Specify a file name, a directory
location (should be the same directory as the one in which you created your
project) and the project to which you want to add the file. Click OK to create
a new file of the chosen type. Type in your source code and save it, e.g. save
it as Hello.cpp in the proj1 directory.
4) If you need to add files to the project at a later date, select Project | Add to
Project | New and click on the Files tab. You will see a window similar to the
window from the step above.
25
OOP Course Al-Balqa
Applied Unviversity
d) You can also set up breakpoints (via the Edit menu) and view the
contents of variables.
e) Use the Debug | QuickWatch option to inspect the contents of
variables.
8) To close the project, click File | Close Workspace. After a project has been
built, you can open it by selecting File | Open Workspace and selecting the
.dsw file (the project workspace file) in the directory in which the project was
originally created.
26
OOP Course Al-Balqa
Applied Unviversity
2) Select File | New and click the Projects tab. Click on Win32 Console
Application and in the Location textbox position to the directory in which you
want to create the project. In the Project Name textbox type the name of the
project, e.g. proj1 and set the radio button to "create new workspace."
Accept all other default settings. You do not need to add the extension .dsw
to the project name (Visual C++ will do it). Click OK. In the window that
follows, set the radio button to "An empty project." Click the Finish button
and then the OK button. You will create a proj1 subdirectory as well as the
proj1.dsw project workspace file within the subdirectory.
3) To create source code files and insert them into the project, select File | New,
click the Files tab and select the file type. Specify a file name, a directory
location (should be the same directory as the one in which you created your
project) and the project to which you want to add the file. Click OK to create
a new file of the chosen type. Type in your source code and save it, e.g. save
it as Hello.cpp in the proj1 directory.
4) If you need to add files to the project at a later date, select Project | Add to
Project | New and click on the Files tab. You will see a window similar to the
window from the step above.
27
OOP Course Al-Balqa
Applied Unviversity
d) You can also set up breakpoints (via the Edit menu or the hand icon or
F9) and view the contents of variables.
e) Use the Debug | QuickWatch option to inspect the contents of
variables. You can also see auto and local variables and "this" in the
lower panels of your screen.
8) To close the project, click File | Close Workspace. After a project has been
built, you can open it by selecting File | Open Workspace and selecting the
.dsw file (the project workspace file) in the directory in which the project was
originally created.
28
OOP Course Al-Balqa
Applied Unviversity
BIBLIOGRAPHY
Cantu, Marco and Steve Tendon, Borland C++ 4.0 Object-Oriented Programming,
New York: Random House, 1994.
Deitel, H.M. and P.J. Deitel, C++ How to Program, Second Edition, Eaglewood
Cliffs, New Jersey: Prentice Hall, 1998.
Gorlen, Keith E., Sanford M. Orlow, and Perry S. Plexico, Data Abstraction and
Object-Oriented Programming in C++, New York: John Wiley & Sons, 1991.
Horstmann, Cay S., Mastering Object-Oriented Design in C++, New York, NY:
John Wiley & Sons, Inc., 1995.
Savitch, Walter, Problem Solving with C++, Second Edition, Menlo Park, Ca.:
Addison-Wesley Publishing Company, Inc., 1999.
Swan, Tom, Mastering Borland C++ 4.5, Second Edition, Indianapolis, Indiana:
SAMS, 1995.
29