Chapter 2 Objects and Classes - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Chapter 2 Objects and Classes

Description:

Chapter 2 Objects and Classes Bernard Chen Spring 2006 2.1 What is OO programming? Object: an atomic unit that has structure and state Information hiding: Black-box ... – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 17
Provided by: csGsuEdu
Learn more at: http://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2 Objects and Classes


1
Chapter 2Objects and Classes
  • Bernard Chen
  • Spring 2006

2
2.1 What is OO programming?
  • Object an atomic unit that has structure and
    state
  • Information hiding Black-box analogy
  • Encapsulation grouping of data and functions
  • Inheritance mechanism allows extending
    functionality of an object.

3
How does C support OO
  • Template the logic is independent of
  • the type
  • Inheritance
  • Polymorphism allows us to implement
  • new types (classes) that
  • share same logic

4
2.2 Basic Class Syntax
  • Class members either data or functions and
    categorized into either public, protected,or
    private.
  • Public visible to an instance of (object) a
    class
  • Private visible only inside an instance of a
    class
  • Protected similar to private but visible to
    derived classes.
  • Default all members are private

5
Constructors
  • Member functions that describe how an object is
    declared and initialized.
  • If no constructor defined, compilers will
    generate one called default constructor.
  • Explicit constructors prevent automatic type
    conversion.

6
Constant Member Function
  • Constant functions (accessors) functions that
    do not change any data member.
  • const is a part of the function signature.
    const return_type name(const parameter_list)
    const
  • Interface describes what can be done to the
    object, i.e. the header.
  • Implementation represents internal processes
    specified by the interface.

7
(No Transcript)
8
Big three Destructor, Copy Constructor, and
Operator
  • Destructor tells how an object is destroyed and
    freesdresources when it exists scope.
  • IntCell()
  • Copy Constructor allows a new object construct
    using the data in an existing one.
  • IntCella(5) // a new IntCellcall a
  • IntCellb(a) // another IntCellcall b
  • Operator copy assignment, copies data members
    using by default.gt may cause shallow copying.

9
This (predefine pointer)
10
2.3 Additional C Features
  • Operator overloading extending the types to
    which an operator can be applied.
  • example string xMarys score is
  • int y95
  • string zxy
  • ., ., ?, sizeof cant be overloaded

11
Additional C Features
  • Type conversion creates a temporary object of a
    new type
  • Example int a 5
  • double b a //implicit cast

12
2.5 Exceptions (report error)
  • An object that stores information transmitted
    outside the normal return sequence and is used to
    signal exceptional occurrences
  • Handle exceptions by throw and catch clauses.

13
Exceptions example
  • try
  • for (intn0 nlt10 n)
  • if (ngt9) throw "Out of range"
  • catch (char str)
  • coutltlt "Exception " ltlt strltlt endl

14
2.6 String Class
  • C string array of character terminated by \0
  • C standard string a STL class with all
    overload operators and built-in functions
  • http//www.bgsu.edu/departments/compsci/docs/strin
    g.html

15
(No Transcript)
16
Summary
  • Construction/ destruction of objects
  • Copy semantics
  • Overloading
  • Implicit/explicit type conversion
  • Information hiding/atomicity
Write a Comment
User Comments (0)
About PowerShow.com