Tonight we will look at: - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Tonight we will look at:

Description:

For example, an automobile has an abstraction layer that allows us to drive ... Engine Technology, brand of vehicle, style of wheels. Alan Yorinks. Lecture 5. 7 ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 18
Provided by: alanyo
Learn more at: http://www.kean.edu
Category:
Tags: look | tonight

less

Transcript and Presenter's Notes

Title: Tonight we will look at:


1
Lecture 5
  • Tonight we will look at
  • Function Overloading
  • Dynamic Data Allocation
  • Abstract Data Types (ADT)
  • Introduction to Object Orientation

2
Function Overloading
  • In C, one can reuse a function name with
    different input parameters.
  • Let's look at lect5_1.cpp and lect5_2.cpp
  • Reasons for overloading
  • Less "namespace polution"
  • Easier for user to understand
  • Very useful when using "classes"
  • Rules
  • parameter list must be different in type and/or
    number of parameters

3
Dynamic Memory Allocation
  • When writing a program that is either time or
    space constrained it may be useful to create
    variables only when needed.
  • Up until now, memory allocation has been static,
    that is at compile time.
  • With dynamic memory allocation techniques, data
    space can be allocated as needed during run-time.
  • Created on heap or free-store.
  • Downside, need to deallocate space. Failure to do
    so creates "memory leak".

4
Dynamic Memory Management
  • To allocate memory
  • In C
  • new operator
  • In C
  • malloc function (and its associated functions)
  • To de-allocate memory
  • In C
  • delete operator
  • In C
  • free function
  • We will use the new and delete operators

5
Syntax
  • new returns a pointer to the type of storage
    allocated
  • type pointerVarName new type
  • int p new int
  • delete takes the pointer variable name
  • can be equal to NULL - be careful
  • delete p
  • Let's look at lect5_3.cpp

6
What is Abstraction?
  • A view that separates the areas of concern from a
    whole, to make an item easier to comprehend and
    manage.
  • For example, an automobile has an abstraction
    layer that allows us to drive almost any type of
    vehicle.
  • Areas of concern
  • Accelerator, Brakes, Steering Wheel, Ignition,
    Gear Shift, Wind Shield Wipers, Heater, Radio,
    Fuel Gauge, Fuel Filler Cap Location
  • Areas of little or no concern
  • Engine Technology, brand of vehicle, style of
    wheels

7
Types of Abstraction For Computer Science
  • Control Abstraction
  • Separates the logical properties of an action
    from its implementation.
  • The basis of functional decomposition.
  • Data Abstraction
  • Separates a data type's logical properties from
    its implementation.
  • Built-in for simple types
  • Simple variables and operators
  • Custom Type composed of data and functions
    specific to operate on that data

8
ADT Continued
  • ADT encapsulates both attributes (data) and
    methods (functions) that operate on that data
    into a single entity.
  • A struct in C contains only a data collection.
  • A class in C is an abstract data type

9
C ADT Is The Class
  • Let's look at a simple C class specification
    (myTime.h)

10
ADT Operations or Method Types
  • Constructor
  • A class method that creates an instance of a
    class
  • Transformer
  • A class method that changes the value of a class
    attribute
  • Observer
  • A class method that returns the value of a class
    attribute
  • Iterator
  • A class method that processes the components of
    an ADT, one at a time.

11
Class Specification
  • Separate the What from the How
  • Public
  • Interface exposed to user
  • Private
  • Items hidden from user
  • Information hiding
  • User only sees what needs to be known to use
    class
  • Protects internals of class from user
  • Can change implementation transparently

12
Class Implementation
  • Normally A Class is defined in two files.
  • The Specification File
  • className.h
  • The Implementation File
  • includes specification file
  • className.cpp
  • Use of the the Scope Resolution Operator
  • The user will include the class.h file, class
    .cpp file (or .o) and generate a separate .cpp
    file to utilize the class.
  • Let's look at myTime.cpp

13
Utilizing the Class
  • Creating a class instance
  • called a class object or simply an object
  • Let's look at lect5_4.cpp
  • A class is a specification of an interface and an
    implementation of the interface
  • An object is an instance of the class.

14
Accessing Class Members
  • Externally, the class public members of the class
    are accessed using dot notation, somewhat like
    accessing structure members.
  • instanceName.classMember
  • true for all public members
  • functions
  • data (if any)
  • Internally, you do not use dot notation, just the
    member name.
  • Let's continue looking at lect5_4.cpp

15
Constructor
  • Classes may contain a special function member
    called a constructor. An implicit one is created
    for you, if you do not explicitly specify one.
  • The constructor is called after the user makes a
    call to create an object, but before control
    returns to the user.
  • Allows for initialization of class specific
    information.
  • Looks like a normal function call, but has name
    of class, and does not have a return type.
  • Let's look at lect 5_5.cpp

16
Destructor
  • A C Class also provides the facility for an
    optional destructor method.
  • Allows for cleanup just before class goes out of
    scope.
  • There is an implicit destructor created for you
    if you don't specify one yourself.
  • Syntax
  • className() body

17
Class Operations
  • Two built-in operations are allowed with classes.
  • Dot operator
  • And assignment operator
  • Does a shallow assignment
  • C provides a copy constructor to customize
    copies to be deep copies.
  • Copy Constructor Syntax
  • className( const className classObjectName)
Write a Comment
User Comments (0)
About PowerShow.com