Chapter 2 Objects and Classes - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Chapter 2 Objects and Classes

Description:

Object: an atomic unit/entity that has ... Encapsulation: grouping of data and functions ... A cast to B and B cast to C = A cast to C????NO. 2.4 Common Idioms ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 23
Provided by: Ken695
Category:
Tags: bb | chapter | classes | objects

less

Transcript and Presenter's Notes

Title: Chapter 2 Objects and Classes


1
Chapter 2Objects and Classes
  • Ken Nguyen
  • Spring 2002

2
2.1 What is OO programming?
  • Object an atomic unit/entity 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
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

4
Constructors
  • Member functions that describe how an object is
    declared and initialized.
  • Iff no constructor defined, compilers will
    generate one called default constructor.
  • Explicit constructors prevent automatic type
    conversion.
  • Initializer list
  • Save time
  • Data member is const
  • Data member is a class type that has no
    zero-parameter constructor.

5
  • class IntCell
  • public
  • explicit IntCell(int initialValue 0)
  • storedValue(initialValue)
  • //implicit empty constructor
  • //prevent auto type-conversion
  • int read() return storedValue
  • void write(int x) storedValue x
  • private
  • int storeValue
  • class IntCell
  • public
  • IntCell()storedValue0
  • IntCell(int initialValue)
  • storedValue initialValue
  • int read() return storedValue
  • void write(int x) storedValue x
  • private
  • int storeValue

6
  • Constant functions functions that do not change
    any data member, i.e. accessors
  • 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
  • include IntCell.h
  • IntCellIntCell(int initialValue 0)
  • storedValue(initialValue)
  • int IntCellread() const
  • return storedValue
  • void IntCellwrite(int x) storedValue x
  • Figure 2.5 IntCell.cpp
  • ifndef _IntCell_H_
  • define _IntCell_H_
  • class IntCell
  • public
  • explicit IntCell(int initialValue 0)
  • int read() const
  • void write(int x)
  • private
  • int storeValue
  • endif
  • Figure 2.4 IntCell.h

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

9
this
  • Predefined pointer pointing at the current
    object.
  • IntCellIntCell()
  • IntCellIntCell(const IntCell rhs)
  • storedValue(rhs.storedValue)
  • const IntCell IntCelloperator(const IntCell
    rhs)
  • if(this ! rhs) // standard alias test
  • storedValue rhs.storedValue
  • return this
  • Figure 2.7

10
2.3 Additional C Features
  • Operator overloading extending the types to
    which an operator can be applied.
  • ., ., ?, sizeof cant be overloaded
  • Overloading functions must have different
    signatures, (return type is a part of the
    signature)
  • Type conversion creates a temporary object of a
    new type
  • int a 5 double b a //implicit cast
  • Conversions not transitive.
  • (Assume that A, B, and C have diff. Types)
  • A cast to B and B cast to C gt A cast to C????NO

11
2.4 Common Idioms
  • Avoid friend functions by using public member
    functions

12
2.5 Exceptions
  • 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
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

Hello world!\0
C string
14
class string public string( const char
cstring "" ) // Constructor string( char ch
) // Constructor string( const string str )
// Copy constructor string( ) delete
buffer // Destructor const string operator
( const string rhs ) // Copy const string
operator( const string rhs ) // Append
const char c_str( ) const return buffer //
Return C-style string int length( ) const
return strLength // Return string length char
operator( int k ) const // Accessor operator
char operator( int k ) // Mutator
operator private char buffer // storage for
characters int strLength // length of string (
of characters) int bufferLength // capacity of
buffer
15
  • ostream operatorltlt( ostream out, const string
    str ) // Output
  • . . .
  • bool operator( const string lhs, const string
    rhs ) // Compare
  • bool operator!( const string lhs, const string
    rhs ) // Compare !
  • bool operatorlt ( const string lhs, const string
    rhs ) // Compare lt
  • . . .
  • Figure 2.22

16
  • stringstring( const char cstring )
  • if( cstring NULL )
  • cstring ""
  • strLength strlen( cstring )
  • bufferLength strLength 1
  • buffer new char bufferLength
  • strcpy( buffer, cstring )

17
  • stringstring( char ch )
  • strLength 1
  • bufferLength strLength 1
  • buffer new char bufferLength
  • buffer 0 ch
  • buffer 1 '\0'

18
  • stringstring( const string str )
  • strLength str.length( )
  • bufferLength strLength 1
  • buffer new char bufferLength
  • strcpy( buffer, str.buffer )
  • .
  • .
  • .
  • Figure 2.23 2.25

19
2.7 Recap
  • vectorltstringgt array(100) // 100 calls
  • string ptr1 new string // 1 call
  • string ptr2 new string(junk) // 1 call
  • string ptr3 new string(s)// 1 call
  • string ptr4 new string100 // 100 calls
  • string ref s // no call

20
2.8 Composition
  • Classes are being used as data member for other
    classes
  • class employee
  • public
  • void setValue(const strig n, double s)
  • name n salary s
  • void print(ostream out cout) const
  • out ltlt name ltlt ( ltlt salary ltlt )
  • private
  • string name
  • double salary

21
  • ostream operatorltlt(ostream out,
  • const
    Employee rhs)
  • rhs.print(out)
  • return out
  • int main()
  • vectorltEmployeegt v(3)
  • v0.setValue(Bill Clinton, 200000.00)
  • . . .
  • for(int i 0 i lt v.size() i)
  • coutltltviltltendl
  • return 0
  • // figure 2.28

22
Summary
  • Construction/ destruction of objects
  • Copy semantics
  • Input/output operations
  • Overloading
  • Implicit/explicit type conversion
  • Information hiding/atomicity
Write a Comment
User Comments (0)
About PowerShow.com