Object Oriented Programming C++ Lecture No 6

Outline

  • Arrays of objects
  • Memory management: the new and delete operators
  • Defining dynamic objects
  • Pointer to object
  • Reference to object
  • Array of pointers to objects

Defining Arrays of Objects

We may define arrays of objects. In the example below we see an array with ten elements of type Point.
void main()
{
Point array[10]; // defining an array with ten objects
array[0].move(15,40); // ‘move’ message to the first element //(index 0)
array[1].move(75,35); // ‘move’ message to the
// second element (index 1)
//… so on // message to other elements
for(int i= 0;i<10;i++) // ‘print’ message to all objects
// in the array
array[i].print();
}

Arrays of Objects

Initializing Arrays of Objects

  • When an array of objects is created, the default constructor of the class is invoked for each element (object) of the array one time.
  • Point array[10];// Default constructor is called 10 times
  • To invoke a constructor with arguments, a list of initial values should be used. To invoke a constructor with more than one arguments, its name must be given in the list of initial values.
    // Constructor
  • Point::Point(int x_first, int y_first = 0);
  • Point array[]= { {10} , {20} , Point(30,40) };
    // An array with 3 elements (objects)
  • If the class have also a default constructor the programmer may define an array of objects as follows:
  • Point array[5]= { {10} , {20} , Point(30,40) };
    // An array with 5 elements
  • Here the list of initial values contains 3 values, which are sent as arguments to the constructors of the first three elements.
  • For the last two elements, the default constructor is called.

Memory Management

  • We have seen examples where arrays are used to set aside memory.
  • Example
    Int arr[10];
  • Drawback with array is:
    We must know array size at coding time.
  • Following code won’t work
    cin>>size;
    int arr[size];
  • Compiler requires array size to be a compile time constant

The new Operator

  • Another approach to obtaining blocks of memory is using new operator.
  • This versatile operator obtains memory from the operating system and returns a pointer to its starting point.
  • new returns a pointer to an unnamed object.
  • #include<iostream>
    #include<cstring>
    using namespace std;
    int main(){
    char* str=”Demonstrating new and delete operators”;
    int len = strlen(str);
    char* ptr;
    ptr = new char[len+1];//set aside memory: string + ‘\0’
    strcpy(ptr, str);
    cout << ptr << endl;
    delete[] ptr;//release ptr’s memory
    return 0;
    }

The delete Operator

  •  When new is used to allocate memory, delete must be used to de-allocate the memory.
  • Deleting the memory doesn’t delete the pointer to it and doesn’t change the address value in the pointer.
  • However, this address is not valid anymore.
  • Don’t use pointers to memory that has been deleted.

Defining Dynamic Objects

  • Classes can be used to define variables like built-in data types (int, float, char etc.)
  • It is also possible to define pointers to objects.
  • In the next example, two pointers (pp1 and pp2) to objects of type Point are defined.

Pointers to Objects

  • Objects are stored in memory, so pointers can point to objects just as they can to variables of basic types.
  • The new Operator
  • The new operator allocates memory of a specific size from the operating system and returns a pointer to its starting point.
  • If it is unable to find space, it returns a 0 pointer.
  • When you use new with objects, it not only allocates memory for the object, it also creates the object in the sense of invoking the object’s constructor.
  • This guarantees that the object is correctly initialized, which is vital for avoiding programming errors.

The delete Operator

  • The delete Operator
    – To ensure safe and efficient use of memory, the new operator is matched by a corresponding delete operator that releases the memory back to the operating system.
    – If you create an array with new, you need the brackets when you delete it.int * ptr = new int[10];
    :
    delete [ ] ptr;
  • Don’t forget the brackets when deleting arrays of objects.
  • Using them ensures that all the members of the array are deleted and that the destructor is called for each one.
  • If you forget the brackets, only the first element of the array will be delete

Reference to Object

  • A reference is simply an alias (another name) for a variable.
  • A parameter may be specified as a reference type.
  • During the function call, a copy of the argument variable is not made, but the variable itself is used instead.
  • To specify a reference type, an ampersand (&) is added after the type name.

Reference to Object

  • Passing an argument by reference can be used to allow a function to modify a variable in the calling program.
  • The main motivation behind pass by reference is efficiency: the entire variable is not copied.

Output
29
0
Press any key to continue

An Array of Pointers to Objects

  • This arrangement allows easy access to group of objects, and is more flexible than placing the objects themselves in an array.

  •  

    A group of objects can be stored by storing an array of pointers to them, rather than storing the objects themselves.

Leave a Reply

Your email address will not be published. Required fields are marked *

16 − 12 =