Thread: objects

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    if I have a class with the public variables int variable1 and bool variable2, how would I return that

    like this? return {int something,bool something};
    You don't return 'classes'. You return 'objects' of the 'class type'. The 'thing' listed prior to the function name is the 'return type' of the function. If you are going to return a variable of type int or an int 'literal'(e.g. 1, 100, 30), you put int in front of the function name. If you are going to return an object of your class, you put the class name in front of the function name. Then, inside the function, you return a variable of your class type:
    Code:
    class Apple
    {
    public:
    	int num;
    	double size;
    	
    	Apple(int n, double d)
    	{
    		num = n;
    		size = d;
    	}
    
    };
    
    Apple func(void)
    {
    	Apple my_apple(10, 3.5);
    	return my_apple;
    }
    
    int main()
    {
    	
    	Apple a = func();
    	cout<<a.num<<endl;
    	cout<<a.size<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 02-27-2005 at 09:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 05:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM