• Log in or sign up for an account here: https://www.sythe.org/login

Program using Class structure

  • Thread starter Thread starter SVXX
  • Start date Start date
S

SVXX

Guest
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.
 
Program using Class structure
Can you please explain what Classes are?

Thanks,

Sid
 
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?
 
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++.
 
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?

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!
 
Runescape Gamblingrunescape gambling
Back
Top