Topics:
Problem Solving and Software Engineering
ADTs
Classes and Objects
Software
Engineering - provides techniques to facilitate the development of computer
programs. Emphasis is placed on issues of efficiency, reliability, and
reusability of the code.
Software
development has many phases.
Life
Cycle of Software (9 main phases)
1.
Specification
2.
Design
3.
Risk Analysis
4.
Verification
5.
Coding
6.
Testing
7.
Refining
8.
Production
9.
Maintenance
Problem Solving in Computer Science - process of taking a problem
and developing a computer program that solves the problem. Solution
generally involves many phases. Top-down design process and step-wise
refinement are important concepts.
Solution
involves two main components: Algorithms & Data Structures. There is a book entitled:
Programming
= Algorithms + Data Structures
Algorithm
- Finite, step-by-step method for solving a problem, i.e., a recipe
Data
Structure - Construct to store a collection of data together with a set of
operations, i.e., implements an ADT (Abstract Data Type).
An
ADT is simply an abstract collection or set of elements and a collection of
well defined operations on the set.
C++
uses classes and objects to implement ADTs.
Chapter
3 (ADT's)
Examples
1: Integer ADT
Data
Attributes: whole numbers
Operations:
Add(), Subtract(), Multiply(), Divide()
Example
2: Rational ADT
Data
Attributes: Pairs of whole numbers (a/b)
Operations:
Constructors
– default, specific, copy
Arithmetic
-- Add(), Subtract(), Multiply(), Divide()
Relational
– Lessthan(),
Equal()
I/O: Extract(), Insert()
Mutators: SetNum(), Set Denom()
Inspectors:
GetNum(),
GetDenom()
To compile and run the code --do the following
> g++ -c rational.cpp
> g++ ratclient.cpp rational.o
> ./a.out
Enter
rational number (a/b): 1/2
Enter
rational number (a/b): 2/3
1/2 +
2/3 = 7/6
1/2 *
2/3 = 2/6
Example
3: Stack ADT
Data Attibutes: list items (where item is some data type)
Operations:
1.
Create empty stack
2.
Destroy stack
3.
Determine whether stack is empty
4.
Add new item (Push)
5.
Remove item (Pop)
6.
Retrieve item
An
important ADT that occurs often in practice is the list ADT. The operations for the list ADT may vary
depending on the application.
Typical
list operations include:
1.
Create empty list
2.
Destroy list
3.
Determine list stack is empty
4.
Determine size of list
5.
Insert item at a given position in list
6.
Delete an item at a given position in list
7.
Retrieve an item from a given position in list
8.
Sort the list
Implementing
Data Structure in C++
C++
is on object oriented language. We will
use this feature when programming data structures. Thus, we will create the
user-defined data structure class, e.g., list class, stack class, queue class,
graph class, etc.
Program that use these class will define objects from the classes an utilize them in the rogram.
Four Major Components of programming a user-defined class and its
associated objects.
Defining
and implementing class
1.
Class Interface - class definition
2.
Class Implementation - code for operations, i.e., member functions
Defining
and utilizing objects of class
3.
Definition (Declaration) of Objects
4.
Utilization of Object in Program
See
code samples for full Rational Class Implementation.