Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Introduction to C++<br />Dr. Darren Dancey<br />
Hello World in C++<br />#include &lt;iostream&gt;<br />using namespace std;<br />int main()<br />{<br />cout &lt;&lt; &quo...
Hello World in Java<br />import java.io.*;<br />public class Helloworld<br />{<br />   public static  void main(Stringargs...
Much is the same…<br />C++<br />#include&lt;iostream&gt;<br /> <br />usingnamespace std;<br /> <br />intmain(char* args[],...
...java<br />Java<br />public class week1{<br />public static void main(Stringargs[])<br />{<br />for(inti = 2; i &lt; 100...
Compiling a C++ Program <br />C:Usersdarren&gt;cd %HOMEPATH%<br />C:Usersdarren&gt;notepad helloworld.cpp<br />C:Usersdarr...
Input and Output in C++<br />Input and Output handled by a library<br />I/O part of the C++ Standard Library<br />Stream B...
The coutObject<br />Instance of type ostream<br />Defined in Standard Library<br />Bound to Console (screen)<br />Characte...
cout Examples<br />cout &lt;&lt; “Hello, World!”;<br />cout &lt;&lt; 5;<br />cout &lt;&lt; 5.1;<br />intmyvar = 5 <br />co...
endl End Line<br />Putting endl onto a stream moves to the next line.<br />cout &lt;&lt; “This is on line 1” &lt;&lt; endl...
cin<br />Part of Standard Libaray<br />Instance of istream<br />Bound to the keyboard<br />The &gt;&gt; operator is overri...
cin examples<br />	char mychar;<br />cin &gt;&gt; mychar;<br />intmyint;<br />cin &gt;&gt; myint;<br />	string mystr;<br /...
I/O Example<br />int age;<br /> <br />cout &lt;&lt; &quot;Please enter you age &quot;;<br />cin &gt;&gt; age;<br /> <br />...
Pointers<br />
Pointers<br />int myvar1= 5;<br />int myvar2 = 10;<br />double mydbl = 5.3;<br />myvar1<br />myvar2<br />mydbl<br />5<br /...
Pointers<br />int myvar1= 5;<br />int myvar2 = 10;<br />double mydbl = 5.3;<br />int* ptrVar;<br />ptrVar  = &myvar2;<br /...
Pointers<br />int myvar1= 5;<br />int myvar2 = 10;<br />double mydbl = 5.3;<br />int* ptrVar ;<br />ptrVar = &myvar2;<br /...
Arrays in C++<br />int myarray[5] = {10, 20, 30, 50, 5};<br />	for (inti =0 ; i &lt; 5; i++){<br />cout &lt;&lt; myarray[i...
Pointer Arithmetic<br />int myarray[5] = {10, 20, 30, 50, 5};<br />int *ptr;<br />ptr = &myarray[0];<br />cout &lt;&lt; &q...
C/C++ can be terse<br />while(*ptrString2++ = *ptrString1++);<br />
Pointers<br />Pointers are variables that hold a memory address<br />The * operator get the value in the memory address <b...
Pointers and Pass by Reference<br />C++ uses pass by value that means the parameters of a function are copies of the varia...
Why By Reference<br />Want to avoid copying large amounts of data.<br />Want the function to modify the value passed in.<b...
Pointers as a Solution<br />Myfunc (int *foo, int *bar){ …}<br />Myfunc(&a, &b)<br />Still pass-by-value but pass in the v...
Objects in C++<br />Dog<br />age: Integer<br />speak()<br />walk( location ) :string<br />
Dog the .h file<br />class dog<br />{<br />public:<br />int age;<br />	char* speak();<br />moveTo(intx, inty);<br />};<br ...
Dog the .cpp file<br />#include &quot;dog.h&quot;<br />#include &lt;iostream&gt;<br />using namespace std;<br />void Dog::...
Initializing a Dog <br />C++<br />#include &lt;iostream&gt;<br />#include &quot;dog.h&quot;<br />using namespace std;<br /...
Scooby a Dynamic Dog <br />C++<br />Dog* scooby = new Dog();<br />(*scooby).speak();  // these calls do<br />scooby-&gt;sp...
Objects and Pointers<br />int myvar1= 5;<br />int myvar2 = 1;<br />double mydbl = 5.3;<br />myvar1<br />myvar2<br />mydbl<...
Pointers<br />int myvar1= 5;<br />int myvar2 = 1;<br />double mydbl = 5.3;<br />Dog* scooby = new Dog();<br />scooby-&gt;a...
CallStackvs Heap<br />Code example callStackExample<br />
Directed Study/Further Reading<br />C++ Pointers <br />http://www.cplusplus.com/doc/tutorial/pointers/<br />Binky Video<br...
Upcoming SlideShare
Loading in …5
×

Cplusplus

3,011 views

Published on

Published in: Health & Medicine, Technology
  • Be the first to comment

Cplusplus

  1. 1. Introduction to C++<br />Dr. Darren Dancey<br />
  2. 2. Hello World in C++<br />#include &lt;iostream&gt;<br />using namespace std;<br />int main()<br />{<br />cout &lt;&lt; &quot;Hello World!&quot; &lt;&lt; endl;<br />}<br />C++<br />
  3. 3. Hello World in Java<br />import java.io.*;<br />public class Helloworld<br />{<br /> public static void main(Stringargs[])<br /> {<br />System.out.println(&quot;Hello World!&quot;);<br /> }<br />}<br />Java<br />
  4. 4. Much is the same…<br />C++<br />#include&lt;iostream&gt;<br /> <br />usingnamespace std;<br /> <br />intmain(char* args[], intargc)<br />{<br />for(inti = 2; i &lt; 100; i++){<br />bool flag = true; <br />for (intj = 2; j &lt;= i/2; j++ ){<br />if (i%j == 0){<br /> flag = false;<br />break;<br /> }<br /> }<br />if (flag == true){<br />cout &lt;&lt; i &lt;&lt; endl;<br /> }<br /> }<br />cin.get();<br /> <br />}<br />
  5. 5. ...java<br />Java<br />public class week1{<br />public static void main(Stringargs[])<br />{<br />for(inti = 2; i &lt; 100; i++){<br />booleanflag = true; <br /> for (intj = 2; j &lt;= i/2; j++ ){<br /> if (i%j == 0){<br /> flag = false;<br /> break;<br /> }<br /> }<br /> if (flag == true){<br />System.out.println(i);<br /> }<br /> }<br />}<br />}<br />
  6. 6. Compiling a C++ Program <br />C:Usersdarren&gt;cd %HOMEPATH%<br />C:Usersdarren&gt;notepad helloworld.cpp<br />C:Usersdarren&gt;cl /EHschelloworld.cpp<br />Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86<br />Copyright (C) Microsoft Corporation. All rights reserved.<br />helloworld.cpp<br />Microsoft (R) Incremental Linker Version 8.00.50727.762<br />Copyright (C) Microsoft Corporation. All rights reserved.<br />/out:helloworld.exe<br />helloworld.obj<br />C:Usersdarren&gt;helloworld<br />Hello, World!<br />C:Usersdarren&gt;<br />
  7. 7. Input and Output in C++<br />Input and Output handled by a library<br />I/O part of the C++ Standard Library<br />Stream Based I/O<br />Stream Based Abstraction provides a common interface to I/O for harddisks, terminals, keyboards and the network <br />
  8. 8. The coutObject<br />Instance of type ostream<br />Defined in Standard Library<br />Bound to Console (screen)<br />Character Output Stream<br />Uses Operator overloading of &lt;&lt; (left bit shift) to take arguments to print<br />Similar to <br />cout &lt;&lt; &quot;Hello World!”<br />cout.writeline(“Hello World”) <br />
  9. 9. cout Examples<br />cout &lt;&lt; “Hello, World!”;<br />cout &lt;&lt; 5;<br />cout &lt;&lt; 5.1;<br />intmyvar = 5 <br />cout &lt;&lt; myvar;<br />string course = “SE5301”;<br />cout &lt;&lt; course;<br />cout &lt;&lt; course &lt;&lt; “ is a “ &lt;&lt; 5 &lt;&lt; “star course”; <br />
  10. 10. endl End Line<br />Putting endl onto a stream moves to the next line.<br />cout &lt;&lt; “This is on line 1” &lt;&lt; endl;<br />cout &lt;&lt; “This is on line 2” &lt;&lt; endl;<br />
  11. 11. cin<br />Part of Standard Libaray<br />Instance of istream<br />Bound to the keyboard<br />The &gt;&gt; operator is overridden to provide input method<br />char mychar;<br />cin &gt;&gt; mychar;<br />
  12. 12. cin examples<br /> char mychar;<br />cin &gt;&gt; mychar;<br />intmyint;<br />cin &gt;&gt; myint;<br /> string mystr;<br />cin &gt;&gt; mystr;<br />
  13. 13. I/O Example<br />int age;<br /> <br />cout &lt;&lt; &quot;Please enter you age &quot;;<br />cin &gt;&gt; age;<br /> <br /> <br />if (age &lt; 18){<br />cout &lt;&lt; &quot;Being &quot; &lt;&lt; age;<br />cout &lt;&lt; &quot; years old you are too young to vote&quot; &lt;&lt; endl;<br /> }else{<br />cout &lt;&lt; &quot;You are old enough to vote&quot; &lt;&lt; endl;<br /> }<br />cout &lt;&lt; &quot;Thank you for using vote-o-matic&quot; &lt;&lt; endl;<br />
  14. 14. Pointers<br />
  15. 15. Pointers<br />int myvar1= 5;<br />int myvar2 = 10;<br />double mydbl = 5.3;<br />myvar1<br />myvar2<br />mydbl<br />5<br />1<br />5.3<br />
  16. 16. Pointers<br />int myvar1= 5;<br />int myvar2 = 10;<br />double mydbl = 5.3;<br />int* ptrVar;<br />ptrVar = &myvar2;<br />cout &lt;&lt; *ptrVar ; // will print out 10 (value in myvar2)<br />myvar1<br />myvar2<br />mydbl<br />ptrVar<br />5<br />10<br />5.3<br />Address<br />Of myvar2<br />
  17. 17. Pointers<br />int myvar1= 5;<br />int myvar2 = 10;<br />double mydbl = 5.3;<br />int* ptrVar ;<br />ptrVar = &myvar2;<br />*ptrVar = 20; <br />cout &lt;&lt; myvar2; <br />myvar1<br />myvar2<br />mydbl<br />ptrVar<br />myvar2<br />5<br />10<br />5.3<br />Address<br />Of myvar2<br />20<br />
  18. 18. Arrays in C++<br />int myarray[5] = {10, 20, 30, 50, 5};<br /> for (inti =0 ; i &lt; 5; i++){<br />cout &lt;&lt; myarray[i] &lt;&lt; &quot; &quot; ;<br /> }<br />cin.get();<br />File:SimpleArrayTraversal<br />
  19. 19. Pointer Arithmetic<br />int myarray[5] = {10, 20, 30, 50, 5};<br />int *ptr;<br />ptr = &myarray[0];<br />cout &lt;&lt; &quot;The value of *ptr is &quot; &lt;&lt; *ptr &lt;&lt; endl;<br />cout &lt;&lt; &quot;The value of *(ptr+2) is &quot; &lt;&lt; *(ptr+2) &lt;&lt; endl;<br />cout &lt;&lt; &quot;Array traversal with ptr&quot; &lt;&lt; endl;<br /> for (inti = 0; i &lt; 5; i++){<br />cout &lt;&lt; *(ptr+i) &lt;&lt; endl;<br /> }<br />cout &lt;&lt; &quot;Array Traversal with moving ptr&quot; &lt;&lt; endl;<br /> for (inti = 0; i &lt; 5; i++){ <br />cout &lt;&lt; *ptr++ &lt;&lt; endl;<br /> }<br />File:pointer Arithmetic<br />
  20. 20. C/C++ can be terse<br />while(*ptrString2++ = *ptrString1++);<br />
  21. 21. Pointers<br />Pointers are variables that hold a memory address<br />The * operator get the value in the memory address <br />*ptr get the value stored at the memory address in ptr.<br />The & gets the memory address of a variable<br />ptr = &myvar get the memory address of myvar and stores it in ptr<br />
  22. 22. Pointers and Pass by Reference<br />C++ uses pass by value that means the parameters of a function are copies of the variables passed in.<br />void myfunc (intfoo, int bar)<br />{…}<br />Myfunc(a,b);<br />The values in a and b are copied into foo and bar.<br />
  23. 23. Why By Reference<br />Want to avoid copying large amounts of data.<br />Want the function to modify the value passed in.<br />
  24. 24. Pointers as a Solution<br />Myfunc (int *foo, int *bar){ …}<br />Myfunc(&a, &b)<br />Still pass-by-value but pass in the value of the memory addresses of a and b.<br />When the values pointed to by foo and bar are changed it will be changing a and b.<br />
  25. 25. Objects in C++<br />Dog<br />age: Integer<br />speak()<br />walk( location ) :string<br />
  26. 26. Dog the .h file<br />class dog<br />{<br />public:<br />int age;<br /> char* speak();<br />moveTo(intx, inty);<br />};<br />C++<br />
  27. 27. Dog the .cpp file<br />#include &quot;dog.h&quot;<br />#include &lt;iostream&gt;<br />using namespace std;<br />void Dog::speak()<br />{<br />cout &lt;&lt; &quot;Woof Woof!&quot; &lt;&lt; endl;<br />}<br />C++<br />
  28. 28. Initializing a Dog <br />C++<br />#include &lt;iostream&gt;<br />#include &quot;dog.h&quot;<br />using namespace std;<br />intmain(char* args[], intargc){<br />cout &lt;&lt; &quot;Dog Program&quot; &lt;&lt; endl;<br /> Dog fido; //on stack<br /> //intmyint<br />fido.age = 5;<br />fido.speak();<br />cin.get();<br />}<br />
  29. 29. Scooby a Dynamic Dog <br />C++<br />Dog* scooby = new Dog();<br />(*scooby).speak(); // these calls do<br />scooby-&gt;speak(); //the same thing<br />scooby-&gt;age = 6;<br />
  30. 30. Objects and Pointers<br />int myvar1= 5;<br />int myvar2 = 1;<br />double mydbl = 5.3;<br />myvar1<br />myvar2<br />mydbl<br />5<br />1<br />5.3<br />
  31. 31. Pointers<br />int myvar1= 5;<br />int myvar2 = 1;<br />double mydbl = 5.3;<br />Dog* scooby = new Dog();<br />scooby-&gt;age = 5; <br />myvar1<br />myvar2<br />mydbl<br />scooby<br />age<br />5<br />1<br />5.3<br />Memory<br />address<br />5<br />
  32. 32. CallStackvs Heap<br />Code example callStackExample<br />
  33. 33. Directed Study/Further Reading<br />C++ Pointers <br />http://www.cplusplus.com/doc/tutorial/pointers/<br />Binky Video<br />http://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/<br />http://cslibrary.stanford.edu/104/<br />

×