Download presentation
Presentation is loading. Please wait.
Published byChester Fisher Modified over 9 years ago
1
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE 3. 141010.
2
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:
3
Homework questions.
4
Reverse an array (pointers) Write a function which reverses an array which only uses pointers, not the arr[] syntax
5
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++; }
6
Dynamic Memory Array Update your array program to take in input and dynamically allocate an array of this size
7
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);
8
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
9
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.
10
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
11
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 falsenew 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.
12
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.
13
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; }
14
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
15
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?
16
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
17
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)
18
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
19
break
20
Pointers 1.Pointer variablesint *p, *q; 2.Int allocationint x; 3.Address-of operatorp = &x; 4.Memory cell to which P points*p = 6; 5.Pointer operationsq = p; ??? pqx ?? pqx ?6 pqx 6 pqx 1 3 4 5
21
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";
22
Double v. float
23
Char, Char*, fstream, array Write a program which reads a string names from a file into an array.
24
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/
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.