Upload
dancey
View
2.214
Download
2
Embed Size (px)
DESCRIPTION
Citation preview
Soft
ware
Develo
pm
en
t
Introduction to C++
Dr. Darren Dancey
Soft
ware
Develo
pm
en
tHello World in C++
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!" << endl;
}
C++
Soft
ware
Develo
pm
en
tHello World in Java
import java.io.*;
public class Helloworld{ public static void main(String args[]) { System.out.println("Hello World!"); }
}
Java
Soft
ware
Develo
pm
en
tMuch is the same…
C++
#include <iostream> using namespace std; int main(char* args[], int argc){
for(int i = 2; i < 100; i++){bool flag = true;for (int j = 2; j <= i/2; j++ ){
if (i%j == 0){flag = false;break;
}}if (flag == true){
cout << i << endl;}
}cin.get();
}
Soft
ware
Develo
pm
en
t...java
public class week1{
public static void main(String args[]){
for(int i = 2; i < 100; i++){boolean flag = true;for (int j = 2; j <= i/2; j++ ){
if (i%j == 0){flag = false;break;
}}if (flag == true){System.out.println(i);}
}
}
}
Java
Soft
ware
Develo
pm
en
tCompiling a C++ Program
C:\Users\darren>cd %HOMEPATH%
C:\Users\darren>notepad helloworld.cpp
C:\Users\darren>cl /EHsc helloworld.cppMicrosoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86
Copyright (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.obj
C:\Users\darren>helloworldHello, World!
C:\Users\darren>
Soft
ware
Develo
pm
en
tInput 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
Soft
ware
Develo
pm
en
tThe cout Object
• 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”)
Soft
ware
Develo
pm
en
tcout Examples
cout << “Hello, World!”;cout << 5;cout << 5.1;int myvar = 5 cout << myvar;string course = “SE5301”;cout << course;cout << course << “ is a “ << 5 << “star course”;
Soft
ware
Develo
pm
en
tendl 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;
Soft
ware
Develo
pm
en
tcin
• Part of Standard Libaray• Instance of istream• Bound to the keyboard• The >> operator is overridden to provide input
method
char mychar;cin >> mychar;
Soft
ware
Develo
pm
en
tcin examples
char mychar;cin >> mychar;
int myint;cin >> myint;
string mystr;cin >> mystr;
Soft
ware
Develo
pm
en
tI/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;
Soft
ware
Develo
pm
en
tPointers
Soft
ware
Develo
pm
en
tPointers
int myvar1= 5;int myvar2 = 10;double mydbl = 5.3;
5
myvar1
1
myvar2
5.3
mydbl
Soft
ware
Develo
pm
en
tPointers
int myvar1= 5;int myvar2 = 10;double mydbl = 5.3;
int* ptrVar;ptrVar = &myvar2;
cout << *ptrVar ; // will print out 10 (value in myvar2)
5
myvar1
10
myvar2
5.3
mydbl
AddressOf
myvar2
ptrVar
Soft
ware
Develo
pm
en
tPointers
int myvar1= 5;int myvar2 = 10;double mydbl = 5.3;
int* ptrVar ;ptrVar = &myvar2;
*ptrVar = 20;
cout << myvar2;
5
myvar1
10
myvar2
5.3
mydbl
AddressOf
myvar2
ptrVar
20
myvar2
Soft
ware
Develo
pm
en
tArrays in C++
int myarray[5] = {10, 20, 30, 50, 5};
for (int i =0 ; i < 5; i++){cout << myarray[i] << " " ;
}
cin.get();
File:SimpleArrayTraversal
Soft
ware
Develo
pm
en
tPointer 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 (int i = 0; i < 5; i++){
cout << *(ptr+i) << endl;}
cout << "Array Traversal with moving ptr" << endl;for (int i = 0; i < 5; i++){
cout << *ptr++ << endl;}
File:pointer Arithmetic
Soft
ware
Develo
pm
en
tC/C++ can be terse
while(*ptrString2++ = *ptrString1++);
Soft
ware
Develo
pm
en
tPointers
• 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
Soft
ware
Develo
pm
en
tPointers 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 (int foo, int bar){…}Myfunc(a,b);
The values in a and b are copied into foo and bar.
Soft
ware
Develo
pm
en
tWhy By Reference
• Want to avoid copying large amounts of data.• Want the function to modify the value passed
in.
Soft
ware
Develo
pm
en
tPointers 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.
Soft
ware
Develo
pm
en
tObjects in C++
Dog
age: Integer
speak()walk( location ) :string
Soft
ware
Develo
pm
en
tDog the .h file
class dog{public:
int age;char* speak();
moveTo(int x, int y);};
C++
Soft
ware
Develo
pm
en
tDog the .cpp file
#include "dog.h"#include <iostream>
using namespace std;
void Dog::speak(){
cout << "Woof Woof!" << endl;}
C++
Soft
ware
Develo
pm
en
tInitializing a Dog
#include <iostream>#include "dog.h"
using namespace std;
int main(char* args[], int argc){
cout << "Dog Program" << endl;Dog fido; //on stack
//int myint
fido.age = 5;fido.speak();
cin.get();}
C++
Soft
ware
Develo
pm
en
tScooby a Dynamic Dog
Dog* scooby = new Dog();
(*scooby).speak(); // these calls doscooby->speak(); //the same thingscooby->age = 6;
C++
Soft
ware
Develo
pm
en
tObjects and Pointers
int myvar1= 5;int myvar2 = 1;double mydbl = 5.3;
5
myvar1
1
myvar2
5.3
mydbl
Soft
ware
Develo
pm
en
tPointers
int myvar1= 5;int myvar2 = 1;double mydbl = 5.3;
Dog* scooby = new Dog();scooby->age = 5;
5
myvar1
1
myvar2
5.3
mydbl
Memoryaddress
scooby
5age
Soft
ware
Develo
pm
en
tCallStack vs Heap
Code example callStackExample
Soft
ware
Develo
pm
en
tDirected Study/Further Reading
• C++ Pointers – http://www.cplusplus.com/doc/tutorial/pointers/
• Binky Video– http://www.docm.mmu.ac.uk/STAFF/D.Dancey/2008/11/25/fun-with-pointers/
– http://cslibrary.stanford.edu/104/