Download presentation
Presentation is loading. Please wait.
Published byUrsula O’Connor’ Modified over 9 years ago
1
Classes, Arrays & Pointers
2
Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler C++ compiler does not care about filenames.
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.