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