Array of Objects
Array of Objects
};
int main ()
{
data *d; //declaration of object pointer
d=new data; //dynamic object
d->display();
cout<<d;
delete d; //deleting dynamic object
cout<<d;
return 0;
}
Array of Objects:
suppose we have 50 students in a class and we have to input the name
and marks of all the 50 students. Then creating 50 different objects and
then inputting the name and marks of all those 50 students is not a good
option. In that case, we will create an array of objects as we do in case of
other data-types.
The array of type class contains the objects of the class as its individual elements. Thus,
an array of a class type is also known as an array of objects. An array of objects is
declared in the same way as an array of any built-in data type.
The syntax for declaring an array of objects is
Example 1:
#include <iostream>
using namespace std;
class books
{
public:
char title [30];
float price ;
void getdata ();
void putdata ();
};
void books :: getdata ()
{
cout<<"Title:";
cin>>title;
cout<<"Price:";
cin>>price;
}
int main()
{
Simple obj;
Simple* ptr; // Pointer of class type
ptr = &obj;
For accessing normal data members we use the dot . operator with object and -> qith
pointer to object. But when we have a pointer to data member, we have to dereference
that pointer to get what its pointing to, hence it becomes,
Object.*pointerToMember
Example:
class Data
{
public:
int a;
void print()
{
cout << "a is "<< a;
}
};
int main()
{
Data d, *dp;
dp = &d; // pointer to object
d.*ptr=10;
d.print();
dp->*ptr=20;
dp->print();
}
Syntax:
Example
class Data
{
public:
int f(float)
{
return 1;
}
};
int main()
{
fp2 = &Data::f; // Assignment inside main()
}
Pointer to Member Functions of Class
Pointers can be used to point to class's Member functions.
Example:
#include <iostream>
using namespace std;
class demo
{
int y;
public:
int a,b;
int func(int z)
{
a=z;
return z;
}
};
int main ()
{
demo obj;
int demo::*ptr; // pointer to data member
ptr=&demo::a; //address to data member a is assigned to pointer
access through object
obj.*ptr=10;
cout<<"a in object after o1.*ptr=10 is:"<<obj.*ptr<<endl;
demo *ptrobj; //pointer to object of the class demo
ptrobj=&obj; //access though object pointer
ptrobj->*ptr=10;
cout<<"a in obj after ptrobj->*ptr is "<<ptrobj->*ptr<<endl;
int(demo::*ptr_func) (int);
ptr_func=&demo::func; //pointer to member function func()
Example:-
#include <iostream>
using namespace std;
class X {
public:
int a;
void f(int b)
{
cout << "The value of b is "<< b << endl;
}
};
int main() {
This pointer:
To understand ‘this’ pointer, it is important to know that how objects look at
functions and data members of a class.
1. Each object gets its own copy of the data member.
2. All access the same function definition as present in the code segment.
Meaning each object gets its own copy of data members and all objects
share single copy of member functions.
Then now question is that if only one copy of each member function exists
and is used by multiple objects, how are the proper data members are
accessed and updated?
Compiler supplies an implicit pointer along with the functions names as
‘this’.
The ‘this’ pointer is passed as a hidden argument to all nonstatic member
function calls and is available as a local variable within the body of all
nonstatic functions. ‘this’ pointer is a constant pointer that holds the
memory address of the current object. ‘this’ pointer is not available in static
member functions as static member functions can be called without any
object (with class name).
Following are the situations where ‘this’ pointer is
used:
1) When local variable’s name is same as member’s
name
#include<iostream>
using namespace std;
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
2) To return reference to the calling object
Another example of using this pointer is to return the reference of current object so that
you can chain function calls, this way you can call all the functions for the current object
in one go. Another important point to note in this program is that I have incremented the
value of object’s num in the second function and you can see in the output that it
actually increment the value that we have set in the first function call. This shows that
the chaining is sequential and the changes made to the object’s data members retains
for further chaining calls.
/* Reference to the calling object can be returned */
Test& Test::func ()
{
// Some processing
return *this;
}
When a reference to a local object is returned, the returned reference can
be used to chain function calls on a single object.
Example:
#include<iostream>
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test(int x = 0, int y = 0)
{
this->x = x;
this->y = y;
}
Test &setX(int a)
{
x = a;
return *this;
}
Test &setY(int b)
{
y = b;
return *this;
}
void print()
{
cout << "x = " << x << " y = " << y << endl;
}
};
int main()
{
Test obj1(5, 5);
obj1.print();
return 0;
}
Function set_new_handler():
Sets new_p as the new-handler function.
The new-handler function may try to make more storage available for a
new attempt to allocate the storage. If -and only if- the function succeeds in
making more storage available, it may return. Otherwise it shall either throw
a bad_alloc exception (or a derived class) or terminate the program (such
as by calling abort or exit).
Before this function is called by the program for the first time, or if new_p is
a null-pointer, the default allocation functions directly throw bad_alloc on
failure.
Parameters
new_p
Function that takes no arguments and returns no value (void).
The function can make more storage available, or throw an exception, or
terminate the program.
If this is a null-pointer, the new-handler function is reset to none (and
bad_alloc is thrown instead).
new_handler is a function pointer type for functions that take no arguments
and return no value.
Return value
The value of the current new-handler function if this has already been set
by this function previously, or a null-pointer if this is the first call to
set_new_handler (or if it was reset by a previous call).
// new_handler example
#include <iostream> // std::cout
#include <cstdlib> // std::exit
#include <new> // std::set_new_handler
void no_memory () {
std::cout << "Failed to allocate memory!\n";
std::exit (1);
}
int main () {
std::set_new_handler(no_memory);
std::cout << "Attempting to allocate 1 GiB...";
char* p = new char [1024*1024*1024];
std::cout << "Ok\n";
delete[] p;
return 0;
}
o/p:-