1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will...
-
Upload
paulina-clark -
Category
Documents
-
view
219 -
download
0
description
Transcript of 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will...
![Page 1: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/1.jpg)
1
Final ExamFinal Exam20 Questions20 Questions
Multiple choice and some minimal programming will be required
![Page 2: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/2.jpg)
2
Topics CoveredTopics Covered
• Implementing Arrays and Structs• List Structures (Arrays & Linked List)• C++ Classes• Object-Oriented Programming• Pointers• Dynamic Memory• Recursion
![Page 3: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/3.jpg)
3
The Basic Array StructureThe Basic Array Structure
Declare an array called temps which will hold up to 5 individual float values.
float temps[5]; // declaration allocates memory
temps[0] temps[1] temps[2] temps[3] temps[4]
7000 7004 7008 7012 7016
number of elements in the array
indexes or subscripts
Base Address
![Page 4: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/4.jpg)
4
Declaration of an ArrayDeclaration of an Array
The index is also called the subscript
In C++, the first array element always has subscript 0. The second array element has subscript 1, etc.
The base address of an array is its beginning address in memory
DataType ArrayName [ConstIntExpression];
![Page 5: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/5.jpg)
5
Assigning Values to Assigning Values to Individual Array ElementsIndividual Array Elements
float temps[ 5 ] ; // allocates memory for arrayint m = 4 ;temps[ 2 ] = 98.6 ;temps[ 3 ] = 101.2 ;temps[ 0 ] = 99.4 ;temps[ m ] = temps[ 3 ] / 2.0 ;temps[ 1 ] = temps[ 3 ] - 1.2 ; // what value is assigned?
temps[0] temps[1] temps[2] temps[3] temps[4]
7000 7004 7008 7012 7016
99.4 ? 98.6 101.2 50.6
![Page 6: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/6.jpg)
6
What values are assigned?What values are assigned?
float temps[ 5 ] ;int m ;
for (m = 0; m < 5; m++){ temps[ m ] = 100.0 + m 0.2 ;}
temps[0] temps[1] temps[2] temps[3] temps[4]
7000 7004 7008 7012 7016
? ? ? ? ?
![Page 7: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/7.jpg)
7
Now what values are printed?Now what values are printed?
float temps[ 5 ] ;int m ; . . . . .for (m = 4; m >= 0; m-- ){ cout << temps[ m ] << endl ;}
temps[0] temps[1] temps[2] temps[3] temps[4]
7000 7004 7008 7012 7016
100.0 100.2 100.4 100.6 100.8
![Page 8: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/8.jpg)
8
Variable SubscriptsVariable Subscripts
float temps[ 5 ] ;int m = 3 ; . . . . . .
What is temps[ m + 1] ?What is temps[ m ] + 1 ?
temps[0] temps[1] temps[2] temps[3] temps[4]
7000 7004 7008 7012 7016
100.0 100.2 100.4 100.6 100.8
![Page 9: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/9.jpg)
9
Initializing in a DeclarationInitializing in a Declaration
int ages[ 5 ] = { 40, 13, 20, 19, 36 } ;
for ( int m = 0; m < 5; m++ ){ cout << ages[ m ] ;}
ages[0] ages[1] ages[2] ages[3] ages[4]
6000 6002 6004 6006 6008
40 13 20 19 36
![Page 10: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/10.jpg)
10
For the Final Exam
Note: You might be asked to write a simple array structure.
![Page 11: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/11.jpg)
11
Array-Based List
![Page 12: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/12.jpg)
12
Linked List ADT
![Page 13: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/13.jpg)
13
Array List vs. Linked List ADT
Array List• Pre-allocation Required• Waste-full if you allocate too much• Fatal if you allocate too little• Data has to be shifted when an item is
inserted or deleted at specified positionLinked List
• Truly dynamic, memory is allocated as needed
• No shifting when an item is inserted, deleted
![Page 14: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/14.jpg)
14
For the Final Exam
Note: You will NOT be asked to write a Linked List
Questions related to Link List are concept based, (no code writing).
![Page 15: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/15.jpg)
15
Structured Data Type Structured Data Type
A structured data type is a type in which each value is a collection of component items.
The entire collection has a single name each component can be accessed individually
![Page 16: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/16.jpg)
16
thisAnimal
.id 2037581
.name “giant panda”
.genus “Ailuropoda”
.species “melanoluka”
.country “China”
.age 18
.weight 234.6
.health Good
![Page 17: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/17.jpg)
17
struct AnimalType
enum HealthType { Poor, Fair, Good, Excellent } ;
struct AnimalType // declares a struct data type{ // does not allocate memory
long id ;string name ;string genus ;string species ; struct members string country ; int age ; float weight ; HealthType health ;
} ;
AnimalType thisAnimal ; // declare variables of AnimalType
AnimalType anotherAnimal ; 17
![Page 18: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/18.jpg)
18
Accessing struct Members
Dot ( period ) is the member selection operator.
After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot.
EXAMPLESthisAnimal.weightanotherAnimal.country
![Page 19: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/19.jpg)
19
For the Final ExamFor the Final Exam
Note: You might be ask to write a simple struct.
![Page 20: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/20.jpg)
20
Abstraction Abstraction
Abstraction is the separation of the essential qualities of an object from the details of how it works or is composed
It focuses on what, not how
It is necessary for managing large, complex software projects
![Page 21: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/21.jpg)
21
Abstract Data Type (ADT)Abstract Data Type (ADT)
Is a programmer-defined type with a set of values and allowable operations for the type.
Some ways to define a new C++ type are:using typedefusing structusing class
![Page 22: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/22.jpg)
22
ADT Specification ExampleADT Specification Example
TYPETimeType
DOMAINEach TimeType value is a time in hours, minutes, and seconds.
OPERATIONSSet the timePrint the timeIncrement by one secondCompare 2 times for equalityDetermine if one time is “less than” another
![Page 23: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/23.jpg)
23
ADT Implementation means ADT Implementation means
Choosing a specific data representation for the abstract data using data types that already exist (built-in or programmer-defined)
Writing functions for each allowable operation
![Page 24: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/24.jpg)
24
Information HidingInformation Hiding
Class implementation details are hidden from the client’s view. This is called information hiding.
Public functions of a class provide the interface between the client code and the class objects.
clientcode specification implementation
abstraction barrier
![Page 25: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/25.jpg)
25
Benefits of information hidingBenefits of information hiding
Data and details can be concealed from the client of the abstraction.
Code can be changed without affecting the client because the specification and interface are unchanged.
![Page 26: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/26.jpg)
26
class TimeTypeclass TimeType Specification Specification
// Specification File ( timetype.h )class TimeType // declares a class data type{ // does not allocate memorypublic : // 5 public function members
void Set ( int hours , int mins , int secs ) ;void Increment ( ) ;void Write ( ) const ;bool Equal ( TimeType otherTime ) const ; bool LessThan ( TimeType otherTime ) const ;
private : // 3 private data membersint hrs ; int mins ; int secs ;
} ;
![Page 27: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/27.jpg)
27
Use of C++ data TypeUse of C++ data Type class class
Facilitates re-use of C++ code for an ADT
Software that uses the class is called a client
Variables of the class type are called class objects or class instances
Client code uses public member functions to handle its class objects
![Page 28: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/28.jpg)
28
Using Using classclass
A class is a programmer-defined type whose components (called class members) can be variables or functions.
Class members are private by default. Compiler does not permit client code to access private class members.
Class members declared public form the interface between the client and the class.
In most classes, the private members contain data, and the public members are functions to manipulate that data.
![Page 29: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/29.jpg)
29
Member functions categorized by taskMember functions categorized by task
CONSTRUCTOR -- a member function that actually creates a new instance and initialized some or all of its data members
ACCESS FUNCTION or OBSERVER -- a member function that can inspect (use but not modify) the data members of a class without changing their values. Such a function is declared with const following the parameter list in both the specification and the implementation files.
![Page 30: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/30.jpg)
30
Client Code UsingClient Code Using TimeTypeTimeType
#include “timetype.h” // includes specification of the classusing namespace std ;
int main ( ){
TimeType currentTime ; // declares 2 objects of TimeType TimeType endTime ; bool done = false ;
currentTime.Set ( 5, 30, 0 ) ; endTime.Set ( 18, 30, 0 ) ; while ( ! done )
{ . . .
currentTime.Increment ( ) ;if ( currentTime.Equal ( endTime ) )
done = true ; } ;}
30
![Page 31: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/31.jpg)
31
classclass type Declaration type Declaration
The class declaration creates a data type and names the members of the class.
It does not allocate memory for any variables of that type!
Client code still needs to declare class variables.
![Page 32: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/32.jpg)
32
C++ Data Type C++ Data Type classclass represents an ADT represents an ADT
2 kinds of class members: data members and function members
Class members are private by default
Data members are generally private
Function members are generally declared public
Private class members can be accessed only by the class member functions (and friend functions), not by client code.
![Page 33: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/33.jpg)
33
AggregateAggregate class class Operations Operations
Built-in operations valid on class objects are:
Member selection using dot ( . ) operator ,
Assignment to another class variable using ( = ),
Pass to a function as argument (by value or by reference),
Return as value of a function
Other operations can be defined as class member functions
![Page 34: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/34.jpg)
34
2 separate files Generally Used for2 separate files Generally Used for classclass Type Type
// Specification File ( timetype .h ) // Specifies the data and function members. class TimeType { public: . . .
private: . . . } ;
// Implementation File ( timetype.cpp ) // Implements the TimeType member functions. . . .
![Page 35: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/35.jpg)
35
Implementation File for Implementation File for TimeTypeTimeType
// Implementation File ( timetype.cpp ) // Implements the TimeType member functions.
#include “ timetype.h” // also must appear in client code #include <iostream>
. . .
bool TimeType :: Equal (TimeType otherTime ) const // Function value == true, if this time equals otherTime // == false , otherwise { return ( (hrs == otherTime.hrs) && (mins == otherTime.mins) && (secs == otherTime.secs)
) ; } . . .
![Page 36: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/36.jpg)
36
Member selection operator .Member selection operator .
When a client uses a public member function, the function call requires a dot preceded by the name of the object. You can think of the function call as being sent to this object.
ofstream outFile ;
outFile.open (“a:\\my.out”) ;...
outFile.close( ) ;
classtype
variable,object,instance
![Page 37: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/37.jpg)
37
Familiar Class Instances and Familiar Class Instances and Function Members Function Members
The member selection operator ( . ) selects either data members or function members
Header files iostream and fstream declare the istream, ostream,and ifstream, ofstream I/O classes
Both cin and cout are class objects and get and ignore are function members
cin.get (someChar) ;cin.ignore (100, ‘\n’) ;
The statements below, declare myInfile as an instance of class ifstream and invoke function member open
ifstream myInfile ;
myInfile.open ( “A:\\mydata.dat” ) ;
![Page 38: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/38.jpg)
38
Scope Resolution Operator ( :: ) Scope Resolution Operator ( :: )
C++ programs typically use several class types
Different classes can have member functions with the same identifier, like Write( )
Member selection operator is used to determine the class whose member function Write( ) is invoked currentTime .Write( ) ; // class TimeTypenumberZ .Write( ) ; // class ComplexNumberType
In the implementation file, the scope resolution operator is used in the heading before the function member’s name to specify its classvoid TimeType :: Write ( ) const{ . . .
}
![Page 39: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/39.jpg)
39
Class Constructors Class Constructors
A class constructor is a member function whose purpose is to initialize the private data members of a class object
The name of a constructor is always the name of the class, and there is no return type for the constructor
A class may have several constructors with different parameter lists. A constructor with no parameters is the default constructor
A constructor is implicitly invoked when a class object is declared--if there are parameters, their values are listed in parentheses in the declaration
![Page 40: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/40.jpg)
40
Specification of Specification of TimeType TimeType Class Class Constructors Constructors
class TimeType // timetype.h{public : // 7 function members
void Set ( int hours , int minutes , int seconds ) ;void Increment ( ) ;void Write ( ) const ;bool Equal ( TimeType otherTime ) const ; bool LessThan ( TimeType otherTime ) const ;
TimeType ( int initHrs , int initMins , int initSecs ) ; // constructor
TimeType ( ) ; // default constructorprivate : // 3 data members
int hrs ; int mins ; int secs ;
} ; 40
![Page 41: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/41.jpg)
41
Implementation of Implementation of TimeTypeTimeType Default Default Constructor Constructor
TimeType :: TimeType ( )// Default Constructor// Postcondition: // hrs == 0 && mins == 0 && secs == 0{
hrs = 0 ; mins = 0 ; secs = 0 ;}
![Page 42: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/42.jpg)
42
Implementation of Another Implementation of Another TimeTypeTimeType Class Constructor Class Constructor
TimeType :: TimeType ( int initHrs, int initMins, int initSecs )// Constructor// Precondition: 0 <= initHrs <= 23 && 0 <= initMins <= 59// 0 <= initSecs <= 59// Postcondition:// hrs == initHrs && mins == initMins && secs ==
initSecs{
hrs = initHrs ; mins = initMins ; secs = initSecs ;}
![Page 43: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/43.jpg)
43
Separate Compilation and Linking of FilesSeparate Compilation and Linking of Files
timetype.h client.cpp timetype.cpp
client.obj
client.exe
timetype.obj
Compiler Compiler
Linker
#include “timetype.h”
implementation file
specification file
main program
![Page 44: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/44.jpg)
44
For the Final ExamFor the Final Exam
Note: You might be asked to write a simple class, without using dynamic memory. (no Copy Constructor or Destructor required.
![Page 45: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/45.jpg)
45
PointersPointers
• A pointer holds the memory address of another object.• Through the pointer we can indirectly manipulate the
referenced object.
Pointers are useful for
• Creating linked data structures such as linked lists,• management of dynamically allocated objects, and• as a function parameter type for passing large objects
such as arrays.
![Page 46: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/46.jpg)
Pass-by-valuePass-by-value
CALLINGBLOCK FUNCTION
CALLED
sends a copy of the contents of the actual parameter
SO, the actual parameter cannot be changed by the function.
46
![Page 47: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/47.jpg)
Pass-by-referencePass-by-reference
sends the location (memory address)of the actual parameter
can change value ofactual parameter
CALLINGBLOCK FUNCTION
CALLED
47
![Page 48: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/48.jpg)
48
Obtaining Memory AddressesObtaining Memory Addresses
the address of a non-array variable can be obtained by using the address-of operator &
int x;float number;char ch;
cout << “Address of x is “ << &x << endl;
cout << “Address of number is “ << &number << endl;
cout << “Address of ch is “ << &ch << endl;
![Page 49: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/49.jpg)
49
What is a pointer variable?What is a pointer variable?
A pointer variable is a variable whose value is the address of a location in memory.
to declare a pointer variable, you must specify the type of value that the pointer will point to, for example,
int* ptr; // ptr will hold the address of an int
char* q; // q will hold the address of a char
![Page 50: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/50.jpg)
50
Using a Pointer VariableUsing a Pointer Variable
int x; x = 12;
int* ptr; ptr = &x;
NOTE: Because ptr holds the address of x, we say that ptr “points to” x
2000
12
x
3000
2000
ptr
![Page 51: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/51.jpg)
51
2000
12
x
3000
2000
ptr
int x; x = 12;
int* ptr; ptr = &x;
cout << *ptr;
NOTE: The value pointed to by ptr is denoted by *ptr
Unary operator Unary operator ** is the indirection is the indirection (deference) operator(deference) operator
![Page 52: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/52.jpg)
52
int x; x = 12;
int* ptr; ptr = &x;
*ptr = 5; // changes the value // at address ptr to 5
Using the Dereference OperatorUsing the Dereference Operator
2000
12 5
x
3000
2000
ptr
![Page 53: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/53.jpg)
53
char ch; ch = ‘A’;
char* q; q = &ch;
*q = ‘Z’; char* p; p = q; // now p and q both point to ch
Another ExampleAnother Example
4000
A Z
ch
5000 6000
4000 4000
q p
![Page 54: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/54.jpg)
Operator Operator newnew Syntax Syntax
new DataType
new DataType [IntExpression]
If memory is available, in an area called the heap (or free store) new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated.
Otherwise, program terminates with error message.
The dynamically allocated object exists until the delete operator destroys it. 54
![Page 55: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/55.jpg)
55
3 Kinds of Program Data3 Kinds of Program Data
STATIC DATA: memory allocation exists throughout execution of program
AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function
DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using operators new and delete
![Page 56: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/56.jpg)
56
Dynamically Allocated DataDynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
cout << *ptr;
NOTE: Dynamic data has no variable name
2000
ptr
‘B’
![Page 57: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/57.jpg)
57
Dynamically Allocated DataDynamically Allocated Data
char* ptr;
ptr = new char;
*ptr = ‘B’;
cout << *ptr;
delete ptr;
2000
ptr
NOTE: delete de-allocates the memory pointed to by ptr
?
![Page 58: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/58.jpg)
Operator delete returns to the free store memory which was previously allocated at run-time by operator new.
The object or array currently pointed to by the pointer is deallocated, and the pointer is considered unassigned.
Using Operator Using Operator deletedelete
58
![Page 59: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/59.jpg)
Operator Operator deletedelete Syntax Syntax
delete Pointer
delete [ ] Pointer
If the value of the pointer is 0 there is no effect.
Otherwise, the object or array currently pointed to by Pointer is deallocated, and the value of Pointer is undefined. The memory is returned to the free store.
Square brackets are used with delete to deallocate a dynamically allocated array. 59
![Page 60: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/60.jpg)
60
Dynamic Array Deallocation Dynamic Array Deallocation
char *ptr ;
ptr = new char[ 5 ]; strcpy( ptr, “Bye” );ptr[ 1 ] = ‘u’;
delete ptr; // deallocates array pointed to by ptr // ptr itself is not deallocated // the value of ptr is undefined.
ptr
?
![Page 61: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/61.jpg)
61
int* ptr = new int; *ptr = 3;
ptr = new int; // changes value of ptr *ptr = 4;
What happens here?What happens here?
3 ptr
3 ptr
4
![Page 62: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/62.jpg)
62
Inaccessible ObjectInaccessible Object
An inaccessible object is an unnamed object that was created by operator new and which a programmer has left without a pointer to it.
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5;
8 ptr
-5 ptr2
![Page 63: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/63.jpg)
63
Making an Object InaccessibleMaking an Object Inaccessible
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5;
ptr = ptr2; // here the 8 becomes inaccessible
8 ptr
-5 ptr2
8 ptr
-5 ptr2
![Page 64: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/64.jpg)
64
Memory LeakMemory Leak
A memory leak is the loss of available memory space that occurs when dynamic data is allocated but never deallocated.
![Page 65: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/65.jpg)
65
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2;
delete ptr2; // ptr is left dangling ptr2 = NULL;
Leaving a Dangling PointerA pointer that points to dynamic memory that has been de-allocated
8 ptr
-5 ptr2
8 ptr
NULL
ptr2
![Page 66: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/66.jpg)
66
Why is a destructor needed?Why is a destructor needed?
When a DynArray class variable goes out of scope, the memory space for data members size and pointer arr is deallocated.
But the dynamic array that arr points to is not automatically deallocated.
A class destructor is used to deallocate the dynamic memory pointed to by the data member.
![Page 67: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/67.jpg)
67
DynArray::~DynArray( ); // Destructor.// POST: Memory for dynamic array deallocated.
{delete [ ] arr ;
}
67
class DynArray Destructorclass DynArray Destructor
![Page 68: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/68.jpg)
68
Shallow CopyShallow Copy vs.vs. Deep CopyDeep Copy
a shallow copy copies only the class data members, and does not make a copy of any pointed-to data
a deep copy copies not only the class data members, but also makes a separate stored copy of any pointed-to data
![Page 69: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/69.jpg)
69
Initialization of Class ObjectsInitialization of Class Objects
C++ defines initialization to mean
•initialization in a variable declaration
•passing an object argument by value
•returning an object as the return value of a function
by default, C++ uses shallow copies for these initializations
![Page 70: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/70.jpg)
70
As a result . . .As a result . . .
when a class has a data member pointer to dynamically allocated data, you should write what is called a copy constructor
the copy constructor is implicitly called in initialization situations and makes a deep copy of the dynamic data in a different memory location
![Page 71: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/71.jpg)
71
Copy ConstructorsCopy Constructors
When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value
You do not call the copy constructor
Like other constructors, it has no return type
Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition
![Page 72: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/72.jpg)
72
Copy ConstructorCopy Constructor
Copy constructor is a special member function of a class that is implicitly called in these 3 situations:
• passing object parameters by value
• initializing an object variable in its declaration
• returning an object as the return value of a function
![Page 73: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/73.jpg)
73
CONSTRUCTOR
COPY CONSTRUCTOR
DESTRUCTOR
Classes with Data Member Pointers NeedClasses with Data Member Pointers Need
![Page 74: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/74.jpg)
74
For the Final ExamFor the Final Exam
Note: You will not have to write source code using pointers or dynamic memory.
Questions related to these topics will be multiple choice.
![Page 75: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/75.jpg)
75
Recursive Function CallRecursive Function Call
A recursive call is a function call in which the called function is the same as the one making the call
In other words, recursion occurs when a function calls itself!
But we need to avoid making an infinite sequence of function calls (infinite recursion)
![Page 76: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/76.jpg)
76
void message(){ cout << “This is a recursive function. \n”;message();}
Recursive Function Example
![Page 77: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/77.jpg)
77
The previous program will eventually crash, because each time a function is called, temporary information is stored on a stack. The stack memory will eventually overflow and cause an error).
A recursive function (as with a loop) must have some algorithm to control the number of times it repeats.
void message(int times){ if(times > 0) { cout << “This is a recursive function. \n”; message(times-1); } return;}
Recursive Function Example
![Page 78: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/78.jpg)
78
Finding a Recursive SolutionFinding a Recursive Solution
The idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved
This easily solved situation is called the base case
Each recursive algorithm must have at least one base case, as well as a general (recursive) case
![Page 79: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/79.jpg)
79
General format forGeneral format forMany Recursive FunctionsMany Recursive Functions
if (some easily-solved condition) // base case
solution statement
else // general case
recursive function call
![Page 80: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/80.jpg)
80
Example Recursive FunctionExample Recursive FunctionFinding the Sum of the Numbers from 1 to nFinding the Sum of the Numbers from 1 to n
int Summation ( int n ){ if ( n == 1) // base case
return 1 ;else // general case
return ( n + Summation ( n - 1 ) ) ;}
![Page 81: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/81.jpg)
81
““Why use recursion?”Why use recursion?”
The previous example could have been written without recursion, by using iteration instead. The iterative solution uses a loop, while the recursive solution uses an if statement.
However, for certain problems the recursive solution is the most natural solution. This often occurs when structured variables are used.
![Page 82: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/82.jpg)
82
For the Final ExamFor the Final Exam
Note: You will not have to write source code using recursion.
Questions related to this topics will be multiple choice.
![Page 83: 1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.](https://reader036.fdocuments.us/reader036/viewer/2022062413/5a4d1b697f8b9ab0599b2609/html5/thumbnails/83.jpg)
83
End of ReviewEnd of Review
20 Questions20 QuestionsMultiple choice and some minimal
programming will be required