Thread: Passing objects?!

  1. #1
    Eggsy84 :: Out of depth!
    Join Date
    Jun 2005
    Location
    Manchester, UK
    Posts
    5

    Passing objects?!

    Hi guys

    Few questions about how to pass objects around a C++ program.

    Please see the following code exerts.


    Code:
    /* Borrower.h
       James Heggs
    */
    
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class Borrower
    {
    private:
       int id;
       string forename;
       string surname;
    public:
       Borrower(int, string, string);
       string getName();
       void setForename(string);
       void toString();
    };




    and Borrower.cpp

    Code:
    /* Borrower.cpp
       James Heggs
    */
    
    #include "Borrower.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    Borrower::Borrower(int aID, string fName, string sName)
    {
       id = aID;
       forename = fName;
       surname = sName;
    }
    
    string Borrower::getName()
    {
       return forename + " " + surname;
    }
    
    void Borrower::toString()
    {
       cout << "ID: " << id << "\nName: " << getName() << "\n";
    }
    
    void Borrower::setForename(string fName)
    {
       forename = fName;
    }
    
    int main()
    {
       //Call Borrower
       Borrower b(12345, "James", "Heggs");
       b.toString();
       b.setForename("Peter");
       b.toString();
       
    }
    ==================
    The the Copy stuff

    Code:
    /*Copy.h
      James Heggs
    */
    
    #include <string>
    #include <iostream>
    using namespace std;
    
    class Copy 
    {
    private:
       static int copyID;
       string title;
    public:
       Copy(string);
       string getTitle();
       void toString();
    };

    and Copy.cpp

    Code:
    /* Copy.cpp
       James Heggs
    */
    
    #include <string>
    #include <iostream>
    #include "Copy.h"
    
    using namespace std;
    
    int Copy::copyID = 1001;
    
    Copy::Copy(string aTitle)
    {
       copyID = copyID;
       title = aTitle;
       copyID++;
    }
    
    string Copy::getTitle()
    {
       return title;
    }
    
    void Copy::toString()
    {
       cout << "ID: " << copyID << 
               "\nTitle: " << title << endl;
               //"\nBorrower: " << b << endl;
    }
    
    int main()
    {
       Copy aCopy("Jurassic Park");
       aCopy.toString();
       Copy bCopy("Tomb Raider");
       bCopy.toString();
    }


    ================

    Two very basic classes, as I am a big noob at C++ and from a Java background can anyone tell me what I would need to add to Copy.h and Copy.cpp so that the Copies could also include a Borrower.

    For instance when creating a Copy make it take the ID, title, and Borrower (Borrower could be initially set as (0000, "none", "none");

    Any help would be great

    Thanks guys

    Eggsy

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    Copy::Copy(string aTitle)
    {
       copyID = copyID;
       title = aTitle;
       copyID++;
    }
    what's the point of that ?

    also, don't pass arguments like this
    Borrower(int, string, string);
    do better:
    Borrower(int, const string&, const string&);
    that way instead of build a new class from the original, which causes an overhead, you only pass a pointer. In the end you'll have in both cases a copy of the original string, but passing by reference is more eficient. If you use the variable passed, to change it's value, just create a copy inside the function/method.

    I didn't get your doubt

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Few questions about how to pass objects around a C++ program.
    Using "pointers" or "references" is the most efficient way.

    You need to learn about "pointers", which don't have a counterpart in Java, and you need to learn about "references", which are much different in C++. You also need to learn about the differences between "passing by value" and "passing by reference". Passing by value does not exist in Java, and passing by reference has a different syntax and some different repercussions than in Java.

    Here is an example:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Cat
    {
    private:
    	string name;
    
    public:
    	Cat(string n)
    	{
    		name = n;
    	}
    	
    	Cat()
    	{
    		name="";
    	}
    
    	void display()
    	{
    		cout<<"I'm a cat, and my name is "<<name<<endl;
    	}
    };
    
    class Apple 
    {
    private:
    	Cat myCat;
    
    public:
    	Apple(const Cat& aCat) 
    	{
    		myCat = aCat;
    	}
    
    	void show()
    	{
    		cout<<"I'm an apple."<<endl;
    		myCat.display();
    	}
    };
    
    
    int main()
    {
    	Cat myCat("Whiskers");
    	myCat.display();
    
    	cout<<endl<<"Apple:"<<endl;
    	Apple a(myCat);
    	a.show();
    
    	return 0;
    }
    There are a couple of things going on in the Apple constructor that aren't apparent. First, when the Apple constructor is called, the Apple object is created before any code in the constructor body is executed. But, in order to create the Apple object, the compiler has to create the myCat object. It does that by calling the default Cat constructor--if there's no default Cat constructor, you will get an error. That is why I had to define a default constructor for the Cat class.

    After the Apple object is constructed, then the body of the constructor is executed. In the body of the constructor, the member variable myCat is assigned the object aCat. In order to do that assignment, the compiler calls something called the "copy constructor". The compiler automatically supplies a default copy constructor for a class. Unlike Java which supplies classes with only a default constructor, C++ supplies classes with various other default functions. In any case, the default copy constructor just copies each member of aCat into myCat. That can have dire consequences in some circumstances, but not in this case. In Java, if you did a similar assignment, then myCat and aCat would both refer to the same object. However, in C++, after the copy constructor copies each member of aCat into myCat, myCat and aCat are two different objects.

    In C++, you are going to see a lot of "const" specifiers in front of function parameter names, e.g.:

    Apple(const Cat& aCat)

    The const specifier announces that the function is not allowed to change the parameter in any way. So, if you have a function, and the function doesn't need to change the argument passed to it, put const in front of the parameter name. If you make a mistake inside the function and try to change a const argument, the compiler will give you an error--alerting you that you are doing something you said you didn't want to do.

    If the "const" specifiers in front of a function's parameter names confuse you, just cross them out--they don't affect the type of the variable you send to the function, they only affect whether the function is allowed to change their values inside the function.
    Last edited by 7stud; 06-10-2005 at 10:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing local objects by reference
    By manav in forum C++ Programming
    Replies: 15
    Last Post: 03-31-2008, 07:32 AM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  3. passing objects
    By r0bbb in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2005, 01:10 PM
  4. Passing objects
    By xshapirox in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2004, 11:34 AM
  5. Passing objects
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:00 AM