Thread: Beginners guide to OOP from a Java background?!

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

    Question Beginners guide to OOP from a Java background?!

    hi guys and gals


    These are probably completely noob questions to most C++ programmers but coming from a Java background I am pretty much struggling with.

    If anyone has studied Java it would be great a bonus.

    In Java suppose we had a Borrower class and a Copy class.

    Just for simplicity imagine Borrower had vars of:

    Code:
    int id;
    string name;
    and Copy had:

    Code:
    int id;
    string copyName;
    Borrower borrower;
    so the Borrower being the obvious relationship.

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

    How do you go about implementing this in C++?

    As most things I have "googled" talk about inheritance which I understand (hopefully ) but not passing of objects?

    Thanks all, hopefully not bored you and someone can help me

    Eggsy

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

    following on..

    Also if anyone has anymore OOP questions please feel free to ask in my post


  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    73
    Code:
    class Borrower
    {
      public:
        Borrower();  // Constructor.
      private:
        int id;
        string name;
    };
    
    class Copy
    {
      public:
        Copy();  // Constructor.
      private:
        int id;
        string copyName;
        Borrower borrower;
    };
    Been awhile since I programmed in Java, but the two implement classes in just about the same way don't they? Except in Java you have to declare private or public for every function and variable.

    When you create a Copy variable Borrower constructor is automatically called.

    Code:
    int main()
    {
      Copy copyVar;
       
      return 0;
    }
    Am I answering your question incorrectly? I fear I am..
    Last edited by Deo; 06-07-2005 at 05:05 PM.

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

    Question Thanks

    No dont think you're answering incorrectly, I'm following you.

    I thought along the same lines as you.

    So now leading on, imagine Borrower.cpp as:

    Code:
    Borrower::Borrower(int aID, string aName)
    { 
          aID = id;
          name = aName;
    }
    
    other methods etc.....
    =============

    OK fine so far

    and Copy.cpp as:

    Code:
    Copy::Copy(int aID, string aName)
    {
          id = aID;
          copyName = aName;
          Borrower b(0, "");
    }
    
    void Copy::setBorrower(Borrower aBorrower)
    {
          borrower = aBorrower;
    }
    
    other method etc..
    It is the setBorrower method that doesn't really work again mainly because I think I am working in Java mode and dont really know what keywords to include?! Also would the above constructor work?

    Thanks

    Eggsy

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your code should work in this case. When you call setBorrower, a copy of the Borrower argument is passed to the function and stored in aBorrower. This is different than Java, where a reference to the Borrower argument is passed to the function. You have to be careful about this in C++ because often copies are made that don't do what you want. In this case it should be fine, since the members of the Borrower class are an int and a string which are copied safely by default.

    A slightly better version would use a constant reference to a Borrower as the SetBorrower parameter (const Borrower& aBorrower). This avoids a copy, and is more in line with what Java does. A better version of the Borrower constructor uses the intializer list, which does not exist in Java.

    You say that it's not working as it is written, if that's the case tell us what is not working.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This code:
    Code:
    Copy::Copy(int aID, string aName)
    {
          id = aID;
          copyName = aName;
          Borrower b(0, "");
    }
    is incorrect in both Java and C++. In Java, if you create an object at local scope inside a function and you don't return a reference to the object or assign the object to a member variable, then there won't be any references to the object in existence, and the object will be destroyed once the function finishes executing(more accurately the object will be subject to automatic garbage collection). In C++, it doesn't matter either way: an object created at local scope is destroyed once the function terminates, and any reference to the object will be invalid and cause your program to crash if you try to use it.

    Your question about how to pass objects in C++ gets to the heart of many of the differences between the two languages. You will need to get a C++ book, and learn about "passing by value" versus "passing by reference", which in turn requires you to learn about pointers and references. Also important will be learning how to dynamically allocate memory for a pointer, and the differences between statically and dynamically allocating memory for variables. Essentially, that means you have to read a whole book on beginning C++.

    Here is a taste of what you will learn. In java, all memory is dynamically allocated with the new operator, e.g.:

    Borrower borrower = new Borrower(10, "some text");

    You probably weren't even aware that in java you were "dynamically allocating memory", and you used that syntax as a matter of course because it's the only way you can create objects in java. In C++, that is not the case, and you can create objects in different ways. You can do this:

    Borrower borrower(10, "some text");

    or this:

    Borrower* myPointer = new Borrower(10, "some text");

    And, you can create a reference to an object like this:

    Borrower b(10, "some text");
    Borrower& myRef = b;

    Confused yet? It only gets more complicated.

    The purpose of a constructor is to initialize the data members, so you want to initialize the borrower data member as well. In java, you might do this:
    Code:
    public Copy(int id, string name, Borrower b)
    {
    	this.id = id;
    	this.name = name;
    	borrower = b;
    }
    In C++, however, objects can be passed to a function "by value" or "by reference". When you pass an object by value, it is copied, and the copy of the object is sent to the function. Inside the function, if you try to change the original object using the copy, it won't work. All you will end up doing is changing the copy, and when the function ends, the copy will be destroyed, leaving the original object unchanged. To pass an object by value, the syntax works like this:

    public Copy(int id, string name, Borrower b)

    That looks identical to the Java syntax for sending a reference to a function. In C++, the effect is totally different. Since there is no such thing as passing by value in java, it will be a new concept you will have to learn about.

    In C++, if you want to pass an object by reference, then you either use a pointer type or a reference type for the parameter:

    public Copy(int id, string name, Borrower* ptr) //pointer variable

    public Copy(int id, string name, Borrower& ref) //reference variable
    Last edited by 7stud; 06-08-2005 at 04:14 AM.

  7. #7
    Eggsy84 :: Out of depth!
    Join Date
    Jun 2005
    Location
    Manchester, UK
    Posts
    5
    Great posts guys thank you!

    Much learning needed I think as still could not get it to work and this is the basics!

    Thanks tho guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  2. Beginners OOP question
    By Skusey in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2006, 06:10 AM
  3. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  4. Java and OOP design help???
    By mart_man00 in forum Tech Board
    Replies: 10
    Last Post: 08-24-2003, 04:52 AM
  5. Absolute Beginner's Guide to Programming
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2003, 03:20 PM