Download presentation
Presentation is loading. Please wait.
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: u Checking the program u Reading the documentation (printf) u Following guidelines Submission instructions README format u 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 u Track state: “in word” vs. “outside word” u Deal with appropriate end conditions (first word or last word)
6
Ex2 - background u Suppose we want to generate text automatically u We want our text to look “natural” How do we go about?
7
Language Models Attempt to capture “structure” in language u “Deep” structure: grammar, meaning u “Shallow” structure: statistics of word usage, etc. We will implement a very simple shallow model
8
Examples u 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 u 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: u Sample w1 u Sample w2 from the distribution of words that follow w1 u Sample w3 from the distribution of words that follow w2 u …
11
Prob of words after “she” Tom Swayer u was 0.128686 u had 0.0723861 u would 0.0589812 u could 0.0241287 u said 0.0187668 u said: 0.0160858 u put 0.0160858 u did 0.0160858 u began 0.0160858 u wouldn't 0.0107239 u went 0.0107239 u should 0.0107239 u never 0.0107239 MacBeth u ha's 0.235294 u comes 0.0588235 u the 0.0588235 u strike 0.0588235 u speaks, 0.0588235 u should 0.0588235 u rubbes 0.0588235 u now? 0.0588235 u liu'd. 0.0588235 u is 0.0588235 u goes 0.0588235 u go 0.0588235 u do's 0.0588235
12
Exercise - outline u Read text, break it into words u Collect statistics (counts) about pairs of words Use specified interface u Sample new text
13
Motivating Example Goal: u Graphics package u Handle drawing of different shapes u Maintain list of shapes
14
Solution #1 struct shape { enum { RECTANGLE, CIRCLE, TRIANGLE } type; doublex, y; doubleheight, width; }; void Draw( shape const* Shape ) { switch( Shape->type ) { case RECTANGLE: … case CIRCLE: …
15
Solution #1 - Discussion Pros: u Simple, direct Cons: u Adding new shapes requires changing all procedures that deal with shape
16
Solution #2 Allow to implement shape specific code struct shape { doublex, y; doubleheight, width; void(*Draw)( shape const* ); }; void Draw( shape const* Shape ) { (*Shape->Draw)(Shape); }
17
Solution #2 Pros: u Extendable u Drawing method of each shape encapsulated u Efficient Cons: u No checks can lead to errors: (*Shape1->Draw)(Shape2);
18
Solution #3 – C++ classes u Language provides tools for objects u 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: u Functions declared in the class definition u Constructor like a function, but no return type Counter::Counter() { m_count = 0; }
22
Class Implementation void Counter::increment() { m_count++; } int Counter::value() { return m_count; }
23
Class Basics: Public/Private u 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 MyClass::a() { // legal x = 1.0; // also legal y = 2.0; b(); }
26
Class Basics: Constructors u 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 u Ensure propose “cleanup” when the object is destructed u Use for freeing memory, notifying related objects, etc.
29
Class Basics: Destructors class MyClass { public: MyClass(); ~MyClass(); // destructor private: char* mem; }; MyClass::Myclass() { mem = (char*)malloc(1000); } MyClass::~Myclass() { free(mem); } int main() { MyClass a; if( … ) { MyClass b; … } … }
30
Class – Example See IntList.h and IntList.cpp u C++ implementation of data structure from last class u Data members of IntList are protected u 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)); u Does not call constructor! u Internal data members are not initialized free(L); u Does not call destructor! u Internal data members are not freed
33
new & delete Special operators: u IntList* L = new IntList; allocate memory call constructor u delete L; call destructor free memory
34
New new ; Allocate an object of type u Apply constructor to the new object u 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 int[10]; // array of 10 ints IntList* b = new IntList[n]; // array of n IntLists u Objects in allocated array must have an argument-less constructor!
37
Delete & array Special operation to delete arrays int* a = new int[10]; int* b = new int[10]; … delete [] a; // proper delete command delete b; // works, but memory leak!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.