css 332 programming issues with object-oriented languages lecture 3. 141010

24
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE 3. 141010.

Upload: chester-fisher

Post on 28-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

CSS 332PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES

LECTURE 3. 141010.

Today’s Agenda• Collect topics / questions• Homework questions.• Present / code snippets

• Arrays, Pointers, Dynamic memory• Constructor Invocation Timing• FileIO• Recursion• Assignment and Copy Constructor• New :: returning an array• More Pointers

• In-class assignment:

Homework questions.

Reverse an array (pointers) Write a function which reverses an array which only uses pointers, not the arr[] syntax

void ReverseArray(int *array, int length){for (int i = 0; i < length / 2; i++){int temp = *(array + i);*(array + i) = *(array + length - 1 - i);*(array + length - 1 - i) = temp;}}

void PrintArray(int *array, int length){int *ind = array;for (int i = 0; i < length; i++){cout << "array[" << i << "] = " << *ind << endl;ind++;}}

Dynamic Memory Array Update your array program to take in input and dynamically allocate an array of this size

cout << "Input Size of Array: ";cin >> ArraySize;

dynArr = new int[ArraySize];for (int i = 0; i < ArraySize; i++){dynArr[i] = i;}

PrintArray(dynArr, ArraySize);ReverseArray(dynArr, ArraySize);cout << endl << "Reversed: " << endl;PrintArray(dynArr, ArraySize);ReverseArray(dynArr, ArraySize);cout << endl << "Reversed again: " << endl;PrintArray2(dynArr, ArraySize);

delete(dynArr);

Time of Invocation • Automatic Local

• Each time block is executed• Static Local

• Once –first time it is hit• Global

• In order of declaration in translation unit• Typically before main() is entered• Destroyed in reverse order of construction

• Dynamic (tbd)• malloc/free• new/delete

Invocation example

Create a class Employee: name, number.

Create a class Cubicle: width, length, Employee

Optional: Office an array of Cubicles

In constructors put a cout<< describing what is being created.

Computer Scientist of the week

Sir Timothy Berners-Lee

• HTTP Protocol• Unlike Al Gore, he can claim to have invented the world wide

web• Director of WWW Consortium• Knighted in 2004

File IO

In C: In C++:In Java:

FILE *fp; ifstream inFile(“name.dat”); import java.io.*;

orFileInputStream infile =

ifstream inFile;new FileInputStream( “name.dat” );

if ((fp = fopen( “name.dat”, “r” )) inFile.open(“name.dat”); ObjectInputStream input = != NULL { if ( inFile ) { // true of false new ObjectInputStream( infile );

fscanf( fp, “%d”, &i ); inFile >> i; try { i = input.readInt( );

fclose(fp); inFile.close( );} catch ( EOFException e ) {

} }input.close( );

}

Note: for output, use ofstream.

File IO example

Read in form the console the name of a file.

The file contains strings (names) which is your team’s roster.

Read in your roster; close the file.

User inputs to console a name to look for on the team. Confirm or deny.

cout << "Input file" << endl;cin >> fName;inFile.open(fName);while (getline(inFile, name)){roster.push_back(name);}inFile.close();

cout << "Input player you are looking for: ";cin >> name;

bool onTeam = false;for (int i = 0; i < roster.size(); i++){if (roster[i] == name){onTeam = true;break;}}

if (onTeam){cout << name << " is on the team." << endl;}else{cout << name << " is not on the team." << endl;}

Recursive Solutions• A recursive solution calls itself

• Each recursive call solves an identical, smaller problem

• Test for base case enables recursive calls to stop

• Eventually one of smaller calls will be base case

DATA STRUCTURES AND PROBLEM SOLVING WITH C++: WALLS AND MIRRORS, CARRANO AND HENRY, © 2013

Recursion example 1 Write a recursive function which sums up all numbers from 1 to n.

Write the same function iteratively.

What is the difference in execution?

Babies

Recursion 2: Multiplying Mice

Month 1

Month 2

Month 4

Month 5

Month 3

Month 6

Month 7

Recursive relation:#babies in Month 7= #mice in Month 5#adults in Month 7 = #mice in Month 6

Fibonacci Sequence mice(n) = mice(n-1) + mice(n-2)

Base cases: mice(1) = 1; mice(2) = 1;

Recursive: mice(n) = mice(n-1) + mice(n-2)

Today’s In-class Assignment.Continue last weeks

• Implement a team roster class• The members of a team (Players) have a first name, last name, and number

• Use an Array as a data structure• Read in players from a file Tony Blair 14

Spiro Agnew 7

Clint Dempsey 6

…..• overload << so that the roster is printed out in alphabetical order

break

Pointers1. Pointer variables int *p, *q;

2. Int allocation int x;

3. Address-of operator p = &x;

4. Memory cell to which P points *p = 6;

5. Pointer operations q = p;

? ? ?

p q x

? ?

p q x

? 6

p q x

6

p q x

1 3 4

5

Pointer C++ Examples• We’ll use a pointer .cpp file

• Compile with command line• Bring into VS environment

• Play with pointers

• Why does this work: char* InitialInput = "Adam";

Double v. float

Char, Char*, fstream, array

• Write a program which reads a string names from a file into an array.

int defencesiveAttack() { int * pointer; int info[1]; pointer = &info[0]; // working out how much user will hit srand((unsigned int)time(0)); info[0] = (strenght+exp)*rand()%5+1; // working out the experience pionts for the hit info[1] = (info[0]/2)-exp; return pointer; }

I am just starting off in c++ and I have function which I want to return two pieces of information from. So I make an array in the function and return it, but you cannot do that in c++ right. So instead I use a pointer to refer to the array in memory and then increment it to get the values right. I have googled this topic and still cannot get it to work. Here is my function, well method, its inside a class.

http://www.cplusplus.com/forum/beginner/6644/