Thread: Moving from C/Java to C++: some questions

  1. #1
    Just
    Guest

    Moving from C/Java to C++: some questions

    Hello,

    I've programmed in Java and C for quite some time, but I'm now trying to learn C++. A few questions have come up, which I'd appreciate help with.

    (1) In Java, it's common for a constructor to look like this:

    Code:
    public MyConstructor(int x, char c) {
      this.x = x;
      this.c = c;
    }
    In other words, the this keyword is used to differentiate between the parameters to the constructor, and the members of the class. Is there a way to do this C++? (With *this or something?) Or is this just not commonly done, and instead the parameters are given different names to the member variables?

    (2) C has headers such as <time.h>, <stdlib.h> etc. I can use these in C++, right? So what's the deal with <ctime>, <cstdlib>, and so forth? Is there a difference between the two styles?

    (3) Is there such a thing as an ANSI-C++? I'm learning C++ from an old book (early 90s) - is this a problem? Is there a standardised version of C++ that only newer books would cover?


    Thanks for all your help,

    Justin

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    1. Yes, in C++ you can use "this". If I am correct, in C++ "this" is a pointer, so it would be something like this:

    Code:
    MyClass::MyClass (int x, char c)
    {
        this->x = x;
        this->c = c;
    }
    2. Yes, you can the C standard headers. If I am correct, then ctime.h is the new notation for time.h. It is the same, but the c is placed in front of the name to indicate that it is an 'old' C headerfile.

    Compare:

    #include <time.h> // old notation
    #include <ctime> // new notation

    3. Yes, there is an ANSI C++. I thought the latest standard was from 1998 (?). Anyway since 1990 there are a lot of adaptations made to C++, so I'd suggest to get a more recent book. Bruce Eckel has written a book on C++ which is quite good and free for download. See http://www.mindview.net/Books. There are some drafts around. A very good book on C++ is the one of the creator of C++, Bjarne Stroustrup, it's called The C++ Programming Language. Also see his site: http://www.research.att.com/~bs/C++.html.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    MyClass::MyClass (int x, char c)
    {
        this->x = x;
        this->c = c;
    }
    Most c++ programers will balk if you write code like that.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Yep, it was just an example.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    MyClass::MyClass (int x, char c) : x_(x), c_(c) {};

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    All good answers to the Java/C/C++ question!!!
    Mr. C: Author and Instructor

  7. #7
    Just
    Guest
    Thanks for the replies. I had forgotten about the weird initialisation method (I'd only seen it a couple times before):

    Code:
    MyClass::MyClass (int x, char c) : x_(x), c_(c) {};
    Now why the heck would you want to write code like that? C'mon, you can't possibily tell me that's anywhere near as clear as the "Java" method! The C++ way is incredibly terse and ugly, and the Java way is clear and easy to read. So why would C++ programmers "balk" at seeing code like the function I wrote?

    Just

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Yes, but Java is not easy when using I/O from keyboard and formatting numbers!!
    Mr. C: Author and Instructor

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    I personally hate looking at Java code. Just looks foreign and has it's own way of doing things when I'm used to C++.
    Be yourself for those who mind don't matter, and those who matter don't mind.

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Just
    Thanks for the replies. I had forgotten about the weird initialisation method (I'd only seen it a couple times before):

    Code:
    MyClass::MyClass (int x, char c) : x_(x), c_(c) {};
    Now why the heck would you want to write code like that? C'mon, you can't possibily tell me that's anywhere near as clear as the "Java" method! The C++ way is incredibly terse and ugly, and the Java way is clear and easy to read. So why would C++ programmers "balk" at seeing code like the function I wrote?

    Just
    Because it's faster, and it makes complete sense once you understand it.

    Why would c++ programmers "balk"? You have two variables named the same maybe, and you didn't use initilization lists.

    See here: http://www.parashift.com/c++-faq-lit....html#faq-10.6

  11. #11
    Just
    Guest
    "I personally hate looking at Java code. Just looks foreign and has it's own way of doing things when I'm used to C++."

    That's exactly how I feel about C++ because I'm used to Java :P
    Hopefully that'll change... eventually.

    "Because it's faster, and it makes complete sense once you understand it."

    Ok I'll give you the speed advantage (although that doesn't apply to intrinsic types like in my example). But of course it makes complete sense once you understand it - so does mine. I still think the Java way is far easier to understand... but I'll get used to it... eventually.

    Just

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM