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];
public:
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.


Example:

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.

#include<iostream.h>
#include<conio.h>
class book
{
char auth[20];
char title[50];
public:
void accept( );
void display( );
};
void book::accept( )
{
cout<<"\n\n\tEnter Title of the book";
cin>>title;
cout<<"\n\n\tEnter Author Name";
cin>>auth;
}
void book::display( )
{
cout<<"\n\n\tBook Name--->"<<title;
cout<<"\n\n\tAuthor is--->"<<auth;
}
void main( )
{
book b[5];
int i;
clrscr( );
for(i=0;i<5;i++)
{
 b[i].accept( );
}
for(i=0;i<5;i++)
{
 b[i].display( );
}
getch( );
}


Output:-
   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

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (59) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (938) Python Coding Challenge (373) Python Quiz (31) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

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