0% found this document useful (0 votes)
53 views

CS205-2020 Spring - Lecture 9 PDF

This document summarizes key concepts about C++ programming with objects and classes. It discusses the differences between procedural and object-oriented programming, how classes are defined in C++, and access control for class members. It also covers class member function implementations, constructors and destructors, the this pointer, class scope, and provides an example of implementing a stack as an abstract data type using classes.

Uploaded by

Eason Guan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

CS205-2020 Spring - Lecture 9 PDF

This document summarizes key concepts about C++ programming with objects and classes. It discusses the differences between procedural and object-oriented programming, how classes are defined in C++, and access control for class members. It also covers class member function implementations, constructors and destructors, the this pointer, class scope, and provides an example of implementing a stack as an abstract data type using classes.

Uploaded by

Eason Guan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

C/C++ Programming

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

• See program example 1


Reviewing Our Story to Date
• Specify a class design is to provide a class declaration

• Specify a class design is to implement the class member


functions

• Create an object, which is a particular example of a class


Constructor Declaration and
Definition
• A program automatically invokes the constructor when it
declares an object
Ø Have NO return value and has NO declared type
Ø Avoid confusion: between member variables and arguments
• Using constructors
Ø Call the constructor explicitly

Ø Call the constructor implicitly

Ø Constructors are used differently from the other class methods


Default Constructors
• Do you remember the default functions?
• Create an object when you don’t provide explicit
initialization values
Ø Have the members been initialized?
• How to provide default constructors
Ø One is to provide default values for all the arguments to the
existing constructor
Ø The second is to use function overloading to define a second
constructor, one that has no arguments
Ø You can have only one default constructor
Destructors
• When program expires, when the program exits the block of
code in which an object is defined or when you use delete to free
the memory dynamically allocated for an object
Ø Destructor: a special member function is called
Ø Clean up all variables
Ø Use new to create variables in constructor and use delete to free them
• Destructor form
Ø Be formed from the class name preceded by a tilde (~)
Ø Have NO return value and has NO declared type
Ø Must have NO arguments
• See program example 2
Initialization and const
#define VALUE 100
• C++11 list initialization (followed program 2)
const int value = 100;

const int * p_int;


int * const p_int;

void func(const int *);


• const member functions void func(const int &);

Ø A function promises NOT to modify the invoking object


Review of Constructors and
Destructors
• Constructor (special member function of the same name)
Ø Have more than one constructor with the same name, provided
that each has its own signature
Ø Have NO declared type
Ø Have NO arguments for a default constructor
• Destructor (special member function of the name preceded by a tilde)
Ø Invoke a destructor when an object is destroyed
Ø You can have only one destructor per class
Ø Have no return type (not even void) and no arguments
Ø Use delete become necessary when class constructors use new
Knowing Your Objects: The this
Pointer
• The this pointer points to
the object used to invoke
a member function
• In general, all class
methods have a this
pointer set to the address
of the object that invokes
the method
Ø Why is it general and
what is special?
An Array of Objects
• Create several objects of the same class
Ø Declare an array of objects the same way you declare an array of
any of the standard types
ü Either: the class explicitly defines no constructors at all, in which case the
implicit do-nothing default constructor is used
ü Or: an explicit default constructor be defined
ü More: use a constructor to initialize the array elements

• See program example 3


Class Scope
• Review scope
Ø Global (or file) scope
Ø Local (or block) scope
Ø Function names can have global scope but they never have local scope
• Class scope applies to names defined in a class
Ø The names of class data members
Ø Class member functions
Ø Can’t directly access members of a class from the outside world
ü Direct membership operator (.)
ü Indirect membership operator (->)
ü Scope-resolution operator (::)
Class Scope Constants
• Problem: until you create
an object, there’s no
place to store a value
Ø A symbolic constant:
declare an enumeration
within a class
Ø A constant within a
class—using the
keyword static

1. Constants: symbol and literal


2. Defining Constants: macos
3. Enumerated Constants
Scoped Enumerations (C++11)
• Problem: enumerators from
two different enum
definitions can conflict
• Have class scope for its
enumerators
Abstract Data Types
• An example: stack
Ø Create an empty stack
Ø Add an item to the top of a stack
Ø Remove an item from the top
Ø Check whether the stack is full
Ø Check whether the stack is empty

• See program example 4


Summary
• Objects and classes
Ø Two programming styles
Ø Classes in C++
Ø Access control
Ø Function implementations
Ø Constructors and destructors
Ø this pointer
Ø Class scope
Ø Abstract data type: stack
Thanks

[email protected]

You might also like