Announcement

Collapse
No announcement yet.

What's a good C++ book?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What's a good C++ book?

    I have never really done much programming in C++, and while Kuci informs me that it is a terrible hideous language, it seems like it would be a good idea for me to learn it at some point. I'm guessing that there is no equivalent to the K&R C book for C++, but can anyone recommend a good reference? If it comes with a CD or has any sort of "learn in x time period" then I'm assuming it's crap. (Is that a safe assumption?)

    I also have a few questions about it:

    1. What precisely are destructors? Does C++ call them when you free an object?
    2. What exactly happens when you use the new operator? Does it somehow recursively malloc everything an object needs? Is it a Good Thing or a Bad Thing? Does it work the same way it works in Java?
    3. Why does cout overload the left shift operator? Is it just too cool to be a method? Where else are operators overloaded in bizarre and idiosyncratic ways?

    I apologize if these are stupid questions, but #3 convinced me that I should assume nothing. Seriously though, as far as I can tell it has literally nothing to do with bit shifting.

    Thanks.
    If there is no sound in space, how come you can hear the lasers?
    ){ :|:& };:

  • #2
    Originally posted by Hauldren Collider View Post
    I have never really done much programming in C++, and while Kuci informs me that it is a terrible hideous language, it seems like it would be a good idea for me to learn it at some point. I'm guessing that there is no equivalent to the K&R C book for C++, but can anyone recommend a good reference? If it comes with a CD or has any sort of "learn in x time period" then I'm assuming it's crap. (Is that a safe assumption?)
    C++ is not hideous. It's a ridiculously popular language for a reason. C++ in the wrong hands is hideous. Unfortunately, there are many wrong hands.

    Once you are familiar with the basics, Effective C++ is very popular. There's a sequel called More Effective C++. Also, any book by Herb Sutter is good (he does Exceptional C++).

    The K&R equivalent for C++ is cryptically called The C++ Programming Language.

    I also have a few questions about it:

    1. What precisely are destructors? Does C++ call them when you free an object?
    Destructors are methods to release resources allocated by that object. In general, if your constructor uses "new", your destructor uses "delete".

    They get called when you delete an object. There are, of course, complications to this (particularly with inheritance).

    2. What exactly happens when you use the new operator? Does it somehow recursively malloc everything an object needs? Is it a Good Thing or a Bad Thing? Does it work the same way it works in Java?
    "new" instantiates the object on the heap, calling its constructor. It allocates enough memory for all class members as well.

    3. Why does cout overload the left shift operator? Is it just too cool to be a method? Where else are operators overloaded in bizarre and idiosyncratic ways?
    Frankly, C's printf is no less bizarre and idiosyncratic. You can overload operators in C++. The << syntax (and >> for cin) were just chosen for the sake of the standard. You can think of the << as something puking, and >> as a funnel. Out and in!

    In C++, you can do things like:
    Code:
    cout << car;
    where car is an instance of a Toyota class. It could print out "Make: Toyota, Model: Camry", etc.

    It's just a different way to think about the data. Which is the whole point of being object-oriented.
    "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
    Ben Kenobi: "That means I'm doing something right. "

    Comment


    • #3
      By the way, the C++ FAQ is a fantastic reference for newbies.

      "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
      Ben Kenobi: "That means I'm doing something right. "

      Comment


      • #4
        And finally, the best reason to learn C++: A good C++ developer will earn way more than a good C# developer, Java developer, or Objective-C developer.

        The only developers who make more are COBOL developers.

        C++ is a hard language to do right. That means once you can do it right, you have tremendous value. Let the kids play with their managed languages.

        It's very sad when I need to review the C++ code of "today's kids" who go to school doing software engineering in Java or C#. No concept of memory management or how computers actually work.

        Me: "Just pass a void* and dynamic cast it."
        Kid: "Holy ****, you can do that? That's ridiculously dangerous."

        I wish I wore shades at work, I could've done the Caruso right there.
        "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
        Ben Kenobi: "That means I'm doing something right. "

        Comment


        • #5
          Originally posted by Asher View Post
          The K&R equivalent for C++ is cryptically called The C++ Programming Language.
          Ah, yeah. But The C Programming Language is basically the best C book, and it doesn't seem like the same is true of this.
          Destructors are methods to release resources allocated by that object. In general, if your constructor uses "new", your destructor uses "delete".

          They get called when you delete an object. There are, of course, complications to this (particularly with inheritance).
          Can you elaborate on these complications? Do you have to rewrite the entire destructor for every child class?

          Thanks again.

          Also , do COBOL developers really make that much? I suppose it's hard to find people willing to sacrifice their happiness...
          If there is no sound in space, how come you can hear the lasers?
          ){ :|:& };:

          Comment


          • #6
            Also if I can get off the waitlist I will be taking Intro to Computer Systems next semester, which promises lots of memory management and even assembly. I'm feeling a mixture of excitement and dread.

            At least I won't have to prove loop invariants or other annoying theoretical crap.
            If there is no sound in space, how come you can hear the lasers?
            ){ :|:& };:

            Comment


            • #7
              Originally posted by Asher View Post
              Me: "Just pass a void* and dynamic cast it."
              Kid: "Holy ****, you can do that? That's ridiculously dangerous."

              I wish I wore shades at work, I could've done the Caruso right there.
              Unions

              (The C kind, not the labor kind)

              I had to write a virtual machine for my final assignment in my last CS class (we had a shell, we just had to write the instructions like byte push and add). I'm pretty sure I'm the only person who used unions to cast void*s to whatever type they were supposed to be.
              If there is no sound in space, how come you can hear the lasers?
              ){ :|:& };:

              Comment


              • #8
                Originally posted by Hauldren Collider View Post
                Ah, yeah. But The C Programming Language is basically the best C book, and it doesn't seem like the same is true of this.
                It's worth a read, but C++ has really evolved over the years and Effective C++ and Exceptional C++ are far more relevant today. Another book is "Thinking in C++", which I'm told is excellent but I can't be bothered to read.

                Can you elaborate on these complications? Do you have to rewrite the entire destructor for every child class?
                No, not necessarily. A very common cause of memory leaks is people don't mark their destructors as virtual for the entire inheritance hierarchy. This means if you have an class of "Automobile", which is the parent of the "Car" class, which is a parent of the "Toyota" class...and each class has its own constructors/destructors...if you have an array of "Automobile" objects and you destroy the array, unless you've made all of the destructors virtual only the base class' destructor is called. This leaves a large memory leak as the Car and Toyota destructors would not be called.

                It's a more advanced concept that may not make too much sense until you know more C++.

                Also , do COBOL developers really make that much? I suppose it's hard to find people willing to sacrifice their happiness...
                Yes, because COBOL powers very, very important systems for very, very wealthy companies and organizations and the pool of COBOL developers is very, very small.
                "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                Ben Kenobi: "That means I'm doing something right. "

                Comment


                • #9
                  dp
                  "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                  Ben Kenobi: "That means I'm doing something right. "

                  Comment


                  • #10
                    Originally posted by Asher View Post
                    No, not necessarily. A very common cause of memory leaks is people don't mark their destructors as virtual for the entire inheritance hierarchy. This means if you have an class of "Automobile", which is the parent of the "Car" class, which is a parent of the "Toyota" class...and each class has its own constructors/destructors...if you have an array of "Automobile" objects and you destroy the array, unless you've made all of the destructors virtual only the base class' destructor is called. This leaves a large memory leak as the Car and Toyota destructors would not be called.

                    It's a more advanced concept that may not make too much sense until you know more C++.
                    I think I understand it--it calls the destructors for all the parents if they are virtual. But I can imagine some scenarios in my head where that results in double freeing memory if you aren't careful.
                    If there is no sound in space, how come you can hear the lasers?
                    ){ :|:& };:

                    Comment


                    • #11
                      Before buying any books, take a look at the C++ FAQ. Start with the Big Picture section. I find it is very succinct while still clear.
                      "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                      Ben Kenobi: "That means I'm doing something right. "

                      Comment


                      • #12
                        I use other people's code and places like cplusplus.com

                        I think that C++ is a great language.

                        Jon
                        Jon Miller-
                        I AM.CANADIAN
                        GENERATION 35: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.

                        Comment


                        • #13
                          I'm pathetically giddy about the coming of C++11. It was only recently formalized...back in August.
                          "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                          Ben Kenobi: "That means I'm doing something right. "

                          Comment


                          • #14
                            What do you think of CUDA versus OpenCL?

                            JM
                            Jon Miller-
                            I AM.CANADIAN
                            GENERATION 35: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.

                            Comment


                            • #15
                              How much development is still done in C++? Many companies seem to have shifted entirely to web languages.

                              Cuda is very cool, I played with it back in high school.
                              If there is no sound in space, how come you can hear the lasers?
                              ){ :|:& };:

                              Comment

                              Working...
                              X