Saturday 31 March 2018

Array of Objects

Any object whether built in or user defined, can be stored in an array. When you declare the array, you tell the compiler the type of object to store and the number of objects for which to allocate room.

The compiler know how much room is needed for each object based on the class declaration.

Consider the following class definition :
class vehicle
int vcode;
char vname[30];
void getinfo( );
void disinfo( );

The identifier vehicle is a user defined data type and can be used to create objects that relate to a different categories of vehicles.


vehicle v1[3];     vehicle v2[4];

The array contains three objects namely , v1[0] , v1[1], v1[2] of type vehicle class. Similarly, v2 contains 4 objects namely v2[0], v2[1], v[2], v[3].

Accessing member data in an array of objects is a two step process. You identify the member of the array by using the index operator([]) and then you add the member operator(.) to access the particular member variable.

For Example:
v[i] disinfo( );

will display the data of the ith element of the array v1. That is, this statement request the objects v[i] to invoke the member function disonfo( ). An array of object is stored inside the memory in same way as a multi-dimensional array.

The array v1 represented in figure below

Here only the space for data items of object is created. Member function are stored separately and will be used by all the objects.

Write a program to declare a class book having data members as a title and author. Accept the data for five books and display the accepted data.

class book
char auth[20];
char title[50];
void accept( );
void display( );
void book::accept( )
cout<<"\n\n\tEnter Title of the book";
cout<<"\n\n\tEnter Author Name";
void book::display( )
cout<<"\n\n\tBook Name--->"<<title;
cout<<"\n\n\tAuthor is--->"<<auth;
void main( )
book b[5];
int i;
clrscr( );
 b[i].accept( );
 b[i].display( );
getch( );

   Enter Title of the book c_basic
   Enter Author Name ABC
   Enter Title of the book cpp
   Enter Author Name DEF
   Enter Title of the book java
   Enter Author Name PQR
   Enter Title of the book sql
   Enter Author Name XYZ
   Enter Title of the book php
   Enter Author Name MNO

Book Name--->c_basic
 Author is--->ABC
Book Name--->cpp
 Author is--->DEF
Book Name--->java
 Author is--->PQR
Book Name--->sql
 Author is--->XYZ
Book Name--->php
Author is--->MNO


Post a Comment

Popular Posts


AI (28) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (121) C (77) C# (12) C++ (82) Course (66) Coursera (184) Cybersecurity (24) data management (11) Data Science (99) Data Strucures (7) Deep Learning (11) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Pandas (3) PHP (20) Projects (29) Python (791) Python Coding Challenge (268) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (41) UX Research (1) web application (8)


Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses