`
bulote
  • 浏览: 1297108 次
文章分类
社区版块
存档分类
最新评论

Chapter 1. Why Object-Oriented Programming in C++

 
阅读更多
Object –Oriented Programming
Using C++

2005
Chapter 1. Why Object-Oriented
Programming in C++
• Object-Oriented Programming.
• Why C++ is a Better C.
• Encapsulation and Type Extensibility.
• Construction of Objects.
• Conversions, Operators, and Seamless
Types.
• Inheritance.
• Polymorphism.
• Benefits of Object-Oriented Programming.
Chapter 1. Why Object-Oriented
Programming in C++ ?
• Object-oriented programming (OOP) is the
programming methodology of choice in the
1990s.
• It is the product of 30
years of programming
practice and experience that goes back to
Simula 67 and continues with Smalltalk,
LISP, Clu, and more recently Actor, Eiffel,
Objective C, Java, and C++.
Chapter 1. Why Object-Oriented
Programming in C++ ?
• Two aspects of C++ are stressed.
– The first is its superiority as a general-purpose
programming language because of its new
features.
– The second is the success of C++ as an objectobjectoriented
programming language.
1.1 Object-Oriented Programming
• Object-oriented programming is a data-centered
view of programming, in which data and behavior
are strongly linked.
• OOP also views computation as simulating
behavior.
• We will be using the term abstract data type
(ADT)
to mean a user-defined extension to the
native types available in the language. It consists
of a set of values, and a collection of operations
that can act on those values.
1.1 Object-Oriented Programming
• Objects
O bj e c t s
are class variables. Object-oriented
programming allows ADTs to be easily
created and used. OOP uses the mechanism
of inheritance
i n h e r i t an c e
to conveniently derive a new
type from an existing user-defined type.
• In OOP, objects are responsible for their
behavior.
1.1 Object-Oriented Programming
• The new class construct in C++ provides the
encapsulation mechanism to implement
ADTs. Encapsulation includes both the
internal implementation details of a specific
type and the externally available operations
and functions that can act on objects of that
type.
1.1 Object-Oriented Programming
• OOP Concepts
– Simulating activity in the world
– Having user-defined types
– Hiding implementation detail
– Reusing code through inheritance
– Allowing run-time interpretation of function
call
1.2 An Example C++ Program
• Why C++ is a better C?
– C++ is a marriage of the low level and the high
level.
– C++ adds object-oriented features that are
designed to allow a programmer to create or
import a library appropriate to the problem
domain.
1.2 An Example C++ Program
• Demo on page 4: hello.cpp
– The IO library is found in the file iostream
or
iostream.h
.
– The library string
is part of the standard C++
library and must be included to use the
declaration string.
– The namespace
std is reserved for use with the
standard libraries.
– The
//
symbol is used as a rest-of-line comment
symbol.
1.2 An Example C++ Program
– The inline
modifier of the function is used to
tell the compiler to compile this function
without resorting to function call and return
instructions, if possible.
– The identifier cout
is defined in iostream
as the
standard output stream connected by most C++
systems to the screen for output.
1.2 An Example C++ Program
– The identifier endl
is a standard manipulator
that flushes the output buffer, printing
everything at that point while going to a new
line.
– The operator <<
is the put to output operator
writing out what comes after it to cout
.
• Demo on page 5: dinner.cpp
1.3 Encapsulation and Type
Extensibility
• OOP is a balanced approach to writing
software. Data and behavior are packaged
together. This encapsulation creates userdefined
types, extending and interacting
with the native types of the language.
• Type- extensibility
T y pe - e xt e n s i bi l i t y
is the ability to add userdefined
types to the language that are as
easy to use as native types.
1.3 Encapsulation and Type
Extensibility
• An abstract data type
abs t r ac t dat a t y pe
such is a description
of the ideal public behavior of the type.
• Operations concatenate and print are called
methods
m e t h ods
.
.
A concrete implementation of the
ADT also has implementation limits; These
limits affect public behavior.
1.3 Encapsulation and Type
Extensibility
• Encapsulation
E n c aps u l at i on
is the ability to hide internal
detail while providing a public interface to a
user-defined type. C++ uses class
and
struct
declarations in conjunction with
access keywords private, protected,
and
public
to provide encapsulation.
• Using
message
m e s age
and method
m e t h od
to replace the
traditional terms function invocation
f u n c t i on i n v oc at i on
and
member function.
m e m be r f u n c t i on .
1.3 Encapsulation and Type
Extensibility
• The keyword public
indicates that access of
the members that follow it are available
without restriction.
• Without this keyword, class members are by
default private to the class.
• Private members are available for use only
by other member functions of the class.
Public members are available to any
function within the scope of the class
declaration.
1.3 Encapsulation and Type
Extensibility
• Demo on page 6: string1.cpp
– The declaration of member functions allows the
ADT to have particular functions act on its
private representation.
– Member functions that do not modify member
variables' values are declared const.
– Code that uses this type is its client,
and can
use only public members to act on my_string
variables.
1.3 Encapsulation and Type
Extensibility
• Demo on page 6: string1.cpp
– The member functions are called using the dot
operator or structure member operator.
1.4 Constructors and Destructors
• In OOP terminology, a variable is called an
object.
obj e c t .
• A constructor
c on s t r u c t or
is a member function whose
job is to initialize an object of its class.
• A constructor of one argument can provide
a type conversion, unless the keyword
explicit
iltcexp
is used in declaring the oneargument
constructor.
1.4 Constructors and Destructors
• A destructor
de s t r u c t or
is a member function whose
job is to finalize a variable of its class.
• The destructor
de s t r u c t or
is called implicitly when an
automatic object goes out of scope.
• The remodeled class will use a constructor
to allocate an appropriate amount of storage
dynamically using the new
operator.
1.4 Constructors and Destructors
• Demo on page 8: string2.cpp
• A
constructor's name is the same as the
class name.
• A destructor is written as an ordinary
member function, whose name is the same
as the class name preceded by the tilde
symbol ~
.
1.4 Constructors and Destructors
• It is usual to overload the constructor,
writing a variety of such functions to
accommodate more than one style of
initialization.
1.5 Overloading
• A typical declaration invoking:
my_string a, b(10), c("1 came by horse.");
• The overloaded constructor is selected by
the form of each declaration.
1.5 Overloading
• Overloading is the practice of giving several
meanings to an operator or a function.
• Demo on page 10: string3.cpp
– version of print()
– overload
– friend
and operator
1.5 Overloading
• The keyword operator
eraotrp
precedes the
operator token, and replaces what would
otherwise be a function name in a function
declaration.
• The keyword friend
gives a function access
to the private members of a class variable.
A friend function is not a member of the
class, but has the privileges of a member
function in the class in which it is declared.
1.5 Overloading
• Dissection of the operator+() Function
my_string& operator+(const my_string& a, const my_string& b)
• The declaration type&
identifier declares the
identifier to be a reference variable.
• Use of const
indicates that the arguments cannot
be modified.
my_string* temp=new my_string(a.len+b.len);
• We use new
to allocate a sufficiently long
character array.
return *temp;
• The dereferenced pointer temp references the
concatenated my_string.
1.6 Templates and Generic
Programming
• C++ uses the keyword temp1ate to provide
parametric polymorphism.
par am e t r i c pol y m or ph i s m .
• Parametric polymorphism allows the same
code to be used with respect to different
types where the type is a parameter of the
code body.
1.6 Templates and Generic
Programming
• An especially important use for this
technique is in writing generic container
c on t ai n e r
classes.
c l as s e s .
• A container class is used to contain data of a
particular type.
• The syntax of the class declaration is
prefaced by:
template <class identifier
i de n t i f i e r
>
1.6 Templates and Generic
Programming
• Demo on page 13: tstack.cpp
– When processing such a type, the code must
always use the angle brackets <>
as part of the
declaration.
1.7 The Standard Template Library
(STL)
• The standard template library (STL)
is the
C++ standard library providing generic
programming for many standard data
structures and algorithms.
• The library provides container classes, such
as vectors, queues, and maps; navigation
over these containers using iterator classes;
and algorithms for their use such as sorting
and searching functions.
1.7 The Standard Template Library
(STL)
• We present a brief description emphasizing
these three legs:
– containers
– iterators
– algorithms
• Demo on page 15: stl_list.cpp
1.8 Inheritance
• A
singular concept in OOP is the
promotion of code reuse through the
inheritance
i n h e r i t an c e
mechanism.
• A
new class is derived
de r i v e d
from an existing
one called the base
bas e
class.
• The derived class reuses the base class
members and can add to or alter them.
1.8 Inheritance
• OOP Design Methodology
1. Decide on an appropriate set of types.
2. Design their relatedness into the code, using
inheritance.
• Demo on page 16: student1.cpp
1.8 Inheritance
• OOP Design Methodology
1. Decide on an appropriate set of types.
2. Design their relatedness into the code, using
inheritance.
1.8 Inheritance
• Demo on page 16: student1.cpp
1.9 Polymorphism
• A polymorphic
pol y m or ph i c
function or operator has
many forms.
• A function is called based on its signature,
s i gn at u r e ,
which is the list of argument types in its
parameter list.
a/b;
cout<<a;
1.9 Polymorphism
• Polymorphism localizes responsibility for
behavior.
1.9 Polymorphism
• Abstract base class
A bs t r ac t bas e c l as s
is a class containing one
or more pure virtual functions, as show in
the following code.
• Demo on page 20: shape1.cpp
1.10 C++ Exceptions
• C++ introduces an exception handling
mechanism that is sensitive to context.
• The context for raising an exception is a try
t r y
block. Handlers declared using the keyword
catch
c at c h
are found at the end of a try block.
• An exception is raised by using the throw
t h r ow
expression.
• Demo on page 21: stackex.cpp
1.11 Benefits of Object-Oriented
Programming
• The central element of OOP is the encapsulation
of an appropriate set of data types and their
operations.
• Classes also provide data hiding.
• Another important concept in OOP is the
promotion of code reuse through the inheritance
mechanism.
• The solution will be more encapsulated and thus
more robust and easier to maintain and change.
Also, the solution will be more reusable.
• OOP
= type-extensibility
+ polymorphism
1.11 Benefits of Object-Oriented
Programming
• OOP programming is frequently more
difficult than normal procedural
programming as found in C. There is at
least one extra design step before the coding
of algorithms.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics