Array of Objects in C--(Unit-1).Docx
Array of Objects in C--(Unit-1).Docx
array of objects is a collection of objects of the same class type that are stored in contiguous
memory locations. Since each item in the array is an instance of the class, each one's member
variables can have a unique value. This makes it possible to manage and handle numerous
objects by storing them in a single data structure and giving them similar properties and
behaviors.No memory or storage is allocated when a class is defined; only the object's
specification is defined. We must create objects in order to use the data and access the class's
defined functions.
array of objects as a single variable that can hold multiple values. Each value is stored in a
separate element of the array, and each element can be accessed by its index.
Arrays in C++ are typically defined using square brackets [] after the type. The index of the
array, which ranges from 0 to n - 1, can be used to access each element.
Syntax for declaring an array of objects in C++:
class className {
//variables and functions
};
className arrayName[arraySize];
where,
className is the name of the class that the objects belong to.
arrayName is the name of the array of objects.
arraySize is the number of objects in the array or the size of array, specified as a constant
expression.
Example
#include<iostream>
class MyClass {
private:
int data;
public:
void initialize(int i){
data = i;
cout<<"object "<<i+1<<endl;}
};
int main(){
MyClass array_of_objects[5];
array_of_objects[i].initialize(i+1);
array_of_objects[i].method(i);}
}
Output:
object 1
object 2
object 3
object 4
object 5
Advantages of Array of Objects:
1. The array of objects represent storing multiple objects in a single name.
2. In an array of objects, the data can be accessed randomly by using the index number.
3. Reduce the time and memory by storing the data in a single variable.
Disadvantages of Array of Objects:
1. The main disadvantage with arrays of objects is that the constructor runs for each object.
If the default constructor doesn’t do the right thing for multiple objects in the array, we
have to specify the constructor arguments for each element at the time the array is
declared, which we wouldn’t have to do if we allocated an array of object pointers, and
allocated objects as needed.
Pointer is a variable whose value is the address of another variable. Like any variable or
constant, you must declare a pointer before you can work with it. The general form of a pointer
variable declaration is −
type *var-name;
Following are the valid pointer declaration −
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
Pointers in C++
Pointers have many but easy concepts and they are very important to C++ programming. There
are following few important pointer concepts which should be clear to a C++ programmer −
Sr.N
Concept & Description
o
Null Pointers
1 C++ supports null pointer, which is a constant with a value of zero defined in several standard
libraries.
Pointer Arithmetic
2
There are four arithmetic operators that can be used on pointers: ++, --, +, -
Pointers vs Arrays
3
There is a close relationship between pointers and arrays.
4 Array of Pointers
You can define arrays to hold a number of pointers.
Pointer to Pointer
5
C++ allows you to have pointer on a pointer and so on.
The behavior of an object pointer is identical to that of a variable pointer. But in this case,
the object's address is kept instead of the variables. When a class object is formed in
the main function, a pointer variable is declared similarly to the variable itself. Using a data
type for the pointer is not recommended when generating a pointer to an object. Instead, we
must make use of the object pointer's class name. The -> symbol must be used to call a class
member function using a Pointer in the main function.
Syntax:
We declare a pointer to an object using the object's class name followed by an asterisk
(*) and the pointer name.
Objects are created using the new keyword, which dynamically allocates memory for the
object. After that, the Pointer is assigned the memory address of the newly created object.
ClassName *objPtr = new ClassName(); // Creating an object and assigning its address to the
pointer
We "dereference" the pointer using the asterisk (*) operator to access the object itself
(rather than its members).
▪ References vs Pointers
▪ You cannot have NULL references. You must always be able to assume that a
reference is connected to a legitimate piece of storage.
▪ Once a reference is initialized to an object, it cannot be changed to refer to
another object. Pointers can be pointed to another object at any time.
▪ A reference must be initialized when it is created. Pointers can be initialized at
any time.
▪ Creating References in C++
the & in these declarations as reference. Thus, read the first declaration as "r is an
integer reference initialized to i" and read the second declaration as "s is a double
reference initialized to d.". Following example makes use of references on int and
double
References are usually used for function argument lists and function return values. So following
are two important subjects related to C++ references which should be clear to a C++ programmer
−
References as Parameters
1
C++ supports passing references as function parameter more safely than parameters.
int main()
{
int x = 20;
Output
The address of the variable x is :- 0x7fff412f512c
2. The indirection operator/Dereference operator (*)
The indirection/ dereference operator is a unary operator that returns the value of the variable
present at the given address. It is completely opposite to the address-of operator. It is spelled as
a value pointed at the address.
Example:
● C++
int main()
{
cout
<< "The value stored at the variable price is Rs : "
<< (*price);
return 0;
}
Output
The value stored at the variable price is Rs : 3899
Scope Resolution Operator in C++
The scope resolution operator is used to reference the global variable or member function that is
out of scope. Therefore, we use the scope resolution operator to access the hidden variable or
function of a program. The operator is represented as the double colon (::) symbol.
For example, when the global and local variable or function has the same name in a program,
and when we call the variable, by default it only accesses the inner or local variable without
calling the global variable. In this way, it hides the global variable or function. To overcome this
situation, we use the scope resolution operator to fetch a program's hidden variable or function.
Uses of the scope resolution Operator
1. It is used to access the hidden variables or member functions of a program.
2. It defines the member function outside of the class using the scope resolution.
3. It is used to access the static variable and static function of a class.
4. The scope resolution operator is used to override function in the Inheritance.
Program to access the hidden value using the scope resolution (::) operator
Program1.cpp
1. #include <iostream>
2. using namespace std;
3. // declare global variable
4. int num = 50;
5. int main ()
6. {
7. // declare local variable
8. int num = 100;
9.
10. // print the value of the variables
11. cout << " The value of the local variable num: " << num;
12.
13. // use scope resolution operator (::) to access the global variable
14. cout << "\n The value of the global variable num: " << ::num;
15. return 0;
16. }
Output
The value of the local variable num: 100
The value of the global variable num: 50