1 / 14

Classes, Arrays & Pointers

Classes, Arrays & Pointers. Compiler & Linker expectations. file1.cpp. file2.cpp. filen.cpp. …. Compiler. Compiler. Compiler. file1.o. file2.o. filen.o. …. Linker. C++ compiler does not care about filenames. application (executable). Classes.

rona
Download Presentation

Classes, Arrays & Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes, Arrays & Pointers

  2. Compiler & Linker expectations file1.cpp file2.cpp filen.cpp …. Compiler Compiler Compiler file1.o file2.o filen.o …. Linker C++ compiler does not care about filenames. application (executable)

  3. Classes • High level concepts are same as Java – details are bit different. • Example: Complex Numbers • Unlike Java, • C++ compiler does not care about filenames. • C++ uses 2 files for each class: one header (.h) file for definition, another source file (.cpp) for implementation • Need to use “#include ….h” to use any class.

  4. Concepts: Constructor • Constructor • mostly similar to Java • Exception: default values for formal parameters

  5. Concepts: Operator overloading • Operator overloading: ComplexNumber x, y, z; z = x + y; • In Java?

  6. Concepts: Pass by value or reference • Passing parms by value or reference • User selection • const keyword for reference types • Java? • Method overloading – similar to Java • use different argument types to differentiate

  7. Concepts: Method overloading • Method overloading – similar to Java • use different argument types to differentiate

  8. Concepts: Friend • friend designation - breaks OOP philosophy! • specific functions/methods outside the class can access private data  Why?

  9. Concepts: Objects • Objects can be created as local variables just like any basic data types. ComplexNumber num1;

  10. Arrays • Basic data types and classes are treated the same way in C++, unlike Java. ComplexNumber numbers[5];

  11. Array version #2 ComplexNumber *numbers; numbers = new ComplexNumber[5];

  12. Array version #3 (equivalent to Java) ComplexNumber **numbers; numbers = new ComplexNumber*[5]; for( index i = 0 ; i < 5 ; i++) numbers[i] = new ComplexNumber(…);

  13. Pointers • Explicit in C++: ComplexType *cnump; • Pointer arithmetic: (cnump + 5) • “Address of” operator: & • Dereference operator for objects: -> cnump->setComplex(…); • Dynamic memory allocation requires pointers (just like references in Java)

  14. Dynamic memory allocation • No automatic garbage collection in C++ • # of new invocations should match # of delete invocations. • If a class constructor allocates memory (i.e. uses “new …”), it needs a destructor method too – it should use “delete …” to release allocated memory.

More Related