Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs."— Presentation transcript:

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

2 C and C++ C evolved from BCPL and B. BCPL was developed by Martin Richards in 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

3 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

4 C(++) programs without classes

5 Call by reference example

6 Call with pointer example

7 Pointer arithmetic

8 Pointer arithmetic, p.2

9 Bracket Program (C, C++)

10 Main() for bracket

11 Passing arguments: value, reference

12 Point Class

13 Point Class, cont.

14 Point Program Example

15 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.

16 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.

17 An Exquisite Point Class

18 An Exquisite Point Class, p.2

19 An Exquisite Point Class, p.3

20 Using the Point Class

21 Constructors and Destructors Dynamic memory allocation

22 Constructors and Destructors, p.2

23 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 garbagecollection. 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.

24 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, youre 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

25 Stack Template Class

26 Main Program, Stack Template

27 Inheritance: Access Specifiers

28 Research project revisited

29 Undergrad

30 Research project class

31 Research project class, p.2

32 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!

33 Main program

34 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®

Download ppt "1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs."

Similar presentations

Ads by Google