Procedural Concept: - The Main Program Coordinates Calls To Procedures and Hands Over Appropriate Data As Parameters
Procedural Concept: - The Main Program Coordinates Calls To Procedures and Hands Over Appropriate Data As Parameters
Procedural Concept: - The Main Program Coordinates Calls To Procedures and Hands Over Appropriate Data As Parameters
The main program coordinates calls to procedures and hands over appropriate data as parameters. 1
Object-Oriented Concept
C++
Supports Data Abstraction Supports OOP
Encapsulation Inheritance Polymorphism
Generic Algorithms
sort(), copy(), search() any container Stack/Vector/List
3
structured
array struct union class
char short int long bool float double long double address pointer reference
5
Recall that . . .
char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant because the value of str itself cannot be changed by assignment. It points to the memory location of a char.
6000
H
str [0]
e
[1]
l
[2]
l
[3]
o
[4]
\0
[5] [6] [7]
6
Addresses in Memory
When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable int float char 2000
x
2006
ch
number
ch
cout << Address of x is << &x << endl; cout << Address of number is << &number << endl;
NOTE: Because ptr holds the address of x, we say that ptr points to x
10
*: dereference operator
int x; x = 12; int* ptr; ptr = &x; cout << *ptr;
3000
2000 ptr 2000 12 x
6000
4000
p
// the rhs has value 4000 // now p and q both point to 13ch
\0
Reference Variables
Reference variable = alias for another variable
- Contains the address of a variable (like a pointer) - No need to perform any dereferencing (unlike a pointer) - Must be initialized when it is declared
int x = 5; int &z = x; int &y ; cout << x << endl; cout << z << endl;
// z is another name for x //Error: reference must be initialized -> prints 5 -> prints 5 // same as x = 9;
-> prints 9 -> prints 9
15
z = 9;
cout << x << endl; cout << z << endl;
16
int main (void){ int v = 5, x = 10; cout << v << x << endl; p_swap(&v,&x); cout << v << x << endl; r_swap(v,x); cout << v << x << endl; return 0; }
void r_swap(int &a, int &b) { int temp; temp = a; (2) a = b; (3) b = temp; }
17
DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using operators new and delete
AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function
19
Stack
Low-end
20
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.
22
Operator new
2000
char*
ptr;
??? 5000
ptr
5000
cout
<<
*ptr;
24
Operator delete
2000
char*
ptr;
5000 ???
ptr
5000
cout
delete
<<
*ptr;
ptr;
Example
char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, Bye ); ptr 3000
ptr[ 0 ] = u;
6000
B y e \0 u // deallocates the array pointed to by ptr // ptr itself is not deallocated // the value of ptr becomes undefined
27