Array of objects, C/C++ Programming

Array of Objects:

The objects can be declared just like a structure or even a primary data type.  Array of objects may be required to work with large set of data.   When the data is very few objects without array can be used. The declaration of array of object is a follows.

int x; int x[10];  The integer x can store only one data at a time where as array integer x

can store 10 data at time because the size is defined as 10.  This is equivalent to declaring 10 individual integers.

 

class employee

{           char name [30]; float age; public:

void getdata(void);

void putdata(void);

};

 

The class employee has two data members and two functions members.  Earlier an object was produced using as shown

employee manager,foreman,worker;

The three objects are created with different names.  The class employee can created as array of objects as shown below.

employee managers[3]; employee foremen[15]; employee workers[75];

 

 

There are 93 objects of class employee.   There grouped into three manager, foreman, worker.  This is literally means the object store information of 3 managers, 15 foremen, and 75 workers.   Thus array of object will permit group the functionality.   The array element can be accessed using dot operator, just accessing member in the structure.

manager[i].getdata( );

 

The given program will give better understanding of array of objects class employee

{char name[25]; float age; public:

void getdata(void)

{cout<<"Enter Name: "<<"\n";

cin>>name;

cout<<"Enter Age: "<<"\n";

cin>>age;

}

void putdata(void)

{cout<<"Name is "<

cout<<"Age is "<

}

};

 

int main()

{clrscr();

int i;

employee staff[25];

for (i=0;i<25;i++)

{staff[i].getdata();

}

for (i=0;i<25;i++)

{staff[i].putdata();

}

 

cout<<"\n";

getch();

return 0;

}

 

 

Posted Date: 9/20/2012 7:57:35 AM | Location : United States







Related Discussions:- Array of objects, Assignment Help, Ask Question on Array of objects, Get Answer, Expert's Help, Array of objects Discussions

Write discussion on Array of objects
Your posts are moderated
Related Questions
ABC Car Dealership needs your help to update the ordering system. This car dealer is selling four types of vehicles: Sedan, Truck, SUV, and mini Van. And each type of vehicle can h

Write a C# program to compute the Net Pay for employees. The program is to have a Main method and two user-defined methods. The Main method is to input the employee's name (string)

Insert values in queue - C++ program: Write a program to insert values in queue. int main()   {         int k;     Queue timeLine;     cout     for(

Write a ‘C’ program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number.

1. write a program named InputMethodDemo2 that eliminates the repetitive code in the InputMethod() in the InputMethodDemo program. Rewrite the program so the InputMethod()contains

Implement (a part of) electronic election Scenario where following political parties are participating in election: PPP, PML and PTI.  Create a class PollingStation where it stor

Primarily the term alignment refers the tendency of an address pointer value to be a multiple of some power of two. Thus a pointer with two byte alignment contains a zero in the le

Write a function to determine whether a number is prime: it will return true if the input is prime and false otherwise. Use it to see whether -7 is prime.

Calculate the salary if the person work 8 hours/day and the salary/hour is RM5.20 - UML class diagaram -Class -Exception handling/assertion

The purpose of this assignment is to familiarize you with the notion of encapsulation as well as some aspects of IP and ICMP. Write your programs in C or C++. They will be grade