Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

OOP Course Al-Balqa

Applied Unviversity

WHAT IS OBJECT-ORIENTED
PROGRAMMING?

1. Object-orientation is a natural way of thinking about the world and of


writing computer programs.
a) Objects are all around us--people, animals, plants, cars,
planes, buildings, computers, etc.
b) Abstractions allow us to view screen images as objects
such as people, planes, trees, etc. rather than as individual
dots of color. Abstractions allow us to think in terms of
beaches rather than grains of sand, houses rather than
bricks.

2. All objects have attributes such as size, shape, color, weight, etc.

3. All objects exhibit various behaviors. A baby cries, sleeps, crawls,


walks; a car accelerates, brakes, turns, etc.

4. Humans learn about objects by studying their attributes and observing


their behaviors.

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.

6. Object-oriented programming (OOP) models real-world objects


with software counterparts.
a) It takes advantage of class relationships where objects of
a certain class, such as a class of vehicles, have the same
characteristics.
b) It takes advantage of inheritance relationships, and even
multiple inheritance relationships, where newly created
classes are derived by inheriting characteristics of existing
classes, yet contain unique characteristics of their own.

1
OOP Course Al-Balqa
Applied Unviversity

i) Objects of class convertible have the


characteristics of class automobile, but the
roof goes up and down.

2
OOP Course Al-Balqa
Applied Unviversity

WHAT IS OBJECT-ORIENTED PROGRAMMING? (CONTINUED)

7. Object-oriented programming provides an intuitive way to view the


programming process, namely by modeling real-world objects, their
attributes, and their behaviors.

8. OOP also models communication between objects via messages, i.e.


objects communicate via messages.

9. OOP encapsulates data (attributes) and functions (behaviors) into


objects; the data and functions of an object are intimately tied together.

10. Objects have the property of information hiding. Although objects


may know how to communicate with one another across well-defined
interfaces, objects normally are not allowed to know implementation
details of other objects.
a) Implementation details are hidden within the objects
themselves.
i) It is possible to drive a car effectively
without knowing the details of how engines,
transmissions and exhaust systems work
internally.
b) Information hiding is crucial to good software
engineering.

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

WHAT IS OBJECT-ORIENTED PROGRAMMING? (CONTINUED)

11. Comparison of C and C++:


a) In C and other procedural programming languages,
programming tends to be action-oriented.
i) Data is important in C, but the view is that
data exists primarily in support of the actions
that functions perform.
ii) The unit of programming is the function.
iii) The verbs in a system specification help the
programmer determine the set of functions
that work together to implement the system.
b) In C++, programmers concentrate on creating their own
user-defined types called classes.
i) In C++, the unit of programming is the class
from which objects are eventually
instantiated (i.e. created).
ii) Whereas an instance of a built-in type e.g.
int is called a variable, an instance of a user-
defined type (i.e. a class) is called an object.
a) The programmer uses built-in
types as the building blocks for
constructing user-defined types.
b) The focus of attention in C++ is
objects rather than functions.
The nouns in a system
specification help the C++
programmer determine the set
of classes from which objects
will be created that will work
together to implement the
system.
iii) Each class contains data as well as the set of
functions that manipulate the data.
a) The data components of a class
are called data members.

4
OOP Course Al-Balqa
Applied Unviversity

b) The function components of a


class are called member
functions.

5
OOP Course Al-Balqa
Applied Unviversity

WHAT IS C++?

1. Three fundamental ways to view C++

a) As a better C.

b) As a language which supports data abstraction.


i) Ability to add to a language user-defined
data types that describe abstract entities.
ii) Examples:
a) Complex number
b) Date
c) Deck of cards
d) Group of people
e) An airplane

c) As a language which supports object-oriented


programming.
i) OOP is an extension of data abstraction.
ii) Allows programmer to define new abstract
data types which derive properties from
existing data types (i.e. inheritance).

2. Prominent new features of C++


a) Classes and data abstraction
b) Member variables and member functions
c) Operator overloading and function name overloading
d) Inheritance and derived classes
e) Virtual functions and polymorphism
f) Stream input/output
g) Templates
h) Class libraries
i) Exception handling

6
OOP Course Al-Balqa
Applied Unviversity

COMPUTER SYSTEMS

1. Software - a collection of programs.

2. Hardware - the actual physical machines that make up a computer


installation.

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

the data item when


it is needed.
c) Often, several of these bytes are
grouped together to form a
larger memory location.
(1) One common
grouping of bytes
is called a word
which is typically
two bytes.
(2) The address of the
first byte is used as
the address of this
larger memory
location.

8
OOP Course Al-Balqa
Applied Unviversity

COMPUTER SYSTEMS (CONTINUED)

