Adblock breaks this site

Program using Class structure

Discussion in 'Programming General' started by SVXX, May 1, 2007.

  1. SVXX

    SVXX Guest

    Referrals:
    0
    Program using Class structure

    Well here's a program using class structure..

    Code:
                               
     /*PROCESSING 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);
          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;i<count;i++)
        {
            cout<<"\n"<<itemCode[i];
            cout<<"   "<<itemPrice[i];
        }
        cout<<"\n";
    }
    
    int main()
    {
         ITEMS order;
         order.CNT();
         int x;
         
         do
         {
              cout<<"\nYou can do the following;"
                  <<" Enter appropriate number \n";
              cout<<"\n1 : Add an item ";
              cout<<"\n2 : Display total value";
              cout<<"\n3 : Delete an item";
              cout<<"\n4 : Display all items";
              cout<<"\n5 : Quit";
              cout<<"\n\nWhat is 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<<"Error in input; try again\n";
                
              }
         } while(x != 5);
         
           return 0;
    }
                   
              
                  
            
    Understand anything? Hope so :p. For those who dont,send me a PM and I'll explain everything.
     
  2. SidStudios

    SidStudios Active Member

    Joined:
    Mar 14, 2007
    Posts:
    201
    Referrals:
    0
    Sythe Gold:
    0
    Program using Class structure

    Can you please explain what Classes are?

    Thanks,

    Sid
     
  3. SVXX

    SVXX Guest

    Referrals:
    0
    Program using Class structure

    Well...you could always use Wikipedia lol. But nevermind.

    Classes are a type of user defined data structure. There are two such types of data structures,struct and class. I used class in my program.

    Classes can have following members,which have their own functions(as is the case in my program). Then the functions can be followed up in int main().
    Classes display a property of inheritance too. Using the "friend" function,you can link stuff between two or more classes.
    Anything more to explain?
     
  4. SidStudios

    SidStudios Active Member

    Joined:
    Mar 14, 2007
    Posts:
    201
    Referrals:
    0
    Sythe Gold:
    0
    Program using Class structure

    Thanks for the explanation. Do you use classes to store data in an application?

    I'm not too good with C++.
     
  5. thiefmn6092

    thiefmn6092 Guest

    Referrals:
    0
    Program using Class structure

    When a *function* is in a class, it's referred to as a method. A structure is just a collection of named types under a name where as a class is the same except with a provided group of methods. You can also define constants with a preprocessor directive
    Code:
     #define <name> <value> 
    Great program though, keep working at it!
     
< BEst WEbbrowser Ever if it didnt work Before At your computer now it will!!! | C# html source grabber >


 
 
Adblock breaks this site