Compiler amp Linker expectations file1cpp file2cpp filencpp file1o file2o fileno Linker application executable Compiler Compiler Compiler C compiler does not care about filenames ID: 546390
Download Presentation The PPT/PDF document "Classes, Arrays & Pointers" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Classes, Arrays & PointersSlide2
Compiler & Linker expectations
file1.cpp
file2.cpp
filen.cpp
….
file1.o
file2.o
filen.o
….
Linker
application
(executable)
Compiler
Compiler
Compiler
C++ compiler does not
care about filenames.Slide3
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.Slide4
Concepts: Constructor
Constructor
mostly similar to Java
Exception: default values for formal parametersSlide5
Concepts: Operator overloading
Operator overloading:
ComplexNumber x, y, z;
z = x + y;In Java? Slide6
Concepts: Pass by value or reference
Passing parms by value or reference
User selection
const keyword for reference types
Java?Method overloading – similar to Javause different argument types to differentiateSlide7
Concepts: Method overloading
Method overloading – similar to Java
use different argument types to differentiateSlide8
Concepts: Friend
friend designation - breaks OOP philosophy!
specific functions/methods outside the class can access private data
Why?Slide9
Concepts: Objects
Objects can be created as local variables just like any basic data types.
ComplexNumber num1;Slide10
Arrays
Basic data types and classes are treated the same way in C++, unlike Java.
ComplexNumber numbers[5];Slide11
Array version #2
ComplexNumber *numbers;
numbers = new ComplexNumber[5];Slide12
Array version #3 (equivalent to Java)
ComplexNumber **numbers;
numbers = new ComplexNumber*[5];
for( index i = 0 ; i < 5 ; i++)
numbers[i] = new ComplexNumber(…);Slide13
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)Slide14
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.