Oop Lecture 8
Oop Lecture 8
• Example:
int *ptr; //Pointer that can point to an integer
ptr = new int; //Now it points to allocated memory
delete ptr;
int *p;
p = new int[10];//allocating dynamic array
delete [] p;
Example
int main() {
int *ptr; //Pointer that can point to an integer
ptr = new int(87);//Allocate memory and initialize to 87
if(!ptr) //NULL pointer returned
{
cout << "Allocation error\n";
}
else
{
cout <<"Memory location: "<< ptr;
cout <<" contains the int value: "<< *ptr <<endl;
delete ptr; //deallocate the memory
}
return 0;
}
Example
int main() {
int *ptr; // Pointer that can point to an integer
int size = 5; // Size of the array
• Syntax:
ClassName ObjectName[number of objects];
• Example:
Employee e[50];
Array of Object
Output
Array of Object
• Other way for array of object with similar output
Array of Object
Output
Dynamically Allocate Array of Object
Output
Dynamically Allocate Array of Object
Output