Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Classes

Description:

Test next week do not be late! Last name: ?-? - Raschel Hall (Silverman ... and babes enter the time for a heauie iudgement heere, but how it was the newest ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 38
Provided by: csHu
Category:
Tags: classes

less

Transcript and Presenter's Notes

Title: Classes


1
Classes
2
Logistics
  • Test next week do not be late!
  • Last name
  • ?-? - Raschel Hall (Silverman Building)
  • ?-? - Feldman Hall

3
Feedback on ex0
  • Common problems
  • Checking the program
  • Reading the documentation (printf)
  • Following guidelines
  • Submission instructions
  • README format
  • Compilation
  • -Wall

4
Feedback on ex0
  • enum FALSE 0, TRUE
  • int isSpace(char c)
  • if( c c \n )
  • return TRUE
  • else
  • return FALSE
  • Compare to
  • int isSpace(char c)
  • return ( c c \n )

5
Feedback on ex0 -- logic
  • Counting words
  • Count beginning of words
  • or
  • Count end of words
  • Both require detecting word boundaries
  • Track state in word vs. outside word
  • Deal with appropriate end conditions
  • (first word or last word)

6
Ex2 - background
  • Suppose we want to generate text automatically
  • We want our text to look natural
  • How do we go about?

7
Language Models
  • Attempt to capture structure in language
  • Deep structure grammar, meaning
  • Shallow structure statistics of word usage,
    etc.
  • We will implement a very simple shallow model

8
Examples
  • put this noble banquo, present death, to make all
    is often since too, go too, go ore of cawdor in
    exposure flye all and babes enter the time for
    a heauie iudgement heere, but how it was the
    newest state esteeme him off dead, are you?

9
Examples
  • eventually, they themselves had just one end of
    that, and track of his way and tom's days --
    right to- gether ment was a thing they would hold
    such stuff, oppressed their industry kept him in
    ha'nted houses." they reached the school they
    were given worlds, was within five mile and
    finally hit that ain't ever been taking chances."
    rock stood upon the air of the raft had been
    a-swimming.

10
Model
  • 1-st order Markov model
  • Sample w1
  • Sample w2 from the distribution of words that
    follow w1
  • Sample w3 from the distribution of words that
    follow w2

11
Prob of words after she
  • Tom Swayer
  • was 0.128686
  • had 0.0723861
  • would 0.0589812
  • could 0.0241287
  • said 0.0187668
  • said 0.0160858
  • put 0.0160858
  • did 0.0160858
  • began 0.0160858
  • wouldn't 0.0107239
  • went 0.0107239
  • should 0.0107239
  • never 0.0107239
  • MacBeth
  • ha's 0.235294
  • comes 0.0588235
  • the 0.0588235
  • strike 0.0588235
  • speaks, 0.0588235
  • should 0.0588235
  • rubbes 0.0588235
  • now? 0.0588235
  • liu'd. 0.0588235
  • is 0.0588235
  • goes 0.0588235
  • go 0.0588235
  • do's 0.0588235

12
Exercise - outline
  • Read text, break it into words
  • Collect statistics (counts) about pairs of words
  • Use specified interface
  • Sample new text

13
Motivating Example
  • Goal
  • Graphics package
  • Handle drawing of different shapes
  • Maintain list of shapes

14
Solution 1
  • struct shape
  • enum RECTANGLE, CIRCLE, TRIANGLE type
  • double x, y
  • double height, width
  • void
  • Draw( shape const Shape )
  • switch( Shape-gttype )
  • case RECTANGLE
  • case CIRCLE

15
Solution 1 - Discussion
  • Pros
  • Simple, direct
  • Cons
  • Adding new shapes requires changing all
    procedures that deal with shape

16
Solution 2
  • Allow to implement shape specific code
  • struct shape
  • double x, y
  • double height, width
  • void (Draw)( shape const )
  • void
  • Draw( shape const Shape )
  • (Shape-gtDraw)(Shape)

17
Solution 2
  • Pros
  • Extendable
  • Drawing method of each shape encapsulated
  • Efficient
  • Cons
  • No checks can lead to errors
  • (Shape1-gtDraw)(Shape2)

18
Solution 3 C classes
  • Language provides tools for objects
  • Ideas similar to Java
  • Many differences in details

19
Simple Class Declaration
  • class Counter
  • public
  • Counter() // Constructor
  • void increment() // A method
  • int value() // Another one
  • private
  • int m_count

20
Using the class
  • int
  • main()
  • Counter cnt // Call to constructor!
  • printf("Initial value d\n", cnt.value() )
  • cnt.increment()
  • printf("New value d\n", cnt.value() )

21
Class Implementation
  • Implementation
  • Functions declared in the class definition
  • Constructor
  • like a function, but no return type
  • CounterCounter()
  • m_count 0

22
Class Implementation
  • void
  • Counterincrement()
  • m_count
  • int
  • Countervalue()
  • return m_count

23
Class Basics Public/Private
  • Declare which parts of the class are accessible
    outside the class
  • class Foo
  • public
  • // accessible from outside
  • private
  • // private

24
Example
  • class MyClass
  • public
  • int a()
  • double x
  • private
  • int b()
  • double y
  • int
  • main()
  • MyClass foo
  • // legal
  • foo.x 1.0
  • foo.a()
  • // illegal
  • foo.y 2.0
  • foo.b()

25
Example
  • class MyClass
  • public
  • int a()
  • double x
  • private
  • int b()
  • double y
  • int
  • MyClassa()
  • // legal
  • x 1.0
  • // also legal
  • y 2.0
  • b()

26
Class Basics Constructors
  • Initialize the class object upon construction
  • class MyClass
  • public
  • MyClass()
  • MyClass( int i )
  • MyClass( double x, double y )
  • MyClass a // Calls
  • MyClass b(5) // Calls
  • MyClass c( 1.0, 0.0 ) // Calls

1
2
3
1
2
3
27
Constructors
  • class MyClass
  • public
  • MyClass()
  • main()
  • MyClass a // Constructor is called

28
Destructors
  • Ensure propose cleanup when the object is
    destructed
  • Use for freeing memory, notifying related
    objects, etc.

29
Class Basics Destructors
  • class MyClass
  • public
  • MyClass()
  • MyClass() // destructor
  • private
  • char mem
  • MyClassMyclass()
  • mem (char)malloc(1000)
  • MyClassMyclass()
  • free(mem)
  • int
  • main()
  • MyClass a
  • if( )
  • MyClass b

30
Class Example
  • See IntList.h and IntList.cpp
  • C implementation of data structure from last
    class
  • Data members of IntList are protected
  • Usage is more natural
  • IntList L
  • L.pushFront(6)
  • if( !L.isEmpty() )
  • x L.popBack()

31
Classes Memory allocation
  • Consider this code
  • main()
  • IntList L
  • What is the difference?
  • Compare to
  • main()
  • IntList L (IntList)malloc(
  • sizeof(IntList))
  • free(L)

32
Classes Memory allocation
  • IntList L (IntList)malloc(sizeof(IntList))
  • Does not call constructor!
  • Internal data members are not initialized
  • free(L)
  • Does not call destructor!
  • Internal data members are not freed

33
new delete
  • Special operators
  • IntList L new IntList
  • allocate memory
  • call constructor
  • delete L
  • call destructor
  • free memory

34
New
  • new lttypegt
  • Allocate an object of type lttypegt
  • Apply constructor to the new object
  • Return a pointer to the new object
  • Can be used with any type
  • int i new int
  • char p new (char )

35
New Constructors
  • class MyClass
  • public
  • MyClass()
  • MyClass( int i )
  • MyClass( double x, double y )
  • MyClass a
  • a new MyClass // Calls
  • a new MyClass(5) // Calls
  • a new MyClass( 1.0, 0.0 ) // Calls

1
2
3
1
2
3
36
New arrays
  • To allocate arrays, use
  • int n 4
  • int a new int10 // array of 10 ints
  • IntList b new IntListn
  • // array of n IntLists
  • Objects in allocated array must have an
    argument-less constructor!

37
Delete array
  • Special operation to delete arrays
  • int a new int10
  • int b new int10
  • delete a // proper delete command
  • delete b // works, but memory leak!
Write a Comment
User Comments (0)
About PowerShow.com