Yingcai Xiao - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

Yingcai Xiao

Description:

C++ Part II Yingcai Xiao TOC C++ Preprocessing, Compilation and Linking Preprocessing Separating Class Definition and Class Implementation Separating Class Definition ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 53
Provided by: xiao
Category:
Tags: cast | xiao | yingcai

less

Transcript and Presenter's Notes

Title: Yingcai Xiao


1
CPart II
  • Yingcai Xiao

2
  • TOC
  • Header files and Makefile
  • Command line arguments argc and argv
  • Introduction to pointers C1.ppt
  • Advanced pointer concepts
  • Arrays
  • Pointer arithmetic
  • Functional pointers
  • Virtual functions
  • CORBA

3
  • C
  • Preprocessing, Compilation and Linking

4
  • C Compilation

5
  • 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 ltiostreamsgt // system include
    files
  • inlcude Rectangle.h // user defined
    files
  • define string substitution
  • e.g. define PI 3.1415926
  • ifdef
  • endif
  • e.g ifdef DEBUG
  • avoid multiple inclusion of header files
  • ifndef
  • define
  • endif

6
  • Separating Class Definition and Class
    Implementation
  • Header (include) files
  • extension .h
  • contain function headers for non-OOP coding
    (e.g. math.h)
  • contain class definitions for OOP coding (e.g.
    iostreams.h)
  • needed for the compiler to perform type checking
  • distributed as text file with software libraries
  • implementations are hided in .cpp files
  • 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

7
  • Separating Class Definition and Class
    Implementation

Header file Rectangle.h class Rectangle
protected int x, y public Rectangle ()
Rectangle (int cx, int cy) double Area( )
Methods have only headers, no bodies
8
  • Separating Class Definition and Class
    Implementation

Implementation file Rectangle.cpp include
"Rectangle.h" using namespace std RectangleRec
tangle () x0 y0 RectangleRectangle
(int cx, int cy) xcx ycy double
RectangleArea( ) return xy
9
  • Separating Class Definition and Class
    Implementation

Implementation file Rectangle.cpp include
ltiostreamgt // system header files include
Rectangle.h // user defined header files using
namespace std int main() Rectangle
rect1 cout ltlt rect1.Area() ltlt "\n" cout ltlt
"Please enter 'q' to quit.\n " char a cin gtgt
a
10
  • 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
11
  • 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 gt define DEBUG 1

12
  • Makefile
  • create a makefile with dependencies
    (CExamples\MyRectangle)
  • sep main.o Rectangle.o MyRectangle.o
  • g main.o Rectangle.o MyRectangle.o -o sep.exe
  • main.o main.cpp
  • g -c main.cpp
  • Rectangle.o Rectangle.cpp
  • g -c Rectangle.cpp
  • MyRectangle.o MyRectangle.cpp
  • g -c MyRectangle.cpp
  • in cygwin, type
  • make
  • make f makefile
  • make will compare time stamps of the files and
    only recompile files being modified since last
    compilation.

13
  • Command Line Arguments
  • LINUX

14
  • Command Line Arguments
  • When running a program from the command line,
    you can pass arguments to the main function of
    the program.
  • e.g. mvcnc M1 V1 2
  • Each argument is passed as a null terminated
    string of characters to the main.
  • e.g. M1
  • Argument strings are grouped together by an
    array of pointers to strings of characters char
    argv
  • argc counts the total number of arguments
    passed, including the command name (e.g. mvcnc).
  • If you need an integer, use atoi to convert a
    string to an integer.
  • argc and argv are not object oriented. In Java
    and C, they are merged into one as an array of
    String objects.

15
  • Example

int main(int argc, char argv) cout ltlt
"Your have typed " for(int i 0 i lt argc
i) cout ltlt argvi ltlt " " cout ltlt
"\n" if(argc ! 4) cerr ltlt "Usage "
ltlt argv0 ltlt " Model View n( of integer to
process)." ltlt endl exit(1) int n
atoi(argv3) cout ltlt "You requested to process
" ltlt n ltlt " integers. \n" return(1)
16
  • Command Line Arguments in Cygwin
  • Cygwin a UNIX emulator for Windows.
  • All programs-gtCygwin-gtCygwin Bash Shell
  • cp T/Xiao/OOP/C/mvcnc.exe .
  • (Use / instead of \ as separators in UNIX.)
  • ./mvcnc
  • ./mvcnc M1 V2 3

17
  • Command Line Arguments in V.S.
  • All programs-gtMS Visual Studio-gt MS Visual Studio
    2005
  • File-gtOpen-gtProject/Solution
  • Select the existing project
  • In the Solution Explorer pane, click on the
    project name
  • Right click to bring up the Property menu.
  • Configuration Properties-gtDebugging-gtCommand
    Arguments
  • Type in the desired arguments.
  • e.g. M1 V2 3
  • The arguments will be passed in when Debug-gtRun

18
  • Command Line Arguments in LINUX
  • What?
  • UNIX, operating system for workstations, by Ken
    Thompson, Dennis Ritchie and Douglas McIlroy.
    http//en.wikipedia.org/wiki/Unix.
  • LINUX, UNIX for PCs, Linus Torvalds,
    http//www.linux.org/
  • Fedoro, LINUX-based PC OS, http//fedoraproject.o
    rg/
  • Start LINUX in the labs
  • Reboot PC in the lab, hit return before Windows
    starts.
  • GRUB, GRand Unified Bootloader, loads BIOs (Basic
    IO).
  • Select Fedoro 2.6.21-1.
  • User Name / Password

19
  • Command Line Arguments in LINUX
  • Use LINUX
  • Applications-gtSystem Tools-gtTerminal
  • mkdir oop
  • cd oop
  • rm file-name
  • rmdir directory-name
  • Places-gtCD/DVD Creator
  • Create, Compile and Run Programs
  • Applications-gtAccessories-gtText Editor
  • Type you code and save it to mvcnc.cpp
  • c mvcnc.cpp o mvcnc.exe (g mvcnc.cpp o
    mvcnc.exe)
  • ./mvcnc M1 V2 3

20
  • Instantiating a Class in Java

ClassName ObjectName
Rectangle rect declares a reference of class
Rectangle.
A reference to a Rectangle object.
rect
rect is the name of a memory space that stores
a reference.
A reference is an internal pointer, it needs to
point to an object before being used.
21
  • References in Java
  • Class Rectangle
  • protected  int x, y
  • Public Rectangle () x0y0
  • Public Rectangle (int cx, int cy) xcx ycy
  • Public double area( ) return xy

int width
Int height
Rectangle ()
Rectangle (int w, int h)
Area
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
y
22
  • References in Java and C

Rectangle rect  double area rect.Area() //
will not compile in Java Rectangle rect  new Rec
tangle () // Use the first constructor

rect
0x12345678
int width
Int height
Rectangle ()
Rectangle (int w, int h)
Area
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
0x12345678
y
  • Dereferencing is automatic for a reference in
    Java. (No rect)
  • double area rect.Area()
  • Please note the notation difference between a
    pointer and a name.

23
  • Value Types in Java and C

int i In both Java and C i is the name
of a memory space that stores an integer
value. int i 8 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.
8
i
24
  • Instantiating a Class in C

Class Name
In C Rectangle rect declares an object of
class Rectangle. rect is a value type.
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
rect
rect is the name of a memory space that stores
a Rectangle object.
25
  • Pointers in C

Rectangle rect  double area rect-gtArea() //
Wrong! Compiles. Try! Rectangle rect  new Rectan
gle () // Correct, use the first constructor

rect
0x12345678
int width
Int height
Rectangle ()
Rectangle (int w, int h)
Area
0
0
Rectangle ()
Rectangle (int cx, int cy)
double Area()
0x12345678
double a (rect).Area() double b
rect-gtArea()
  • 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.)

26
  • Value Types v.s. Pointer/Reference Types
  • Variables of value types have the object (not the
    pointer/reference) memories allocated by the
    compiler and can be used as an object directly
    without new.
  • Variables of pointer/reference types have the
    pointer/reference (not the object) memories
    allocated by the compiler and can not be used as
    an object directly without new.
  • In Java and C
  • Rectangle rect // rect is a reference
  • rect  new Rectangle (3, 4)  // rect reference
    an object of allocated memory location
  • double a rect.Area() // use . to
    dereference
  • In C
  • Rectangle rect // rect is a pointer
  • rect  new Rectangle (3, 4)  // rect points to
    an object of allocated memory location
  • double a (rect).Area() // use and . to
    dereference
  • double b rect-gtArea() // use -gt to
    dereference
  • In C
  • Rectangle rect // rect is a value type object
    of allocated memory location
  • double a rect.Area() // use . to
    dereference

27
  • Value Types v.s. Pointer/Reference Types
  • Value Types are Stack Objects
  • memories allocated at compile time on the stack
  • automatically freed when the objects are out of
    scope
  • less overhead, code runs faster
  • less flexible, sizes need to be known at compile
    time
  • Pointer / Reference Types point to Heap Objects
  • memory are dynamically allocated at run time on
    the heap
  • stays there even if the pointers/references are
    out of scope
  • dynamically allocated memories need to be freed
    / deleted manually
  • more flexible, sizes need not to be known at
    compile time
  • more overhead, code runs slower

28
  • Free Dynamically Allocated Memory

In Java, dynamically allocated memories are
automatically freed by the garbage collector. In
C, there is no garbage collector. dynamically
allocated memories needs to be freed manually
using free (C syntax) or delete (C
syntax). Rectangle rect // rect is a
pointer rect  new Rectangle (1, 2)  // rect
points to an object of allocated memory location
double a rect-gtArea() // use -gt to
dereference free(rect) // delete
rect rect  new Rectangle (3, 4)  // rect
points to another object of allocated memory
location double a rect-gtArea() // use -gt
to dereference free(rect) // delete rect
29
  • The Five1 Nevers
  • Never dereference pointers before allocating
    memories to them.
  • Never dereference pointers before assigning
    values to the objects they point to. Dynamically
    allocated memories are not clean in C.
  • Dont forget to free the dynamically allocated
    memories after using them. This will cause memory
    leak. No one else will be able to use the
    memories allocated by your program before you
    free them.
  • Never free the same memory more than once.
  • Never free stack memory (used by value
    variables)
  • C compilers allow you to make all of the above
    mistakes. Causing big problems for others that
    you even dont know.
  • To avoid the problems, assign 0 to pointers when
    they are created and deleted. Only use the
    pointers when they are not equal to 0, i.e.,
    if(pointer) is true.
  • Rectangle rect 0 // rect is a pointer
  • rect  new Rectangle (1, 2)  // rect points to
    an object of allocated memory location
  • if(rect) double a rect-gtArea() // use -gt
    to dereference
  • if(rect) free(rect) rect 0 // free the
    memory and reset the pointer to 0

30
  • 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 ltlt p1.x ltlt ltlt p1.y
  • cout ltlt p2.x ltlt ltlt p2.y
  • p2.x 3 p2.y 4
  • cout ltlt p1.x ltlt ltlt p1.y
  • cout ltlt p2.x ltlt ltlt p2.y
  • Point p3 p3 p1
  • cout ltlt p1.x ltlt ltlt p1.y
  • cout ltlt p3-gtx ltlt ltlt p3-gty
  • p3-gtx 5 p3-gty 6
  • cout ltlt p1.x ltlt ltlt p1.y
  • cout ltlt p3-gtx ltlt ltlt p3-gty

31
  • 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 ltlt p3-gtx ltlt ltlt p3-gty
  • p3 p1
  • cout ltlt p3-gtx ltlt ltlt p3-gty
  • delete p3
  • p3 new Point()
  • cout ltlt p3-gtx ltlt ltlt p3-gty
  • delete p3
  • free (p3)

