Thread: help needed with passing objects by reference

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    51

    help needed with passing objects by reference

    Im currently working hard on learning c++ and I would appreciate
    any help with this problem.

    I want this program to check if the rectangles area equals 100 and then take an action based on that check. I want to do the check in a separate function and have the function return 1 if it equals 100 and 0 if it doesn't. The "rectangle" class works fine with all other programs so that is not the problem.

    Code:
    #include <iostream>
    #include <rectangle.h>
    
    using namespace std;
    
    int checkarea(rectangle *recta);
    
    int main()
    {
    rectangle rect;
    cout<<"The area is: "<<rect.GetArea()<<endl;
    	if(checkarea(&rect)!=0);
    	{
    	return 0;
    	}
    cout<<"End of main()."<<endl;
    return 0;
    }
    
    int checkarea (rectangle  *recta)
    {
    	if (recta->GetArea()==100)        //I didn' really know
    	{                                                //if I should have used	cout<<"recta->GetArea() is 100."<<endl;   //the dot
    	return 1;                                    //operator instead of ->
    	}                                               //here.
    cout<<"End of checkarea()."<<endl;
    return 0;
    }
    By the way, this program isn't any serious one. I just wrote it to illustrate my problem.
    Last edited by finnepower; 06-30-2002 at 10:33 AM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Just a couple of things i spotted

    Code:
    if(checkarea(&rect) != 0);
    {
      return 0;
    }
    Change this to

    Code:
    if((checkarea(&rect)) != 0)
    {
      return 0;
    }
    And

    Code:
    if (recta->GetArea()==100)
    to

    Code:
    if ((recta->GetArea()) == 100)
    Apart from that it looks ok to me.
    PuterPaul.co.uk - Portfolio site

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    If you want to use references:

    Code:
    bool checkarea (rectangle & recta)
    {
        if (recta.GetArea()==100)        
        {                                                
              cout << "recta.GetArea() is 100." << endl;   
              return true;                                   
        }                                              
        cout << "End of checkarea()." << endl;
        return false;
    }
    then use if ( checkarea (rect) == true ), etc.

  4. #4
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Code:
    if(checkarea(&rect) != 0);
    {
      return 0;
    }
    Change this to

    Code:
    if((checkarea(&rect)) != 0)
    {
      return 0;
    }
    !!!!
    What is the problem with :
    if(checkarea(&rect) != 0)

    this is just different stylistic approach
    there is not any sintax error in this!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Passing by reference to functions
    By avi2886 in forum C Programming
    Replies: 12
    Last Post: 03-03-2009, 01:56 PM
  3. Passing if/ofstream objects by reference
    By evilkillerfiggi in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2005, 11:13 AM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. Passing the variable as a Reference
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 02:35 PM