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

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

Followers

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