33
Software Development Introduction to C++ Dr. Darren Dancey

Cplusplus

  • Upload
    dancey

  • View
    2.214

  • Download
    2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Cplusplus

Soft

ware

Develo

pm

en

t

Introduction to C++

Dr. Darren Dancey

Page 2: Cplusplus

Soft

ware

Develo

pm

en

tHello World in C++

#include <iostream>

using namespace std;

int main(){

cout << "Hello World!" << endl;

}

C++

Page 3: Cplusplus

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

Page 4: Cplusplus

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();

 }

Page 5: Cplusplus

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

Page 6: Cplusplus

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>

Page 7: Cplusplus

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

Page 8: Cplusplus

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”)

Page 9: Cplusplus

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”;

Page 10: Cplusplus

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;

Page 11: Cplusplus

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;

Page 12: Cplusplus

Soft

ware

Develo

pm

en

tcin examples

char mychar;cin >> mychar;

int myint;cin >> myint;

string mystr;cin >> mystr;

Page 13: Cplusplus

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;

Page 14: Cplusplus

Soft

ware

Develo

pm

en

tPointers

Page 15: Cplusplus

Soft

ware

Develo

pm

en

tPointers

int myvar1= 5;int myvar2 = 10;double mydbl = 5.3;

5

myvar1

1

myvar2

5.3

mydbl

Page 16: Cplusplus

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

Page 17: Cplusplus

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

Page 18: Cplusplus

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

Page 19: Cplusplus

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

Page 20: Cplusplus

Soft

ware

Develo

pm

en

tC/C++ can be terse

while(*ptrString2++ = *ptrString1++);

Page 21: Cplusplus

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

Page 22: Cplusplus

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.

Page 23: Cplusplus

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.

Page 24: Cplusplus

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.

Page 25: Cplusplus

Soft

ware

Develo

pm

en

tObjects in C++

Dog

age: Integer

speak()walk( location ) :string

Page 26: Cplusplus

Soft

ware

Develo

pm

en

tDog the .h file

class dog{public:

int age;char* speak();

moveTo(int x, int y);};

C++

Page 27: Cplusplus

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++

Page 28: Cplusplus

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++

Page 29: Cplusplus

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++

Page 30: Cplusplus

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

Page 31: Cplusplus

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

Page 32: Cplusplus

Soft

ware

Develo

pm

en

tCallStack vs Heap

Code example callStackExample

Page 33: Cplusplus

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/