e) Secondary memory (aka secondary storage, auxiliary


memory, auxiliary storage, external memory, and external
storage).
i) Used to hold data that will stay in the
computer before and/or after the program is
run.
a) Stored in units called files.
b) Different kinds of secondary
memory are:
(1) Hard disks.
(2) Diskettes (aka
floppy disks).
(3) Tapes.
(4) CD-ROM.

4. Compiler - a program that translates a program written in a high-level


language like C++ into a program written in the machine language
which the computer can directly understand and execute.

9
OOP Course Al-Balqa
Applied Unviversity

PROGRAMMING AND PROBLEM-SOLVING

1. Algorithm - a sequence of precise instructions that leads to a solution.


a) Algorithms can be written in English or in a programming
language.
b) The algorithm should be designed before writing the
program.

2. Phases of the software life cycle:


a) Analysis and specification of the task (problem
definition).
b) Design of the software (algorithm design).
c) Implementation (coding).
d) Testing.
e) Maintenance and evolution of the system.
f) Obsolescence.

3. Types of programming errors:


a) Syntax errors.
i) Violation of the grammar rules of the
programming language.
ii) Typically discovered by the compiler during
compilation.
b) Run-time errors.
i) Occur when the program is running.
ii) Can cause the system to "hang."
iii) Example of a run-time error is division by
zero.
c) Logic errors.
i) Mistakes in the underlying algorithm or in
translating the algorithm into the C++
language.
ii) Hardest to diagnose.
iii) May not become evident until later in the
program life cycle.

10
OOP Course Al-Balqa
Applied Unviversity

LANGUAGE LEVELS AND PROGRAMMING ENVIRONMENT

1. Levels:

a) High level - resembles the way humans think and speak.


Must undergo some sort of translation to machine
language to be executed. Business and scientific
applications are usually programmed in a high-level
language to facilitate portability from computer to
computer.

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

c) Low level - relates directly to the underlying hardware.


Used to write operating systems and monitors. Assembly
language is a low-level language.

2. C++ is an intermediate- to high-level language. It can be used to write


programs from business applications to operating systems. It can be
compact and fast in comparison with other high-level languages.

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:

 Provide internal documentation to help one programmer read another's


program.
 Help a programmer understand his or her own program, especially during
program implementation.
 Explain certain code fragments or the reason an object is declared as part of a
program.
 Indicate the programmer's name and the goal of the program.
 Separate and identify the purpose of individual modules (functions) in the
program.
 Help to debug a program.

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
*/.

/* Update Customer Record */


/*
A comment may extend
over many lines
*/

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:

cout << expression-1 << expression-2... << expression-N ;

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++.
//

#include <iostream.h> // preprocessor directive

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:

cin >> object-1 >> object-2... >> object-N ;

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;

cout << "Enter an integer value: " ;


cin >> intvar; // enter an integer value
cout << endl << "The value you entered was " << intvar << endl;
return 0;
}

16
OOP Course Al-Balqa
Applied Unviversity

BORLAND 4.5 C++ EDITOR, COMPILER AND DEBUGGER

Setting up the environment:


1. Within Window's File Manager, build a subdirectory in which you will
store all of the source code for the project.
2. Launch Borland 4.5 C++.
3. Project New (If the project has already been built in a prior session,
select Project Open and choose the project name. Your environment
will be ready for you and you can either edit your program or test and
debug it.)
a) Project path and name - Enter the project name (with an
.ide extension) and directory. This name will eventually
be the name of your .exe file. The directory should be the
same as where your source files are (or will be) located.
b) Select EasyWin as your Target Type.
c) Click on OK.
4. You will see a project window open at the bottom of your screen.
a) Position the cursor on the .exe file name and right click.
b) Click on Add Node and enter your source file name that
you want included in the project. Repeat for all .cpp
source files in the project. Do not enter any .h files into
the project. (They will automatically be included via the
#include statement(s) in the .cpp files.)
c) Right click on any remaining files listed under the .exe file
in the project window and select Delete Node. You will
not be using the files that were automatically listed in the
project.
5. Options Project
a) Enter into the source, intermediate, and final fields the
directory where your project and source files are located
(remember to keep everything in the same directory).
b) Click on OK

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

BORLAND 4.5 C++ EDITOR, COMPILER AND DEBUGGER


(CONTINUED)

Compiling, linking and running programs in the project:


1. There are two ways to compile and link a project.
a) Project Make All -- recompiles only the most recently
changed source files.
b) Project Build all -- recompiles all source files. Use this
option when recompiling on a different PC for the first
time, switching between DOS and EasyWin target
modules or if you think a project should compile OK but
is not compiling.
2. Debug Run (cntl-F9) or the lightning bolt icon
a) Runs the project.
b) At the end of execution, click on the box in the top left
corner of your display window to close the window and
return to the project development environment.

Debugging the project:


