Chapter 12, Classes and Abstraction
We begin to move from procedural programming to object oriented programming.
What is functional decomposition?
Notice that this focuses on functions
Abstract data type (ADT): A data type whose properties (domain and operations) are specified independently of any particular implementation.
What are the domain of a data type?
What are the operations on a data type?
Think about strings:
Answer the two above questions.
Note, when we discuss functions, we discuss only flow of control
With perhaps a hint of data (parameters) thrown in.
The definition of ADT changes that.
This is a new way to think about things and it IS different from what you have learned so far.
From early times until the early 70's everything was functional decomposition/procedural programming
Early OO languages were adopted in the 70s
Stroustrap released c++ in 85.
There is still an incredibly large base of procedural code out there.
Just a side note, c++ is not an object oriented language. It is Multi-paradigm
procedural
imperative
functional
object-oriented
generic
modular
Building an ADT in C++
The
class
construct is the way objects/ADTs are built in c++
You will eventually discover that classes and structs are essentially the same thing.
But we tend to use structs as collections of data, a procedural programming idea.
We uses classes to implement ADT's.
Classes have
member data, the data associated with an object.
member function, the functions needed to manipulate and observe that data.
Protection: the ability to keep "programmers" from changing the data in unexpected ways.
Basic Syntax
class identifier { access modifier: member list access modifier: member list };
Access modifiers consist of
private
only instances of the class can access members listed here.
public
any code can access this method.
member lists consist of
variable declarations, with initialization if desired (since c++11)
function declarations
Unless marked otherwise, all members are private.
This is the only syntatic/semantic difference between a class and a struct.
Structs - everything is public.
Structs may have function and access modifiers.
Instances of a class are called
objects
There are a set of standard member functions
A
constructor
initializes the object.
A
destructor
cleans up after an object when it is destroyed.
A
observer
or
getter
examines an object.
A
transformer
or
setter
changes an object.
The methods or function members of a class determine the formal interface.
You need to provide sufficient methods to fully manipulate an instance of a class.
There is much more, but this is enough for a start.