Yingcai Xiao
TOC
Header files and Makefile Command line arguments: argc and argv Introduction to pointers: C++1.ppt Advanced pointer concepts Arrays Pointer arithmetic Functional pointers Virtual functions CORBA
C++ Compilation
Source File (.cpp) Preprocessing
Linking
Binary File (.exe)
Preprocessing
copy everything in the include files into the .cpp file replace all symbolic constants remove all white spaces: \n \t and space. preprocessing flags #include : copy everything in the include files e.g. #include <iostreams> // system include files #inlcude Rectangle.h // user defined files #define: string substitution e.g. #define PI 3.1415926 #ifdef #endif e.g #ifdef DEBUG
Implementation files extension: .cpp contain function implementation for non-OOP coding contain class implementation for OOP coding compiled into .obj files obj files are binary files
Preprocessing
#ifndef RECTANGLEH #define RECTANGLEH
class Rectangle{ protected: int x, y; public: Rectangle (); // only header, no body Rectangle (int cx, int cy); double Area( ); };
#endif
Compilation
cygwin pwd /cygdrive/c/Documents and Settings\xiao is mapped to C:\Documents and Settings\xiao mkdir oop copy main.cpp Rectangle.cpp and rectangle.h to the directory g++ main.cpp Rectangle.cpp I. o sep.exe ./sep Compilation flags: -I : search path for include files -o : output exe file name -l : add libraries files -c : generating object files -g : generating debugging info -D : defining symbolic constants -DDEBUG => #define DEBUG 1
Makefile
create a makefile with dependencies (C++Examples\MyRectangle) sep : main.o Rectangle.o MyRectangle.o g++ main.o Rectangle.o MyRectangle.o -o sep.exe
Example
int main(int argc, char* argv[]) { cout << "Your have typed: "; for(int i = 0; i < argc; i++) cout << argv[i] << " "; cout << "\n"; if(argc != 4) { cerr << "Usage: " << argv[0] << " Model View n(# of integer to process)." << endl; exit(1); } int n = atoi(argv[3]); cout << "You requested to process " << n << " integers. \n"; return(1); }
References in Java
Class Rectangle { protected: int x, y; Public: Rectangle () {x=0;y=0;} Public: Rectangle (int cx, int cy) {x=cx; y=cy;} Public: double area( ) { return x*y; } } int width 0
x y
Int height 0
Rectangle () {x=0;y=0;} w, int Rectangle (int cx, inth) cy) Area Area() double
Int height 0
Rectangle () {x=0;y=0;} w, int Rectangle (int cx, inth) cy) Area Area() double
i is a value type, for which we can directly store an integer value into the memory named as i. Compiler already allocated memory to store the value and we dont need to new to allocate memory to store the value.
0 0
Rectangle () {x=0;y=0;}
Rectangle (int cx, int cy)
double Area()
rect is the name of a memory space that stores a Rectangle object.
Pointers in C++
Rectangle *rect; double area = rect->Area(); // Wrong! Compiles. Try! Rectangle *rect = new Rectangle (); // Correct, use the first constructor rect 0x12345678 double a = (*rect).Area(); double b = rect->Area();
Int height 0
Rectangle () w, int Rectangle (int cx, inth) cy) Area Area() double
The type of a pointer tells the compiler the layout of the memory pointed to by the pointer. (Please note the notation difference between
a pointer and a name.)
3.
4. 5. 6. 7.
Code Example
Write down the output of the following code and draw a picture to show the memory structure. class Point { public: int x; int y; }; Point p1; p1.x = 1; p1.y = 2; Point p2 = p1; // Copies p1
cout << p1.x << << p1.y; cout << p2.x << << p2.y;
p2.x = 3; p2.y = 4;
cout << p1.x << << p1.y; cout << p2.x << << p2.y;
p3->x = 5; p3->y = 6;
cout << p1.x << << p1.y; cout << p3->x << << p3->y;
Code Example
Find and describe the errors in the following code. class Point { public: int x; int y; }; Point p1; p1.x = 1; p1.y = 2; Point *p3; cout << p3->x << << p3->y; p3 = & p1; cout << p3->x << << p3->y; delete p3; p3 = new Point(); cout << p3->x << << p3->y; delete p3; free (p3);
Code Example
Find and describe the errors in the following code. p3 = new Point(); p3->x = 3; p3->y = 4; Point *p4 = p3; cout << p4->x << << p4->y; delete p3; delete p4; Point *p5; int i = 1; if(i == 1) { Point p6; p5 = & p6; Point *p7 = new Point(); } cout << p5->x << << p5->y;
Arrays in C++
int a[2]; a[0] = 5; a[1] = 10; cout << a[0] << << a[1]; // stack objects, size has to be a constant and cant be changed at runtime. int size = 2; int b[size]; // will not compile in C++ // An array name represents a constant pointer, cant change its value. a
10
int size; cin >> size; // assuming user entered 2. int *p; // a pointer p = new int[size]; p[0] = 5; p[1] = 10; cout << p[0] << << p[1]; // heap objects; dynamically allocated at runtime, size can be a variable delete p; // free the memory. p
0xff
10
int *p2; // a pointer p2 = a; p2[0] = 50; p2[1] = 100; cout << a[0] << << a[1]; delete p2; // Dont do this.
Pointer Arithmetic
Pointers can be moved back and forth by one object with ++ and --
5
p
10
0xff00 0xff04
// a pointer
int a[2] = {5,10}; int *p; p = a; cout << p[0]; p++; cout << p[0]; p--; cout << p[0]; p++; cout << p[1];
Type Cast
class Rectangle{ protected: int x, y; public: Rectangle () {x=0;y=0;} Rectangle (int cx, int cy) {x=cx; y=cy;} double Area( ) { return x*y; } };
int width 0
x y
Rectangle rect;
Int height 0
Rectangle () {x=0;y=0;} w, int Rectangle (int cx, inth) cy) Area Area() double
Type Cast
class Rectangle{ protected: int x, y; public: Rectangle () {x=0;y=0;} Rectangle (int cx, int cy) {x=cx; y=cy;} double Area( ) { return x*y; } }; class MyRectangle: public Rectangle{ public: void Set(int dx, int dy){x=dx; y=dy;} };
double Area()
void Set(int dx, int dy)
MyRectangle rect2;
MyRectangle *p2; // implicit cast to a child class, will not compile p2 = p1; //explicit cast to a child class, allowed p2 = (MyRectangle *) p1; cout << p2->Area(); // ok p2->Set(1,1); //will compile, cause runtime error
double Area()
Implicit type cast a pointer to its child class (down cast) is not permitted.
Explicit type cast a pointer to its child class (down cast) is permitted, but should be used with care.
double Area()
void Set(int dx, int dy)
Implicit or explicit type cast a pointer to its parent class (up cast) is permitted. Up cast is important for using a predefined event loop. Shape->Draw(); Down cast should only be used when the original object is an instance of the child class.
Passing by Value
void Reset(MyRectangle rectf) { // rectf is the formal parameter, it has its own memory. rectf.Set(1,1); cout << rectf.Area(); // 1 } void main () { MyRectangle rect; cout << rect.Area(); // 0 Reset(rect); // rect is the actual parameter cout << rect.Area(); // 0 }
Pass-by-value: The value of the actual parameter is copied to the formal parameter when a method is called. The value of the formal parameter is not copied to the actual parameter when the method returns. The formal parameter expires when the method returns. The value of the actual object can not be changed by the called method.
Passing by Pointer
void Resetp(MyRectangle *rectp) { // rectp is the formal parameter, a pointer. rectp->Set(1,1); rectp 0xff00 cout << rectp->Area(); } void main () { MyRectangle rect; rect cout << rect.Area(); // 0 Resetp(&rect); // &rect is the actual parameter cout << rect.Area(); // 1 }
double Area()
void Set(int dx, int dy)
Pass-by-pointer: The address of the object is copied to the pointer formal parameter when a method is called. When the pointer is dereferenced the actual object (rect in the main) is accessed. The value of the actual object can be changed by the called method.
Reference
A reference is an alias of an object, it references the same memory of the object. void main () { MyRectangle rect; cout << rect.Area(); // 0 // declare and initialize a reference MyRectangle &rectr = rect; rect rectr
double Area()
void Set(int dx, int dy)
Passing by Reference
Void Reset(MyRectangle &rectr) { rectf // rectr is the formal parameter, a reference. rectr.Set(1,1); cout << rectr.Area(); // 1 } void main () { MyRectangle rect; rect cout << rect.Area(); // 0 // rect is the actual parameter, its reference is passed. Reset(rect); cout << rect.Area(); // 1 }
double Area()
void Set(int dx, int dy)
Pass-by-reference: The formal parameter of a method is a reference of the actual object (rect in the main), when accessing the formal parameter you are really accessing the actual object. Its value can be changes by the called method.
Calling a Function
Call by Value Calling Function Called Function Side Effect Reset(rect); Reset(MyRectangle rect) No Call by Pointer Reset(&rect); Reset(MyRectangle *rectp) Yes Call by Reference Reset(rect); Reset(MyRectangle &rectr) Yes
Function Pointers
A function name is a pointer. Taking the address of a function gives its address. Therefore: function-name == &function-name
Virtual Functions
Binding
Connecting a function call to a function body is called binding When binding is performed before the program is run (by the compiler and linker), its called early binding, static binding or compile-time binding. When binding occurs at runtime, based on the type of the object, it is called late binding, dynamic binding or runtime binding. The key words, virtual, causes late binding in C++. If a function is declared as virtual in the base class, it is virtual in all the derived classes
Four objects are newed at runtime: a Wind, a Percussion, a Stringed and a Brass. Their pointers are all upcasted into Instrument *. When executing the what functions, the correct what belonging to the classes, not the one in Instrument, are used. This is called polymorphism, since one type of function pointer, Instructment->what, actually points to different functions and therefore behaves differently at different situations. Polymorphism says which function to call is not determined by the type of the pointer but by the type of the objects its is pointed to. Polymorphism is achieved through late binding of those functions in the VTABLE. In C++, the virtual keyword causes late binding. Note Instrucment *ip behaves polymorphically too even though all the objects it points to are auto objects. Now remove virtual in front of what in Instrucment. What happends? Also note, there is no adjust function in Brass, so it is bound to the adjust in Wind.
CORBA
Common Object Request Broker Architecture http://www.omg.org/gettingstarted/corbafaq.htm A specification (standard) that allows object sharing over the network. Independent of computer, operating system, programming language, and network. Managed by OMG (Object Management Group) For each object type an interface is defined in OMG IDL (Interface Definition Language) The interface is independent of programming language, but maps to C, C++, Java, COBOL, Smalltalk, Ada, Lisp, Python, and IDLscript. CORBA 2 refers to CORBA interoperability and the IIOP protocol CORBA 3 refers to the CORBA Component Model CORBA/e refers to CORBA for embedded Less than 20 venters in total in 2007. http://corba-directory.omg.org/vendor/list.htm Web Service is a more promising alternative.