converting from java

This is a discussion on converting from java within the C++ Programming forums, part of the General Programming Boards category; im just in the middle of learning the difference between java and c++, although i do know c already... is ...

  1. #1
    Unregistered
    Guest

    Post converting from java

    im just in the middle of learning the difference between java and c++, although i do know c already...


    is there an equivilent of interfaces in c++?

    how do you extend classes?

    how do you extend more than one class?

    how do you use null?

    how do you return a pointer?

    is there any way of using references instead?

    is memory allocation (malloc?) critical to learn (am i going to kill my computer if i dont learn to use it?)?

    what collections (preferably linked lists) are available or do I have to make my own?




    i know how sick everyone gets of these types of questions so i thank you for your time and patience

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164

    Re: converting from java

    is there an equivilent of interfaces in c++?
    Hmm... COM interfaces are interfaces. I don't really know Java so I don't know what interfaces it has.

    how do you extend classes?
    Put a ':' after the class. Example:

    class Car : Vehicle
    {
    long cDoors;
    }

    how do you extend more than one class?
    ??

    how do you use null?
    NULL is the same as 0. It is often used to initialize pointers.

    how do you return a pointer?
    From a function:

    char* func()
    {
    char* pointer;
    return pointer;
    }

    returns a char pointer.

    is there any way of using references instead?
    Returning references:

    char& func()
    {
    char c = new char;
    return c;
    }

    Although I don't think references works the same in C++ as in Java. Correct me if I'm wrong.

    is memory allocation (malloc?) critical to learn (am i going to kill my computer if i dont learn to use it?)?
    It is important but it's really easy also. Using the 'new' and 'delete' are faster than malloc. It's just simple:

    char* pointer = new char[1000];
    delete pointer;

    what collections (preferably linked lists) are available or do I have to make my own?
    There are vectors, etc. but I would do my own. Preferably, I would do template classes for linked lists.
    // Gliptic

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    >is there an equivilent of interfaces in c++?

    You have abstract base classes that you could use instead.



    >how do you extend classes?

    class derived : public base
    {
    };

    where derived and base are your class names


    >how do you extend more than one class?

    class derived : public base1, base2
    {
    };



    >how do you use null?

    Same as in Java: CObject* pObject = NULL;


    >how do you return a pointer?

    CObject* function( COBject* o )
    {
    o->Method();
    return o;
    }


    >is there any way of using references instead?

    CObject& function( COBject& o )
    {
    o.Method();
    return o;
    }


    >is memory allocation (malloc?) critical to learn

    No. Use new and delete. The difference to Java however
    IS critical to learn. You don't need 'new' with anything
    non-dynamic. If you need exactly one string, no need to
    'new' it. On the other hand, keep good track of everything
    created with new, because it's your job to make sure it
    gets delete'd again.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    char* pointer = new char[1000];
    delete pointer;
    Shouldn't that be delete [] pointer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. First Java Class at college
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 09-29-2004, 10:38 PM
  3. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  4. Replies: 6
    Last Post: 08-07-2003, 02:05 PM
  5. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21