Sunday, March 17, 2013

Comparing Cost Using Friend Function

#include <iostream>
using namespace std;
class Order{
               int orderid;
               char productname[25];
               int rate;
               int quantity;
               int getcost()
               {
                   int cost=rate*quantity;
                   return cost;
               }
               public:
               Order()
               {
                   orderid=0;
                   strcpy(productname,"NIL");
                   rate=0;
                   quantity=0;
               }
               void getdata();
               friend Order comp(Order a[]);
               void compshow(Order);
               void display();
           };
void Order::getdata()
{
    cout<<"Enter Product ID: ";
    cin>>orderid;
    fflush(stdin);
    cout<<"Enter Product Name: ";
    cin.getline(productname,24);
    cout<<"Enter Rate: ";
    cin>>rate;
    cout<<"Enter Quantity: ";
    cin>>quantity;
}
Order comp(Order a[])
{
    int flag=0;
    int max=a[0].getcost();
    for(int i=0;i<3;++i)
    {
        if(max<a[i].getcost())
        {
            max=a[i].getcost();
            flag=i;
        }
    }
    return a[flag];
}
void Order::compshow(Order z)
{
    cout<<"I.D: "<<z.orderid;
    cout<<"\nProduct Name: "<<z.productname;
    cout<<"\nRate: "<<z.rate;
    cout<<"\nQuantity: "<<z.quantity;
    cout<<"\nCost: "<<z.getcost();
}
void Order::display()
{
    cout<<"I.D: "<<orderid;
    cout<<"\nProduct Name: "<<productname;
    cout<<"\nRate: "<<rate;
    cout<<"\nQuantity: "<<quantity;
    cout<<"\nCost: "<<getcost();
}
int main()
{
    Order a1,a[5];
    for(int i=0;i<3;++i)
    {
        a[i].getdata();
    }
    system("cls");
    for(int j=0;j<3;++j)
    {
        a[j].display();
        cout<<"\n\n";
    }
    cout<<"The Product with highest cost: \n\n";
    a1=comp(a);
    a1.compshow(a1);
    return 0;
}

0 comments:

Post a Comment