CS205-2020 Spring - Lecture 9 PDF
CS205-2020 Spring - Lecture 9 PDF
Language
CS205 Spring
Feng Zheng
2019.04.23
Content
• Brief Review
• Objects and classes
Ø Two programming styles
Ø Classes in C++
Ø Access control
Ø Function implementations
Ø Constructors and destructors
Ø This pointer
Ø Class scope
• Summary
Brief Review
Review
• A header file
• Header File Management (guarding scheme)
• Scope and Linkage
• 1. Automatic Storage Duration
• 2. Static Duration Variables:
Ø External, Internal and No Linkage
Ø Specifiers and Qualifiers
Ø Functions Linkage
• 3. Storage Schemes and Dynamic Allocation
Objects and Classes
Procedural and Object-Oriented
Programming
• Procedural Programming
Ø Firstly concentrate on the procedures you will follow
Ø Then think about how to represent the data
• Object-Oriented Programming
Ø Begin by thinking about the data
ü Concentrate on the object as the user perceives it
ü Describe the object and the operations that will describe the user’s
interaction with the data
ü Decide how to implement the interface and data storage
Ø Put together a program to use your new design
What Is a Type?
• Specifying a basic type does three things
Ø It determines how much memory is needed for a data object
Ø It determines how the bits in memory are interpreted (long vs.
float)
Ø It determines what operations, or methods, can be performed
using the data object (integer vs. pointer)
• For built-in types
Ø The information about operations is built in to the compiler
• For user-defined types in C++
Ø Have to provide the same kind of information yourself
Classes in C++
• A class is a C++ vehicle for translating an abstraction to a
user-defined type
Ø Include data representation
Ø Include methods for manipulating that data
• A class specification has two parts
Ø A class declaration, which describes the data component, in terms
of data members, and the public interface, in terms of member
functions, termed methods
Ø The class method definitions, which describe how certain class
member functions are implemented
Access Control
• Describe access control
for class members
Ø Any program that uses an
object of a particular
class can access the
public portions directly
Ø A program can access the
private members of an
object only by using the
public member functions
Components
• Abstraction component: the public interface
• Encapsulation component: gather the implementation details
and separate them from the abstraction
Ø Data hiding: insulation of data from direct access by a program is
called
Ø Data hiding is an instance of encapsulation
ü Prevent you from accessing data directly
ü Absolve you from needing to know how the data is represented
ü By default, the members are private (in structure type: public by default)
Implementing Class Member
Functions
• Provide code for those member functions
represented by a prototype in the class
declaration
Ø Use the scope-resolution operator (::) to
identify the class to which the function belongs
Ø Access the private components of the class
Ø Has class scope (the same name for multi-class)
• Inline function:
Ø Any function with a definition in the class
declaration automatically
Ø Define a member function outside the class
declaration and still make it inline
Which Object Does a Method Use?
• Contain storage for its own internal variables, the class
members
• But all objects of the same class share the same set of class
methods, with just one copy of each method
• Questions?
Ø What about a static variable for member functions of a class?
Ø What about a static member of a class?
Ø What about a static member function of a class?
Using Classes
• A program example:
create and use objects
of a class