Arrays can be used as member variables in a class
//C++ program to process Shopping List
#include <iostream>
using namespace std;
const int m=50;
class ITEMS
{
int itemCode[m];
float itemPrice[m];
int count;
public:
void CNT(void){count=0;}
void getItem(void);
void displaySum();
void remove(void);
void displayItems(void);
};
void ITEMS::getItem(void)
{
cout<<“Enter item code:”;
cin>>itemCode[count];
cout<<“Enter item Cost:”;
cin>>itemPrice[count];
count++;
}
void ITEMS::displaySum(void)
{
float sum=0;
for(int i=0;i<count;i++)
sum=sum+itemPrice[i];
cout<<“\nTotal value:”<<sum<<“\n”;
}
void ITEMS::remove(void)
{
int a;
cout<<“Enter item code:”;
cin>>a;
for(int i=0;i<count;i++)
if(itemCode[i]==a)
itemPrice[i]=0;
}
void ITEMS::displayItems(void)
{
cout<<“\nCode Price\n”;
for(int i=0;i<count;i++)
{
cout<<“\n”<<itemCode[i];
cout<<” “<<itemPrice[i];
}
cout<<“\n”;
}
main()
{
ITEMS order;
order.CNT();
int x;
do
{
cout<<“\nMENU”;
cout<< “\n 1: Add an Item”;
cout<< “\n 2: Display Total value”;
cout<< “\n 3: Delete an item”;
cout<< “\n 4: Display all items”;
cout<<“\n 5: Exit”;
cout<< “\n Enter your option:”;
cin>>x;
switch(x)
{
case 1: order.getItem();break;
case 2: order.displaySum(); break;
case 3: order.remove(); break;
case 4: order.displayItems(); break;
case 5: break;
default: cout<<“Enter an valid option, try again!!!\n”;
}
}while(x!=5);
cout<<“\nThank You!!!”;
}
Array of Objects
//C++ program demonstrates Array of Objects
#include<iostream>
using namespace std;
class employee
{
char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
void employee::getdata(void)
{
cout<< “Enter Name:”;
cin>>name;
cout<< “Enter Age:”;
cin>>age;
}
void employee::putdata(void)
{
cout<< “Name:”<<name<< “\n”;
cout<< “Age:”<<age<< “\n”;
}
const int size=3;
main()
{
employee manager[size]; //array of managers
for(int i=0;i<size; i++)
{
cout<< “\nEnter details of Manager”<<i+1<< “\n”;
manager[i].getdata();
}
cout<< “\n”;
for(int i=0;i<size; i++)
{
cout<< “\nList of Managers” << “\n”;
cout<< “\n Manager”<<i+1<< “\n”;
manager[i].putdata();
}
}