class

This is a discussion on class within the C++ Programming forums, part of the General Programming Boards category; in a class i dont really know too much im stuck on the part about constuctor and destucton and how ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    class

    in a class i dont really know too much
    im stuck on the part about constuctor and destucton and how to call a function from in a class
    or example i have a code i started and i want to make a class the declaers variebles than has a funtion in it.
    i have the funtion asking for some data than i want to have another funtion in it that uses the data.. and i got really lost.
    i have posted what i have done so far but i think its really off and it isnt done so pls dont post about that being something wrong..
    Code:
    #include <iostream>
    
    using namesspace std;
    
    class database
    {
          public:
                 int employee;
                 float salery;
                 int position;
                 int x;
                 void position (int rank)
                 {
                      switch (rank)
                      {
                             case 1:
                                  cout<<"cashier";
                                  break;
                             case 2:
                                  cout<<"Bag boy";
                                  break;
                             case 3:
                                  cout<<"delly";
                                  break;
                             case 4:
                                  cout<<"janeter";
                                  break;
                             case 5:
                                  cout<<"banker";
                                  break;
                             case 6:
                                  cout<<"produce";
                                  break;
                             case 7:
                                  cout<<"vice-maneger";
                                  break;
                             case 8:
                                  cout<<"maneger";
                                  break;
                             case 10:
                                  cout<<"1) cashier \n2) bag boy \n3) delly \n4) janeter \n5) banker \n";
                                  cout<<"6) produce \n7) vice-maneger \n8) maneger \n";
                                  cin>> rank;
                                  position (rank);
                                  break;
                             default:
                                     cout<<"not in database";
                                     break;
                      }
                 }
                 void quarry ()
                 {
                      cout<<"Employee id number?"<<endl;
                      cin>> id_number;
                      cout<<"The salery of employee :"<< id_number<<endl;
                      cin>> salery;
                      cout<<"The current status of employee?     10 for listings"<<endl;
                      cin>> rank;
                 }
                 void respond ()
                 {
                      cout<<"employee id number is :"<<idnumber<<endl;
                      cout<<"employee salery :"<<salery<<" per hour"<<endl;
                      cout<<"your employee's position is :";
                      position (rank);
                      cout<<endl;
                 }
    };
    
    int main()
    {

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Well your code doesnt have a constructor or deconstructor (though the compiler will give you a basic one in the background), but if you want one you define it like so:

    Code:
    class database
    {
    public:
    database();  //constructor
    ~database(); // deconstructor
    }
    The constructor is called whenever you make an OBJECT of the class, and you make an object of the class if you plan on using that class for something. You can give the constructor arguements (but not the deconstructor), so when you create an object of the class you can give it arguments that basicly define initial values that you want for every database object. You can also do things inside of the constructor or deconstructor, for example delete pointers (allocated memory) ro avoid memory leaks.

    Theres 2 problems I see with your code. The first is your argument in the function 'position', when you called it you put 'position(rank)' .. well what is rank? its nothing, the 'int rank' in the function cant go out of that scope, so you would need to make a new variable whereever you are calling that function and define it to the case you want, or put the case in directly.

    The second is a class problem for defining your functions. If you want functions for your class you give the prototype inside the class, and then define the action function outside of it. ie. void database:osition (int rank) { }, however you could define them inside the class if they were inline, but I dont think that would work with the code you want to have here, or would it be efficient.

    Code:
    #include <iostream>
    
    using namespace std; //you had 2 s's in 'namespace'
    
    class database
    {
          private: // you might as well define these as private variables
                 int employee; //you can combine these variables, ie. int employee, position, x, rank;
    //removed int position, and x, it was not necessary
                 int id_number; //added here because you have defined down there as if in class scope
                 int rank; //added this because you are using this as if its scope is everywhere in the class.
                 float salery;
    
         public:
                int position (); //prototype, changed from void to int to return the positon #
                void quarry (); //prototype
                void respond (); //prototype
    };
    
    int database::position ()
                 {
                      switch (rank)
                      {
                             case 1:
                                  cout<<"cashier";
                                  break;
                             case 2:
                                  cout<<"Bag boy";
                                  break;
                             case 3:
                                  cout<<"delly";
                                  break;
                             case 4:
                                  cout<<"janeter";
                                  break;
                             case 5:
                                  cout<<"banker";
                                  break;
                             case 6:
                                  cout<<"produce";
                                  break;
                             case 7:
                                  cout<<"vice-maneger";
                                  break;
                             case 8:
                                  cout<<"maneger";
                                  break;
                             case 10:
                                  cout<<"1) cashier \n2) bag boy \n3) delly \n4) janeter \n5) banker \n";
                                  cout<<"6) produce \n7) vice-maneger \n8) maneger \n";
                                  cin>> rank;
                                  break;
                                  //position (rank); commenting this out, put a while(1) loop on the menu instead of a goto type
                             default:
                                  cout<<"not in database";
                                  break;
                      }
                 return rank;
                 }
    
    void database::quarry ()
                 {
                      cout<<"Employee id number? "<<endl;
                      cin>> id_number;
                      cout<<"The salery of employee: "<< id_number<<endl;
                      cin>> salery;
                      cout<<"The current status of employee?     10 for listings "<<endl;
                      cin>> rank;
                      position (); //added this so it has something to do after taking input
                 }
    
    void database::respond ()
                 {
                      cout<<"employee id number is: "<<id_number<<endl; //you mispelled id_number
                      cout<<"employee salery: "<<salery<<" per hour"<<endl;
                      cout<<"your employee's position is: " <<position ()<<endl; // changed this part, you can put it all together
                 }
                 
    int main ()
    {
          database editDatabase;  //declare an object of 'database' class
    
          editDatabase.quarry(); //call the function
          editDatabase.respond();
          
          cin.ignore(); //pause the system to view output
          cin.get();
        
    }
    Alright that will compile, and I tried not to many many changes so I could relate to your method of coding. That means there are some "problems" with your, and that coding, because its not really efficient, or does it make sense in some areas (the return position #, salary amount, etc.). In my own version I would change a lot, but I didnt want to go an add a bunch of strings, while statements, remove and add functions or else it wouldnt be the image you had in mind.. this is just a compiling version.

    btw you have some spelling mistakes of course, salery, maneger, janeter, etc.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21