Thread: How do you call member functions

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    How do you call member functions

    Hi, I am still reasonably new at this and i've come up against yet another problem. So far i haven't had trouble creating classes and objects and using member functions of specific objects in main, but on this program i made, i need a member function of an object to be called from another function outside main.
    This is a simple program that asks you to define stock of your three shops at the start and then you can sell stuff from any shop.

    Code:
    #include <iostream>
    int choicea();
    int choiceb();
    int choicec();
    
    class shop
    {
    private:
    	int books;
    	int pencils;
    
    public:
    	int init()
    	{
    		std::cout<<"How many books?: ";
    		std::cin>>books;
    		std::cout<<"And how many pencils?: ";
    		std::cin>>pencils;
    	}
    
    	int sellb()
    	{
    		int d;
    		std::cout<<"How many books would you like to sell?: ";
    		std::cin>>d;
    		books=-d;
    	}
    
    	int sellp()
    	{
    		int f;
    		std::cout<<"How many pencils did you sell?: ";
    		std::cin>>f;
    		pencils=-f;
    	}
    
    	int display()
    	{
    		std::cout<<"There are "<<books<<" books and " <<pencils <<" pencils\n";
    	}
    
    };
    
    
    int main()
    {
    	char in;
    	shop shop1;
    	shop shop2;
        shop shop3;
    	std::cout<<"Initialize your stock for your 3 shops\n";
    	shop1.init();
    	shop2.init();
    	shop3.init();
    	std::cout<<"What would you like to do?\nA.Sell books\nB.Sell Pencils\nC.Quit\n";
    	std::cin>>in;
    	switch(in)
    	{
    	case 1:
    		choicea();
    		break;
    	case 2:
    			choiceb();
    			break;
    	case 3:
    				choicec();
    				break;
    				default:
    					std::cout<<"Not valid, Please choose again: ";
    	}
    	return 0;
    }
        
    	   
     int choicea()
    {
    	char a;
    std::cout<<"What shop are you selling from? A.Shop 1\nB.Shop 2\nC.Shop 3: \n";
    std::cin>>a;
    switch(a)
    {
    case 1:
    		shop1.sellb() ;
    		break;
    case 2:
    			shop2.sellb();
    			break;
    case 3:
    				shop3.sellb();
    				break;
    default:
    					std::cout<<"Not valid, Please choose again: ";
    					return 0;
    }
    }
     int choiceb()
    {
    	char a;
    std::cout<<"What shop are you selling from? A.Shop 1\nB.Shop 2\nC.Shop 3: \n";
    std::cin>>a;
    switch(w)
    {
    case 1:
    		shop1.sellp() ;
    		break;
    case 2:
    			shop2.sellp();
    			break;
    case 3:
    				shop3.sellp();
    				break;
    default:
    					std::cout<<"Not valid, Please choose again: ";
    					return 0;
    }
    }
    
    int choicec()
    {
    	return 0; //figure out how to quit!!!!
    }
    I thought member functions were global but it seems their only loacal to where the object is created,does that make sense?
    I know there's probably way better ways of doing this but its just practice to get the hang of objects.
    Could someone please tell me how to call a member function of an object from a function.
    Thanks

    P.S These were some of the 10 error msgs

    error C2065: 'shop1' : undeclared identifier
    error C2228: left of '.sellb' must have class/struct/union type

    I'm guessing i'm going to have to learn what unions are

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Makes perfect sense, and I'll explain in a moment.

    First, instead of having an init() function in your classes, I would suggest having a constructor (method with the same name as the class) which does the same thing. That way the user would be prompted for that info when the instance of the class was created.

    Here's how a class works:
    You define the various variables and methods which are clumped together into a class (of type shop in your example). Then, when you want an instance of the class, you declare it like shop shop1; - exactly what you did. shop1 exists in main, but instances of class shop can be declared anywhere. To use shop1 outside of main, pass a pointer to it, and then reference the methods/variables with -> instead of . inside the function (sellb, for example)

    I'm not sure how clear that was - if you have a basic understanding of structs/classes, it should make sense, otherwise, it might be a bit unclear. Post a question about anything that didn't make sense.
    Away.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Thanks Blackrat, its starting to make sense now, i did a quick google on constructors/deconstructors and i still need to learn about them then it should be clear. Is it because when i create a shop object in main it only has a lifespan in main and then dissapears when you leave main to go to another function, is that why i get the
    'shop1' : undeclared identifier
    left of '.sellb' must have class/struct/union type
    errors, because they dont exist in that function?
    So a constructor will live until it is deconstucted?
    I'll be back, got some reading to do
    Thanks again

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Hmmm.....something just occured to me, when i get this program running, how is the switch code going to know if i press a,b or c?
    Its got case 1; case 2; ect, shouldn't it be case a; case b; or doesn't it work like that?

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    books=-d;
    should be
    Code:
    books -= d;
    me think..

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    This should clarify what I was saying before a bit. It's the same case with classes as in this example:

    Code:
    void function();
    void correct(int *pointer);
    
    int main()
    {
         int a = 5;
         function();
         correct(&a)
    }
    
    void function()
    {
         cout<<a; //illegal
    }
    
    void correct(int *pointer)
    {
         cout<<*pointer;  //outputs 5
    }
    Hope the example helped...
    Away.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Elite
    Hmmm.....something just occured to me, when i get this program running, how is the switch code going to know if i press a,b or c?
    Its got case 1; case 2; ect, shouldn't it be case a; case b; or doesn't it work like that?
    try
    Code:
    switch(a)
    {
      case 'a':
        shop1.sellb() ;
        break;
      case 'b':
      //rest of code...
    }

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    I C, I think i'm getting it now, i'll have to write a few programs with them to get comfotable
    Thanks for you help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  3. member functions can't see private data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2006, 02:36 PM
  4. DLL class member functions
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:59 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM