#OOP_D_ITS - 2nd - C++ Getting Started

771 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
771
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • The Machine Languagebinary codeThe High-Level Programming LanguageC, C++, JavaThe Human LanguageIndonesian, English, Arabic
  • C diimplementasikan oleh Dennis Ritchie dari AT&T Laboratories dengan menggunakan sistem operasi UNIX.C merupakan hasil dari proses pengembangan dari BCPL (Basic Combined Programming Language).BCPL diciptakan oleh Martin Richard, dan mempengaruhi bahasa B yang diciptakan oleh Ken Thomson.Bahasa B dikembangkan menjadi bahasa C pada tahun 1970-an.Tahun 1983, didirikan sebuah komite untuk menciptakan ANSI standar untuk bahasa C. (yang dipakai pada Desember 1989)Bahasa C yang diciptakan oleh Kerninghan dan Ritchie dikenal dengan ANSI C  digunakan oleh ISO sehingga menghasilkan ISO Standard C.Tahun 1980, Bjarne Stroustrup dari AT&T Bell Laboratories mulai mengembangkan bahasa C  C++ tahun 1985.
  • #OOP_D_ITS - 2nd - C++ Getting Started

    1. 1. C++ Getting Started<br />01/09/2009<br />1<br />Hadziq Fabroyir - Informatics ITS<br />
    2. 2. Language Classification<br />01/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />2<br />If the line is not busy, connect o the Internet, else, wait…<br />If (line!= busy)<br /> connect (Internet);<br />Else<br /> wait (5)…<br />100010001010001000111011111110111010111101010111010011…..<br />
    3. 3. Preparing a program for execution<br /><ul><li>Steps:
    4. 4. Editing
    5. 5. Create your source code
    6. 6. Compilation
    7. 7. Linking
    8. 8. Create binary code
    9. 9. Loading
    10. 10. Your program is loaded in memory
    11. 11. Execution
    12. 12. Then is executed</li></ul>01/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />3<br />
    13. 13. History C / C++<br />01/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />4<br />
    14. 14. Part 1: Mechanics<br />
    15. 15. C++ is a superset of C<br />New Features include<br />Classes (Object Oriented)<br />Templates (Standard Template Library)<br />Operator Overloading<br />Slightly cleaner memory operations<br />
    16. 16. Segment.h<br />Segment.cpp<br />#include “Segment.h”<br />#include “Point.h”<br />Segment::Segment()<br />{<br /> m_p0 = new Point(0, 0);<br /> m_p1 = new Point(1, 1);<br />}<br />Segment::~Segment()<br />{<br /> delete m_p0;<br /> delete m_p1;<br />}<br />#ifndef __SEGMENT_HEADER__<br />#define __SEGMENT_HEADER__<br />class Point;<br />class Segment<br />{<br />public:<br /> Segment();<br /> virtual ~Segment();<br />private:<br /> Point *m_p0, *m_p1;<br />};<br />#endif // __SEGMENT_HEADER__<br />Some C++ code<br />
    17. 17. #include “Segment.h”<br />#include &lt;iostream&gt;<br />#include<br />Insert header file at this point.<br />Use library header.<br />
    18. 18. Header Guards<br />#ifndef __SEGMENT_HEADER__<br />#define __SEGMENT_HEADER__<br />// contents of Segment.h<br />// ...<br />#endif<br />To ensure it is safe to include a file more than once.<br />
    19. 19. Header Guards<br />#ifndef __SEGMENT_HEADER__<br />#define __SEGMENT_HEADER__<br />// contents of segment.H<br />//...<br />#endif<br />If this variable is <br />not defined …<br />Define it !<br />End of guarded area .<br />
    20. 20. Circular Includes<br />gui.h<br />#include “controller.h”<br />// define gui<br />// ...<br />controller.h<br />#include “gui.h”<br />class Controller<br />{<br />//...<br />private:<br /> Gui* myGui;<br />//...<br />};<br />What’s wrong with this picture?<br />How do we fix it?<br />
    21. 21. Forward Declarations<br />gui.h<br />//Forward Declaration<br />class Controller;<br />// define gui<br />// ...<br />controller.h<br />//Forward declaration<br />class Gui;<br />class Controller<br />{<br />//...<br />private:<br /> Gui* myGui;<br />//...<br />};<br />In header files, only include what you must.<br />If only pointers to a class are used, use forward declarations.<br />
    22. 22. Preprocessor<br />Inlines #includes etc.<br />Compiler<br />Translates to machine code<br />Associates calls with functions<br />Linker<br />Associates functions with definitions <br />Compilation<br />Object files<br />Executable<br />External Libraries, libc.so, libcs123.so<br />
    23. 23. Part 2: Basics<br />
    24. 24. What is a pointer?<br />int x = 10;<br />int *p;<br />p = &x;<br />p gets the address of x in memory.<br />p<br />x<br />10<br />
    25. 25. What is a pointer?<br />int x = 10;<br />int *p;<br />p = &x;<br />*p = 20;<br />*p is the value at the address p.<br />p<br />x<br />20<br />
    26. 26. What is a pointer?<br />Declares a pointer <br />to an integer<br />int x = 10;<br />int *p = NULL;<br />p = &x;<br />*p = 20;<br />& is address operator<br /> gets address of x <br />*dereference operator<br /> gets value at p<br />
    27. 27. Allocating memory usingnew<br />Point *p = new Point(5, 5);<br />new can be thought of a function with slightly strange syntax<br />new allocates space to hold the object.<br />new calls the object’s constructor.<br />new returns a pointer to that object.<br />
    28. 28. Deallocating memory using delete<br />// allocate memory<br />Point *p = new Point(5, 5);<br />...<br />// free the memory<br />delete p;<br />For every call to new, there must be<br />exactly one call to delete.<br />
    29. 29. Using new with arrays<br />int x = 10;<br />int* nums1 = new int[10]; // ok<br />int* nums2 = new int[x]; // ok<br />Initializes an array of 10 integers on the heap.<br />C++ equivalent of the following C code<br />int* nums = (int*)malloc(x * sizeof(int));<br />
    30. 30. Using newwith multidimensional arrays<br />int x = 3, y = 4;<br />int** nums3 = new int[x][4];// ok<br />int** nums4 = new int[x][y];// BAD!<br />Initializes a multidimensional array<br />Only the first dimension can be a variable. The rest must be constants.<br />Use single dimension arrays to fake multidimensional ones<br />
    31. 31. Using delete on arrays<br />// allocate memory<br />int* nums1 = new int[10];<br />int* nums3 = new int[x][4][5];<br />...<br />// free the memory<br />delete[] nums1;<br />delete[] nums3;<br />Have to use delete[].<br />
    32. 32. Destructors<br />deletecalls the object’s destructor.<br />delete frees space occupied by the object.<br />A destructor cleans up after the object.<br />Releases resources such as memory.<br />
    33. 33. Destructors – an Example<br />class Segment<br />{<br />public:<br /> Segment();<br />virtual ~Segment();<br />private:<br /> Point *m_p0, *m_p1;<br />};<br />
    34. 34. Destructors – an Example<br />Segment::Segment()<br />{<br /> m_p0 = new Point(0, 0);<br /> m_p1 = new Point(1, 1);<br />}<br />Segment::~Segment()<br />{<br /> if (m_p0) delete m_p0;<br /> if (m_p1) delete m_p1;<br />}<br />
    35. 35. New vs Malloc<br /><ul><li>Never mix new/delete with malloc/free</li></li></ul><li>Classes vs Structs <br />Default access specifier for classes is private; for structs it is public<br />struct Point {<br /> int x;<br /> int y;<br /> // convenience constructor<br /> Point(int a, int b) <br /> : x(a), y(b)<br /> { }<br /> // @returns distance to another point<br /> double distance(const Point &pnt) {<br /> int dx = m_x – pnt.x;<br /> int dy = m_y – pnt.y;<br /> return math.sqrt(dx*dx + dy*dy);<br /> }<br />};<br />class Segment {<br /> public:<br /> Segment();<br /> virtual ~Segment();<br /> void setPoints(int x0, int y0, int x1, int y1);<br /> protected:<br /> Point *m_p0, *m_p1;<br />};<br />void Segment::setPoints(int x0, int y0, int x1, int y1) {<br /> m_p0 = new Point(x0, y0); <br /> m_p1 = new Point(x1, y1);<br />}<br />
    36. 36. Segment.h<br />#ifndef __SEGMENT_HEADER__<br />#define __SEGMENT_HEADER__<br />class Point;<br />class Segment {<br /> public:<br /> Segment();<br /> virtual ~Segment();<br /> protected:<br /> Point *m_p0, *m_p1;<br />};<br />#endif // __SEGMENT_HEADER__<br />Summary with Header File<br />header file<br />begin header guard<br />forward declaration<br />class declaration<br />constructor<br />destructor<br />member variables<br />need semi-colon<br />end header guard<br />
    37. 37. Part 3: References<br />
    38. 38. Passing by value<br />void Math::square(int i) {<br /> i = i*i;<br />}<br />int main() {<br /> int i = 5;<br /> Math::square(i);<br /> cout &lt;&lt; i &lt;&lt; endl;<br />}<br />
    39. 39. Passing by reference<br />void Math::square(int &i) {<br /> i = i*i;<br />}<br />int main() {<br /> int i = 5;<br /> Math::square(i);<br /> cout &lt;&lt; i &lt;&lt; endl;<br />}<br />
    40. 40. What is a reference?<br />An alias – another name for an object.<br /> int x = 5;<br /> int &y = x; // y is a<br /> // reference to x<br /> y = 10;<br />What happened to x?<br />What happened to y? <br />
    41. 41. What is a reference?<br />An alias – another name for an object.<br /> int x = 5;<br /> int &y = x; // y is a<br /> // reference to x<br /> y = 10;<br />What happened to x?<br />What happened to y? – y is x.<br />
    42. 42. How are references different from Pointers?<br />
    43. 43. ☺~ Any Question ?¿ ~☺<br />[ 35 ]<br />Hadziq Fabroyir - Informatics ITS<br />01/09/2009<br />
    44. 44. For your practice …<br />Page 66 ► 2.9 <br />Page 67 ► 2.12 , 2.13 , 2.14 <br />Page 68 ► 2.19 , 2.23 , 2.26 <br />Page 69 ► 2.27 , 2.29<br />01/09/2009<br />Hadziq Fabroyir - Informatics ITS<br />36<br />

    ×