Introduction To C++ Data Abstraction W/ Classes: Topic #2
Introduction To C++ Data Abstraction W/ Classes: Topic #2
Data Abstraction w/
Classes
Topic #2
CS202 2- 1
Members of a Class
The
CS202 2- 2
Programming Paradigms
abstraction
modular abstraction
data abstraction
object oriented programming (this is
discussed later, once we learn about the
concept of inheritance)
CS202 2- 3
Procedural Abstraction
This is where you build a fence
around program segments, preventing
some parts of the program from
seeing how tasks are being
accomplished.
Any use of globals causes side effects
that may not be predictable, reducing
the viability of procedural abstraction
CS202 2- 4
Modular Abstraction
With modular abstraction, we build a
screen surrounding the internal
structure of our program prohibiting
programmers from accessing the data
except through specified functions.
Many times data structures (e.g.,
structures) common to a module are
placed in a header files along with
prototypes (allows external references)
CS202 2- 5
Modular Abstraction
The corresponding functions that
manipulate the data are then placed in
an implementation file.
Modules (files) can be compiled
separately, allowing users access only
to the object (.o) files
We progress one small step toward
OOP by thinking about the actions that
need to take place on data...
CS202 2- 6
Modular Abstraction
We implement modular abstraction by
separating out various
functions/structures/classes into
multiple .c and .h files.
.c files contain the implementation of
our functions
.h files contain the prototypes, class and
structure definitions.
CS202 2- 7
Modular Abstraction
myfile.h
(Notice the double quotes!)
main.c myfile.c
(Notice no .h file is listed on the above line)
CS202 2- 8
Data Abstraction
Data Abstraction is one of the most
powerful programming paradigms
It allows us to create our own user
defined data types (using the class
construct) and
then
CS202 2- 9
Data Abstraction
With data abstraction we think about
what operations can be performed on a
particular type of data and not how it
does it
Here we are one step closer to object
oriented programming
CS202 2- 10
Data Abstraction
Data abstraction is used as a tool to
increase the modularity of a program
It is used to build walls between a
program and its data structures
what
is a data structure?
talk about some examples of data
structures
CS202 2- 11
Data Abstraction
CS202 2- 12
Data Abstraction
An abstract data type is any type you
want to add to the language over and
above the fundamental types
For example, you might want to add a
new type called: list
which
CS202
Data Abstraction
Once defined, we can create lists
without worrying about how the data is
stored
We hide the data structure used for
the data within the data type -- so it is
transparent to the program using the
data type
We call the program using this new
data type: the client program (or client)
CS202 2- 14
Data Abstraction
Once we have defined what data and
operations make sense for a new data
type, we can define them using the class
construct in C++
Once you have defined a class, you can
create as many instances of that class as
you want
Each instance of the class is
considered to be an object (variable)
CS202 2- 15
Data Abstraction
an object as a variable
CS202 2- 16
What is a Class?
Remember, we used a structure to group
different types of data together under a
common name
With a class, we can go the next step an
actually define a new data type
CS202 2- 17
What is a Class?
First, lets talk about some terminology
Think of a class as the same as a data type
Think of an object as the same as a variable
CS202 2- 18
CS202 2- 19
Defining a Class...
Once we have decided on how the new type of
data should behave, we are ready to define a
class:
class data_type_name {
public:
//operations go here
private:
//memory is reserved here
};
CS202 2- 22
CS202 2- 25
my_str;
vs.
int i;
CS202 2- 26
CS202 2- 27
CS202 2- 28
Limitations...
CS202 2- 29
Limitations...
CS202 2- 30
List Example
List Example
CS202 2- 33
List Example
video out_of_site;
cin.get(out_of_site.title,100,\n);
cin.ignore(100,\n);
home_videos.add(out_of_site);
CS202 2- 34
//use operation
Introduction to C++
Data Hiding
and
Member Functions
CS202 2- 35
CS202 2- 36
class Terminology
Class
think
data type
Object
instance
Members
like
class Terminology
int i;
CS202 2- 38
class Terminology
CS202 2- 39
class Terminology
CS202 2- 40
class Terminology
CS202 2- 41
object.category object.quantity
class Terminology
so,
CS202 2- 42
Structure Example
CS202 2- 43
class Terminology
Data Hiding
functions
clients (e.g., main program) cant muck
with the data directly
this is done by placing the data members in
the private section
and, placing member functions to access &
modify that data in the public section
CS202 2- 45
Data Hiding
Data Hiding
Data Hiding
CS202 2- 48
class Terminology
private:
video my_list[CONST_SIZE];
int num_of_videos;
};
CS202 2- 49
Data Hiding
CS202 2- 50
Where to place....
In reality, the previous example was
misleading. We dont place the
implementation of functions with this
this class interface
Instead, we place them in the class
implementation, and separate this into its
own file
CS202 2- 51
Class Interface:
list.h
class list {
public:
int display_all()
private:
video my_list[CONST_SIZE];
int num_of_videos;
};
CS202 2- 52
prototype statements
structure declarations and definitions
class interfaces and class declarations
include other files
Class Implementation
Class Implementation
list.c
#include list.h
notice the double quotes
int list::display_all() {
for (int i=0; i<num_of_videos; ++i)
cout <<my_list[i].title <<\t
<<my_list[i].category
<<\t <<my_list[i].quantity <<endl;
}
Notice, the code is the same
But, the function is prefaced with the class name and the scope
resolution operator!
This places the function in class scope even though it is implemented
in another file
Including the list.h file is a must
CS202 2- 53
Constructors
CS202 2- 54
Constructors
CS202 2- 55
Constructor
};
CS202 2- 56
Constructor
CS202 2- 57
struct video {
CS202 2- 58
char * title;
char category[5];
int quantity;
};
CS202 2- 59
CS202 2- 60
Default Constructor
Now, lets think about the
implementation.
First, what should the constructor do?
initialize
list::list() {
my_list = NULL;
video_list_size = 0;
num_of_videos = 0;
}
CS202 2- 61
Another Constructor
Remember function overloading? We
can have the same named function
occur (in the same scope) if the
argument lists are unique.
So, we can have another constructor
take in a value as an argument of the
number of videos
and
CS202 2- 62
2nd Constructor
list::list(int size) {
my_list = new video [size];
video_list_size = size;
num_of_videos = 0;
}
Default Arguments
CS202 2- 65
Destructor
Then, we can deallocate the memory
when the lifetime of a list object is over
When is that?
Luckily, when the clients object of the
list class lifetime is over (at the end of
the block in which it is defined) -- the
destructor is implicitly invoked
CS202 2- 66
Destructor
}
(Notice the ~ in front of the function name)
(It can take NO arguments and has NO return type)
(This too must be in the class interface....)
CS202 2- 67
Review of Classes
What is the difference between a class
and a struct
What is a data member?
Where should a data member be placed
in a class? (what section)
What is a member function?
Where should member functions be
placed, if clients should use them?
CS202 2- 68
Review of Classes
What is the difference between a
member function and a regular-old C++
function?
What is the purpose of the constructor?
Why is it important to implement a
constructor?
What is the difference between a class
and an object?
CS202 2- 69
Review of Classes
Show an example of how a client
program defines an object of a list class
How then would the client program call
the constructor? (trick question!)
How then would the client program call
the display_all function?
Why are parens needed?
CS202 2- 70
Review of Classes
CS202 2- 71
Review of Classes
constructor
a set member function, that takes an
integer as an argument and returns
nothing
a display member function
CS202 2- 72
Review of Classes
What happens if we forgot to put the
keyword public in the previous class
interface?
Why is it necessary to place the class
name, followed by the scope resolution
operator (::) when we implement a
member function outside of a class?
CS202 2- 73