1. F8 steps through the program. Does not step into functions.
2. F7 steps through the program. Steps into functions.
3. To set a breakpoint:
a) Put cursor on source code line for breakpoint.
b) Hit F5.
c) To toggle the breakpoint off, repeat the two steps above.
4. Debug Run (or cntl-F9) will run the program to the breakpoint line and
stop. Repeat Debug Run (or cntl-F9) to run to the next breakpoint (or
end of program if no more breakpoints exist.)
5. To view contents of the output screen:
a) Click on the top left corner box and switch to the .exe
window.
6. Debug submenu:
a) Inspect and Evaluate/Modify allow you to inspect the
contents of program variables. Experiment with both
submenu options.
b) Add Watch allows you to set up a watch window which is
always visible. You can specify what variables you want
to watch and then see them change as you step through a
program. A watch window slows execution down

19
OOP Course Al-Balqa
Applied Unviversity

somewhat, so you may want to use the Inspect and


Evaluate/Modify options to inspect variables only when
needed.

7. Before exiting Borland C++, select Project Close Project

20
OOP Course Al-Balqa
Applied Unviversity

EDITING, COMPILING, LINKING AND RUNNING C++ PROGRAMS IN


UNIX

1. Connecting into Unix:


a) From home:
i) Follow the instructions issued with your
account to connect to heart.
ii) Type the command: rsh
linux???.cecs.csulb.edu (replace the ??? with
the IP number of a machine in your lab).
iii) Login and password.
iv) Type logout to exit the linux server.
v) Type logout to exit heart.
b) From school:
i) Login and password.
ii) Hit <enter> to accept VT100.
iii) Type the following command to start X
Windows: startx
iv) Use the middle mouse key to resize windows
if needed. For two-button mice, press both
the left and right buttons simultaneously to
simulate the middle mouse button.
v) To exit X Windows, type exit in the xterm
windows first and then type exit in the
console window.
vi) Type logout to exit Linux.

2. Editing source files


a) Use either the vi editor (can be difficult to use) or pico
(easy to use).
b) Save the source file with a .cpp or .h extension.

3. Compiling and linking programs (done in one step)


a) For one source file, enter g++ filename.
b) For multiple source files, enter g++ filename1 filename2
filename_etc.
c) To explicitly link in a library such as the math library,
enter g++ filename -lm.

21
OOP Course Al-Balqa
Applied Unviversity

d) Executable file will automatically be called a.out unless


you use the option -o output.

4. Running the program


a) Enter a.out (or the output filename if you used the -o
option)

22
OOP Course Al-Balqa
Applied Unviversity

VISUAL C++ 4.0

1) Start the Microsoft Visual C++ Developer Studio.

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.

6) Click on Build | Build proj1.exe. Correct any compiler errors and if


necessary, build the project again.

7) To run your program, select Build | Execute proj1.exe.

8) To run the debugger, select Build | Debug.


a) The submenu option Go executes the program from start to finish.
b) To use the debug options, select the submenu option Step Into. You
will now see a main menu option called Debug.
i) F11 steps into functions.
ii) F10 steps over functions.
iii) Shift + F11 steps out.
c) The output screen can be seen by selecting the window from the screen
bottom list of open windows.
d) You can also set up breakpoints (via the Edit menu) and view the
contents of variables.

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

VISUAL C++ 5.0

1) Start the Microsoft Visual C++ Developer Studio.

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.

5) To compile your project, click on Build | Build proj1.exe. Correct any


compiler errors and if necessary, build the project again.

6) To run your program, select Build | Execute proj1.exe.

7) To run the debugger, select Build | Start Debug.


a) The submenu option Go executes the program from start to finish.
b) To use the debug options, select the submenu option Step Into. You
will now see a main menu option called Debug.
i) F11 steps into functions.
ii) F10 steps over functions.
iii) Shift + F11 steps out.
c) The output screen can be seen by selecting the window from the screen
bottom list of open windows.

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

VISUAL C++ 6.0

1) Start the Microsoft Visual C++ Developer Studio.

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.

5) To compile your project, click on Build | Build proj1.exe. Correct any


compiler errors and if necessary, build the project again.

6) To run your program, select Build | Execute proj1.exe.

7) To run the debugger, select Build | Start Debug.


a) The submenu option Go executes the program from start to finish.
b) To use the debug options, select the submenu option Step Into. You
will now see a main menu option called Debug.
i) F11 steps into functions.
ii) F10 steps over functions.
iii) Shift + F11 steps out.
c) The output screen can be seen by selecting the window from the screen
bottom list of open windows.

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

Barkakati, Nabajyoti, Borland C++ 4 Developer's Guide, Indianapolis, Indiana:


SAMS Publishing, 1994.

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.

Foster, L.S., C by Discovery, Second Edition, El Granada, CA: Scott/Jones Inc.,


Publishers, 1994.

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

You might also like