Object Oriented Software Design
User defined data types vs. fundamental data types.
Why we should create our own types?
What resources OO languages offer to create objects of our own type ?
How can we maximize the reusability of software ? (Encapsulation and hiding)
What do we encapsulate ? (Abstraction)
What are member function and data members ?
How do we create (instantiate an object) ? Constructor functions
How do we call the members of an object ?
Object oriented programming
is essentially building a program around self-contained collections of data and
code to modify that data; this programming model is in contrast to a model that
uses function that act on data scattered throughout a program. Object-oriented
programming (or coding, as programming is commonly referred to) is an
organizational style, but it helps programmers create reusable code because the
code to do a specific thing is entirely contained within a single section of
code, and to use the code to perform tasks - for instance, creating a menu -
involves using only a small number of functions to access the internals of the
class. Think of it as a black box that can be easily carried from place to
place, and that perform complex actions simply at the press of a button: for
instance, a microwave lets you heat food for a specified time limit - say, two
minutes - by typing in the time and pressing the heat button. You do not need to
know how the microwave operates or why the physics works. In the same way that
self-contained appliances simplify life for the consumer, object-oriented
programming simplifies the transfer of source code from one program to another
program by encapsulating it - putting it all in one place.
C++ enables programmers to incorporate new types into the language, through the use of classes. A class is a user-defined type. The compiler can treat new types as if they are one of the built-in types. This is a very powerful feature. In addition, the class provides the mechanism for data abstraction and encapsulation, which are key to object-oriented programming. As we examine some of the new features of C++ we will see these two goals resurface again and again.
Through the use of classes, user-defined types may be created, and if properly defined, C++ will behave as if they are one of the built-in types: int, char, float, and double. It is possible to define a Vector type and perform operations such as addition and multiplication just as easily as is done with ints: Example
A Class is a programmer-defined data type out of which objects can be
created. Objects are created from classes; they have the same relationship
to classes as variables do to C++'s built-in data types.
For example, in the declaration int a; a is said to be a variable,
whereas in this declaration Date a; a is said to be an object.
Objects are also referred to as instances of a class and the process
of creating a new object is frequently referred to as an instantiation
of the object. Each time a new object is instantiated (created), a new set
of data members belonging to the object is created. The particular values
contained in these data members determine the object's state.
Central to the creation of objects is the concept of an abstract data
type, which is simply a user-defined data type, as opposed to the
built-in data types provided by all languages (such as integers, floats
and characters). An abstract data type is simply a user-defined type that
defines both a type of data and the operations that can be performed on
them. Such user-defined data types are required when we wish to create
objects that are more complex than simple integers and characters.
In C++, an abstract data type is referred to as a class. A class defines
both data and functions. This is usually accomplished by constructing a
class in two parts, consisting of a declaration section and an implementation
section.
Format of a Class Definition
Both the variables and functions listed in the class declaration section
are referred to as class members. Individually, the variables are referred
to as both data members and as instance variables. You reference these
data members in the same way that you reference the data members in a
structure: You qualify a data member's name with an instance of the class.
The functions are referred to as member functions, which typically act
on the data members. You invoke a member function by qualifying its name
with the instance of the class in the same way that you reference data
members. A member function name may not be the same as a data member
name.
Example of a Class