DOC PREVIEW
UW CSE 303 - Lecture Notes

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2007Lecture 24— C++ continuedDan Grossman CSE303 Spring 2007, Lecture 24 1'&$%In the middle of C++Doing a tiny fraction of an enormous language:• Many small conveniences over C• OOP without everything being a pointer to a heap object• OOP with manual memory m anagement and lots of HYCSBWKthings• OOP with different kinds of inheritance and overridingBack to our first class-definition in Property.h, Property.cc...Dan Grossman CSE303 Spring 2007, Lecture 24 2'&$%OOP in C++, part 1Like Java:• Fields vs. methods, static vs. instance, constructors• Method overloading (functions, operators, and constructors too)Not quite like J ava:• access-modifiers (e.g., private) syntax and default• declaration separate from implementation (like C)• funny constructor syntax, default parameters (e.g., ... = 0)Nothing like Java:• Objects vs. pointers to objects• Destructors and copy-constructors• virtual vs. non-virtual (to be discussed)Dan Grossman CSE303 Spring 2007, Lecture 24 3'&$%Stack vs. heapJava: cannot stack-allocate an object (only a pointer to one).C: can stack-allocate a struct, then initialize it.C++: stack-allocate and call a constructor (where this is the object’saddress, as always)• Property p1(10000);Java: new Property(...) calls constructor, returns heap-allocatedpointer.C: Use malloc and then initialized, must free exactly once later.C++: Like Java, but can also do new int(42). Like C mustdeallocate, but must use delete instead of free.Dan Grossman CSE303 Spring 2007, Lecture 24 4'&$%DestructorsAn object’s destructor is called just before the space for it is reclaimed.A common use: Reclaim space for heap-allocated things pointed to(first calling their destructors).• But not if there are other pointers to it (aliases)?!Meaning of delete x: call the destructor of pointed-to heap object,then reclaim space.Destructors also ge t c alled for stack-objects (when they leave scope).Advice: Always make destructors virtual (learn why soon)Dan Grossman CSE303 Spring 2007, Lecture 24 5'&$%ArraysCreate a heap-allocated array of objects: new A[10];• Calls default (zero-argument) constructor for each element.• Convenient if there’s a good default initialization.Create a heap-allocated array of pointers to objects: new A*[10]• More like Java (but not initialized?)• As in C, new A() and new A[10] have type A*.• new A* and new A*[10] both have type A**.• Unlike C, to delet e a non-array, you must write delete e• Unlike C, to delet e an array, you must write delete [] eElse HYCSBWK – the deleter must know somehow what is an array.Dan Grossman CSE303 Spring 2007, Lecture 24 6'&$%Digression: Call-by-referenceIn C, we know function arguments are copies• But copying a pointer means you still point to the same(uncopied) thingSame in C++, but a “reference parameter” (the & character after it)is different.Callee writes: void f(int& x) { x = x + 1; }Caller writes: f(y)But it’s as though the caller wrote f(&y) and everywhere the calleesaid x they really said *x.So that little & has a big meaning.Dan Grossman CSE303 Spring 2007, Lecture 24 7'&$%Copy ConstructorsIn C, we know x=y or f(y) copies y (if a struct, then member-wisecopy).Same in C++, unless a copy-constructor is defined, then do whateve rit says.A copy-constructor by definition takes a reference parameter (else we’dneed to copy, but that’s what we’re defining) of the same type.Let’s not talk about the const.Our example use is strange (why increment a counter), but useful forunderstanding what happens.Dan Grossman CSE303 Spring 2007, Lecture 24 8'&$%Now more OOP: SubclassingTo me, OOP is “all about” subclasses overriding methods.• Often not what you want, but what makes OOP fundamentallydifferent from, say, functional programming (CSE341)C++ gives you lots more options than Java with different defaults, soit’s easy to scream “compiler bug” when you mean “I’m using thewrong feature”...Basic subclassing:• class D : public C { ... }• This is public inheritance; C++ has other kinds too (won’t cover)– Differences affect visibility and issues when you have multiplesuperclasses (won’t cover)– So do not forget the public keywordDan Grossman CSE303 Spring 2007, Lecture 24 9'&$%More on subclassing• Not all classes have supe rclasses (unlike Java with Object)• I prefer terms “s uperclass” and “subclass” but C++ programmerstend to use “base class” and “derived class”– Just a terminology thing• Our example code: House derives from Land which derives fromProperty• As in Java, can add fields/methods/constructors, and overridemethods.Dan Grossman CSE303 Spring 2007, Lecture 24 10'&$%Construction and destruction• Constructor of base class gets called before constructor of derivedclass– Default (zero-arg) constructor unless you specify a differentone after the : in the constructor.• Destructor of base c lass gets called after destructor of derivedclassSo constructors/destructors really extend rather than override, sincethat is typically what you want.Dan Grossman CSE303 Spring 2007, Lecture 24 11'&$%Method overriding, part 1If a derived class defines a method with the same name and argumenttypes as one defined in the base class (perhaps because of anancestor), it ove rrides (i.e., replaces) rather than extends.If you want to use the base-class code, you specify the base c lass whenmaking a method call.• Like super in Java (no such keyword in C++ since there may bemultiple inheritance)Warning: the title of this slide is part 1.Dan Grossman CSE303 Spring 2007, Lecture 24


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?