34
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Prog rams

1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Embed Size (px)

Citation preview

Page 1: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

1.00 Lecture 37

A Brief Look at C++: A Guide to Reading C++ Programs

Page 2: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

C and C++

• C evolved from BCPL and B. BCPL was developed by Martin Richards in 1967. Ken Thompson based B on BCPL and used it to write very early versions of UNIX. These were "typeless" languages. All data occupied same amount of memory, and programmer had to deal with data type differences.

• C developed in 1972 by Dennis Ritchie at Bell Lab.

• C++ developed by Bjarne Stroustrup in ealry 1980s at Bell Labs to support OO programming

Page 3: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Areas C/C++ differ from Java®

• Not all code in objects • header files • preprocessor • Call by reference allowed • Pointers and pointer arithmetic • Array bounds checking • Garbage collection, destructors, memory leaks • Templates (parameterized data types) • Automatic instances of objects within a scope • implicit type conversion • operator overloading • multiple inheritance • virtual and non virtual methods

Page 4: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

C(++) programs without classes

Page 5: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Call by reference example

Page 6: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Call with pointer example

Page 7: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Pointer arithmetic

Page 8: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Pointer arithmetic, p.2

Page 9: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Bracket Program (C, C++)

Page 10: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Main() for bracket

Page 11: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Passing arguments: value, reference

Page 12: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Point Class

Page 13: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Point Class, cont.

Page 14: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Point Program Example

Page 15: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Copy constructors in C++

• In C++, objects are passed as copies in call by value

• By default, objects are copied "bitwise" • Objects that allocate memory in their constructors

(arrays, other objects) need a special constructor that is equivalent of the clone( ) method in Java®

• Copy constructor takes reference to object, and returns a new object of same class that is an independent copy.

• Copy constructors often get invoked implicitly. For example, a method that returns a new object will implicitly invoke copy constructor if one exists.

Page 16: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Why copy constructors?

• Object of class A, when constructed, includes an array.

• In a method, we declare a

newObj is a local variable, so its destructor method will be called when method returns. Memory allocated by constructor will be returned to memory pool unless there is a copy constructor for class A that allocated new memory.

Page 17: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

An Exquisite Point Class

Page 18: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

An Exquisite Point Class, p.2

Page 19: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

An Exquisite Point Class, p.3

Page 20: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Using the Point Class

Page 21: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Constructors and Destructors

Dynamic memory allocation

Page 22: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Constructors and Destructors, p.2

Page 23: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Constructors and Destructors, p.3

• No memory management in main program orfunctions, as a goal in C++ – In C, memory was managed for each variable

• You had to remember to allocate it and free it when done, no matter where these events occurred.

• Dynamic memory errors are over 50% of C program errors

– In C++, we build memory management into classes • ‘New’ only in constructors; ‘delete’ only in destructors • Application developer sees nearly automatic garbagecoll

ection. She “never” uses new; creates new objects just by defining them: Student Joe, same as int i

• Class developer has control of garbage collection when needed

– C++ garbage collection is tough on lists, trees, etc.

Page 24: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Memory Management Errors

• Deleting same memory twice is a bug – Hard to find! – Trick: Comment out all deletes if you have a weird

bug in a program with dynamic memo • If bug goes away, you’re deleting some memory twice

• Not deleting memory when done is a leak – Leaking 100 bytes per call on a Web server with a

million calls a day means buying GBs of memory and crashing regularly anyway

Page 25: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Stack Template Class

Page 26: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Main Program, Stack Template

Page 27: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Inheritance: Access Specifiers

Page 28: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Research project revisited

Page 29: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Undergrad

Page 30: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Research project class

Page 31: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Research project class, p.2

Page 32: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Research project class

C++ will dynamically set Student pointers to Undergrad, Grad, SpecGrad to get the pay for each

If we stored Student objects in StaffList instead of pointers, we would only have the base Student class data!

Page 33: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Main program

Page 34: 1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs

Other Items

• Exceptions essentially same as Java® – Try, throw, catch

• Standard template library (STL) with Vectors, Lists, Stacks, similar to Java® – Cannot inherit from STL classes

• Multiple inheritance supported • C has two string types (with conversions)

– Null terminated arrays (old) – String class

• Arrays are just a memory location (reference) – Array size is passed as separate argument – Very problematic (“buffer overruns”)

• Recursion supported as in Java®