32
  • Code Example
  • Find and describe the errors in the following
    code.
  • p3 new Point()
  • p3-gtx 3 p3-gty 4
  • Point p4 p3
  • cout ltlt p4-gtx ltlt ltlt p4-gty
  • delete p3
  • delete p4
  • Point p5
  • int i 1
  • if(i 1)
  • Point p6
  • p5 p6
  • Point p7 new Point()
  • cout ltlt p5-gtx ltlt ltlt p5-gty

33
Arrays in C
int a2 a0 5 a1 10 cout ltlt a0 ltlt
ltlt a1 // stack objects, size has to be a
constant and cant be changed at runtime. int
size 2 int bsize // will not compile in
C // An array name represents a constant
pointer, cant change its value.
5 10
a
int size cin gtgt size // assuming user entered
2. int p // a pointer p new intsize
p0 5 p1 10 cout ltlt p0 ltlt ltlt
p1 // heap objects dynamically allocated at
runtime, size can be a variable delete p //
free the memory.
5 10
0xff
p
int p2 // a pointer p2 a p20 50
p21 100 cout ltlt a0 ltlt ltlt a1 delete
p2 // Dont do this.
34
Pointer Arithmetic
Pointers can be moved back and forth by one
object with and --
5 10
0xff00
p
0xff04
int a2 5,10 int p // a pointer p a
cout ltlt p0 p cout ltlt p0 p-- cout ltlt
p0 p cout ltlt p1 Watch out where you
are with a pointer!
35
  • TICV1C15
  • Type Cast
  • Virtual Function
  • Polymorphism

36
  • Type Cast

class Rectangle protected int x, y public
Rectangle () x0y0 Rectangle (int cx,
int cy) xcx ycy double Area( ) return
xy Rectangle rect
int width
Int height
Rectangle ()
Rectangle (int w, int h)
Area
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
y
37
  • Type Cast

class Rectangle protected int x, y public
Rectangle () x0y0 Rectangle (int cx,
int cy) xcx ycy double Area( ) return
xy
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
y
class MyRectangle public Rectangle public
void Set(int dx, int dy)xdx ydy
MyRectangle rect2
void Set(int dx, int dy)
38
  • Down Cast casting a pointer to a child class

Rectangle rect1 Rectangle p1 rect1 cout ltlt
p1-gtArea() // ok p1-gtSet(1,1) // wont
compile MyRectangle p2 // implicit cast
to a child class, will not compile p2 p1
//explicit cast to a child class, allowed p2
(MyRectangle ) p1 cout ltlt p2-gtArea() //
ok p2-gtSet(1,1) //will compile, cause runtime
error 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.
rect1
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
0xff00
p1
y
0xff00
p2
39
  • Up Cast casting a pointer to the parent class

MyRectangle rect2 MyRectangle p2
rect2 cout ltlt p2-gtArea() //
ok p2-gtSet(1,1) // ok Rectangle p1 p1 p2
// implicit cast to parent, ok //explicit
cast to parent, ok p1 (Rectangle ) p2
cout ltlt p1-gtArea() // ok p1-gtSet(1,1) //
will not compile MyRectangle p3 //explicit cast
to child class, ok p3 (MyRectangle ) p1
cout ltlt p3-gtArea() // ok p3-gtSet(1,1) //
ok 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-gtDraw() Down cast should only be used when
the original object is an instance of the child
class.
rect2
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
0xff00
p2
y
0xff00
p1
void Set(int dx, int dy)
40
  • Passing by Value

void Reset(MyRectangle rectf) // rectf is the
formal parameter, it has its own
memory. rectf.Set(1,1) cout ltlt rectf.Area()
// 1 void main () MyRectangle rect cout ltlt
rect.Area() // 0 Reset(rect) // rect is the
actual parameter cout ltlt 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.
41
  • Passing by Pointer

