Introduction to C++Dr. Darren Dancey
Hello World in C++#include <iostream>using namespace std;int main(){cout << "Hello World!" << endl;}C++
Hello World in Javaimport java.io.*;public class Helloworld{   public static  void main(Stringargs[])   {System.out.println("Hello World!");   }}Java
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(); }
...javaJavapublic 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);		}	}}}
Compiling a C++ Program C:\Users\darren>cd %HOMEPATH%C:\Users\darren>notepad helloworld.cppC:\Users\darren>cl /EHschelloworld.cppMicrosoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86Copyright (C) Microsoft Corporation.  All rights reserved.helloworld.cppMicrosoft (R) Incremental Linker Version 8.00.50727.762Copyright (C) Microsoft Corporation.  All rights reserved./out:helloworld.exehelloworld.objC:\Users\darren>helloworldHello, World!C:\Users\darren>
Input and Output in C++Input and Output handled by a libraryI/O part of the C++ Standard LibraryStream Based I/OStream Based Abstraction provides a common interface to I/O for  harddisks, terminals, keyboards and the network
The coutObjectInstance of type ostreamDefined in Standard LibraryBound to Console (screen)Character Output StreamUses Operator overloading of << (left bit shift) to take arguments to printSimilar to cout << "Hello World!”cout.writeline(“Hello World”)
cout Examplescout << “Hello, World!”;cout << 5;cout << 5.1;intmyvar = 5 cout <<  myvar;string course = “SE5301”;cout << course;cout << course << “ is a “ << 5 << “star course”;
endl End LinePutting endl onto a stream moves to the next line.cout << “This is on line 1” << endl;cout << “This is on line 2” << endl;
cinPart of Standard LibarayInstance of istreamBound to the keyboardThe >> operator is overridden to provide input methodchar mychar;cin >> mychar;
cin examples	char mychar;cin >> mychar;intmyint;cin >> myint;	string mystr;cin >> mystr;
I/O Exampleint 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;
Pointers
Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;myvar1myvar2mydbl515.3
Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;int* ptrVar;ptrVar  = &myvar2;cout << *ptrVar ; // will print out 10 (value in myvar2)myvar1myvar2mydblptrVar5105.3AddressOf myvar2
Pointersint myvar1= 5;int myvar2 = 10;double mydbl = 5.3;int* ptrVar ;ptrVar = &myvar2;*ptrVar = 20; cout << myvar2;  myvar1myvar2mydblptrVarmyvar25105.3AddressOf myvar220
Arrays in C++int myarray[5] = {10, 20, 30, 50, 5};	for (inti =0 ; i < 5; i++){cout << myarray[i] << " " ;	}cin.get();File:SimpleArrayTraversal
Pointer Arithmeticint 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
C/C++ can be tersewhile(*ptrString2++ = *ptrString1++);
PointersPointers are variables that hold a memory addressThe * 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 variableptr = &myvar get the memory address of myvar and stores it in ptr
Pointers and Pass by ReferenceC++ 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.
Why By ReferenceWant to avoid copying large amounts of data.Want the function to modify the value passed in.
Pointers as a SolutionMyfunc (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.
Objects in C++Dogage: Integerspeak()walk( location ) :string
Dog the .h fileclass dog{public:int age;	char* speak();moveTo(intx, inty);};C++
Dog the .cpp file#include "dog.h"#include <iostream>using namespace std;void Dog::speak(){cout << "Woof Woof!" << endl;}C++
Initializing a Dog C++#include <iostream>#include "dog.h"using namespace std;intmain(char* args[], intargc){cout << "Dog Program" << endl;	Dog fido;   //on stack    //intmyintfido.age = 5;fido.speak();cin.get();}
Scooby a Dynamic Dog C++Dog* scooby = new Dog();(*scooby).speak();  // these calls doscooby->speak();   //the same thingscooby->age = 6;
Objects and Pointersint myvar1= 5;int myvar2 = 1;double mydbl = 5.3;myvar1myvar2mydbl515.3
Pointersint myvar1= 5;int myvar2 = 1;double mydbl = 5.3;Dog* scooby = new Dog();scooby->age = 5; myvar1myvar2mydblscoobyage515.3Memoryaddress5
CallStackvs HeapCode example callStackExample
Directed Study/Further ReadingC++ Pointers http://www.cplusplus.com/doc/tutorial/pointers/Binky Videohttp://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/http://cslibrary.stanford.edu/104/

Cplusplus

  • 1.
  • 2.
    Hello World inC++#include <iostream>using namespace std;int main(){cout << "Hello World!" << endl;}C++
  • 3.
    Hello World inJavaimport java.io.*;public class Helloworld{ public static void main(Stringargs[]) {System.out.println("Hello World!"); }}Java
  • 4.
    Much is thesame…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.
    ...javaJavapublic class week1{publicstatic 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:\Users\darren>cd %HOMEPATH%C:\Users\darren>notepad helloworld.cppC:\Users\darren>cl /EHschelloworld.cppMicrosoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86Copyright (C) Microsoft Corporation. All rights reserved.helloworld.cppMicrosoft (R) Incremental Linker Version 8.00.50727.762Copyright (C) Microsoft Corporation. All rights reserved./out:helloworld.exehelloworld.objC:\Users\darren>helloworldHello, World!C:\Users\darren>
  • 7.
    Input and Outputin C++Input and Output handled by a libraryI/O part of the C++ Standard LibraryStream Based I/OStream Based Abstraction provides a common interface to I/O for harddisks, terminals, keyboards and the network
  • 8.
    The coutObjectInstance oftype ostreamDefined in Standard LibraryBound to Console (screen)Character Output StreamUses Operator overloading of << (left bit shift) to take arguments to printSimilar to cout << "Hello World!”cout.writeline(“Hello World”)
  • 9.
    cout Examplescout <<“Hello, World!”;cout << 5;cout << 5.1;intmyvar = 5 cout << myvar;string course = “SE5301”;cout << course;cout << course << “ is a “ << 5 << “star course”;
  • 10.
    endl End LinePuttingendl onto a stream moves to the next line.cout << “This is on line 1” << endl;cout << “This is on line 2” << endl;
  • 11.
    cinPart of StandardLibarayInstance of istreamBound to the keyboardThe >> operator is overridden to provide input methodchar mychar;cin >> mychar;
  • 12.
    cin examples char mychar;cin>> mychar;intmyint;cin >> myint; string mystr;cin >> mystr;
  • 13.
    I/O Exampleint 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;
  • 14.
  • 15.
    Pointersint myvar1= 5;intmyvar2 = 10;double mydbl = 5.3;myvar1myvar2mydbl515.3
  • 16.
    Pointersint myvar1= 5;intmyvar2 = 10;double mydbl = 5.3;int* ptrVar;ptrVar = &myvar2;cout << *ptrVar ; // will print out 10 (value in myvar2)myvar1myvar2mydblptrVar5105.3AddressOf myvar2
  • 17.
    Pointersint myvar1= 5;intmyvar2 = 10;double mydbl = 5.3;int* ptrVar ;ptrVar = &myvar2;*ptrVar = 20; cout << myvar2; myvar1myvar2mydblptrVarmyvar25105.3AddressOf myvar220
  • 18.
    Arrays in C++intmyarray[5] = {10, 20, 30, 50, 5}; for (inti =0 ; i < 5; i++){cout << myarray[i] << " " ; }cin.get();File:SimpleArrayTraversal
  • 19.
    Pointer Arithmeticint 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 betersewhile(*ptrString2++ = *ptrString1++);
  • 21.
    PointersPointers are variablesthat hold a memory addressThe * 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 variableptr = &myvar get the memory address of myvar and stores it in ptr
  • 22.
    Pointers and Passby ReferenceC++ 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 ReferenceWantto avoid copying large amounts of data.Want the function to modify the value passed in.
  • 24.
    Pointers as aSolutionMyfunc (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++Dogage:Integerspeak()walk( location ) :string
  • 26.
    Dog the .hfileclass dog{public:int age; char* speak();moveTo(intx, inty);};C++
  • 27.
    Dog the .cppfile#include "dog.h"#include <iostream>using namespace std;void Dog::speak(){cout << "Woof Woof!" << endl;}C++
  • 28.
    Initializing a DogC++#include <iostream>#include "dog.h"using namespace std;intmain(char* args[], intargc){cout << "Dog Program" << endl; Dog fido; //on stack //intmyintfido.age = 5;fido.speak();cin.get();}
  • 29.
    Scooby a DynamicDog C++Dog* scooby = new Dog();(*scooby).speak(); // these calls doscooby->speak(); //the same thingscooby->age = 6;
  • 30.
    Objects and Pointersintmyvar1= 5;int myvar2 = 1;double mydbl = 5.3;myvar1myvar2mydbl515.3
  • 31.
    Pointersint myvar1= 5;intmyvar2 = 1;double mydbl = 5.3;Dog* scooby = new Dog();scooby->age = 5; myvar1myvar2mydblscoobyage515.3Memoryaddress5
  • 32.
  • 33.
    Directed Study/Further ReadingC++Pointers http://www.cplusplus.com/doc/tutorial/pointers/Binky Videohttp://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/http://cslibrary.stanford.edu/104/