void Resetp(MyRectangle rectp) // rectp is
the formal parameter, a pointer. rectp-gtSet(1,1)
cout ltlt rectp-gtArea() void main
() MyRectangle rect cout ltlt rect.Area() //
0 Resetp(rect) // rect is the actual
parameter cout ltlt rect.Area() //
1 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.
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
0xff00
rectp
y
rect
void Set(int dx, int dy)
42
  • Reference

A reference is an alias of an object, it
references the same memory of the object. void
main () MyRectangle rect cout ltlt rect.Area()
// 0 // declare and initialize a
reference MyRectangle rectr rect cout ltlt
rectr.Area() // use the reference as the
object
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
rectr
y
rect
void Set(int dx, int dy)
43
  • Passing by Reference

Void Reset(MyRectangle rectr) // rectr is
the formal parameter, a reference. rectr.Set(1,1)
cout ltlt rectr.Area() // 1 void main
() MyRectangle rect cout ltlt rect.Area() //
0 // rect is the actual parameter, its reference
is passed. Reset(rect) cout ltlt rect.Area()
// 1 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.
0
0
Rectangle () x0y0
Rectangle (int cx, int cy)
double Area()
x
rectf
y
rect
void Set(int dx, int dy)
44
  • Calling a Function

Call by Value Call by Pointer Call by Reference
Calling Function Reset(rect) Reset(rect) Reset(rect)
Called Function Reset(MyRectangle rect) Reset(MyRectangle rectp) Reset(MyRectangle rectr)
Side Effect No Yes Yes
45
  • Function Pointers
  • A function name is a pointer.
  • Taking the address of a function gives its
    address.
  • Therefore function-name function-name
  • For challenges and fun, try the examples at
  • http//www.cs.uakron.edu/xiao/oop/fun-ptrs.html
  • A function returning an int A function
    returning an int pointer A function prototype
  • A function pointer to a function returning an int
    A function pointer to a function returning an
    int pointer
  • An array of function pointers to functions
    returning ints An array of function pointers to
    functions returning int pointers A pointer to a
    function pointer to a function returning an int
    An array of pointers to function pointers to
    functions returning ints A pointer to a
    function pointer to a function returning an int
    pointer An array of pointers to function
    pointers to functions returning int pointers

46
  • Virtual Functions

47
  • 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

48
  • Late Binding in C
  • The keyword virtual tells the compiler it should
    not perform early binding
  • The compiler creates a single table (called the
    VTABLE) for each class that contains virtual
    functions.
  • The compiler places the addresses of the
    virtual functions for that particular class in
    the VTABLE.
  • In each class with virtual functions, it
    secretly places a pointer, called the vpointer
    (abbreviated as VPTR), which points to the VTABLE
    for that object.
  • When you make a virtual function call through a
    base-class pointer (that is, when you make a
    polymorphic call), the compiler quietly inserts
    code to fetch the VPTR and look up the function
    address in the VTABLE, thus calling the correct
    function and causing late binding to take place.

49
  • Late Binding Example

50
  • Instrument.cpp an Example of Late Binding and
    Polymorphism in C
  • 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-gtwhat, 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.

51
  • 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.

52
  • IIOP and GIOP
  • IIOP (Internet Inter-ORB Protocol) is the
    implementation of GIOP for TCP/IP
  • General Inter-ORB Protocol (GIOP) is the
    abstract protocol for which object request
    brokers (ORBs) communicate through the network.
  • Both specified by OMG.
  • Three parts of GIOP
  • Common Data Representation (CDR)
  • Interoperable Object Reference (IOR)
  • The defined message formats
  • Java CORBA Examples
  • http//java.sun.com/developer/codesamples/idl.htm
    l
  • Real-time CORBA examples
  • http//www.omg.org/news/meetings/workshops/present
    ations/realtime_emb_presentations/Real-time_Tutori
    al_Slides/RTCORBA_workshop_examples.pdf
Write a Comment
User Comments (0)
About PowerShow.com