In C++ I Cannot Grasp Pointers and Classes

后端 未结 27 1615
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 02:25

I\'m fresh out of college and have been working in C++ for some time now. I understand all the basics of C++ and use them, but I\'m having a hard time grasping more advance

相关标签:
27条回答
  • 2020-12-03 02:44

    Your problem seems to be the C core in C++, not C++ itself. Get yourself the Kernighan & Ritchie (The C Programming Language). Inhale it. It's very good stuff, one of the best programming language books ever written.

    0 讨论(0)
  • 2020-12-03 02:46

    Pointers and classes are completely different topics so I wouldn't really lump them in together like this. Of the two, I would say pointers are more fundamental.

    A good exercise for learning about what pointers are is the following:

    1. create a linked list
    2. iterate through it from start to finish
    3. reverse it so that the head is now the back and the back is now the head

    Do it all on a whiteboard first. If you can do this easily, you should have no more problems understanding what pointers are.

    0 讨论(0)
  • 2020-12-03 02:49

    For classes:

    The breakthru moment for me was when I learned about interfaces. The idea of abstracting away the details of how you wrote solved a problem, and giving just a list of methods that interact with the class was very insightful.

    In fact, my professor explicitly told us that he would grade our programs by plugging our classes into his test harness. Grading would be done based on the requirements he gave to us and whether the program crashed.

    Long story short, classes let you wrap up functionality and call it in a cleaner manner (most of the time, there are always exceptions)

    0 讨论(0)
  • 2020-12-03 02:51

    Pointers and classes aren't really advanced topics in C++. They are pretty fundamental.

    For me, pointers solidified when I started drawing boxes with arrows. Draw a box for an int. And int* is now a separate box with an arrow pointing to the int box.

    So:

    int foo = 3;           // integer
    int* bar = &foo;       // assigns the address of foo to my pointer bar
    

    With my pointer's box (bar) I have the choice of either looking at the address inside the box. (Which is the memory address of foo). Or I can manipulate whatever I have an address to. That manipulation means I'm following that arrow to the integer (foo).

    *bar = 5;  // asterix means "dereference" (follow the arrow), foo is now 5
    bar = 0;   // I just changed the address that bar points to
    

    Classes are another topic entirely. There's some books on object oriented design, but I don't know good ones for beginners of the top of my head. You might have luck with an intro Java book.

    0 讨论(0)
  • 2020-12-03 02:51

    For pointers and classes, here is my analogy. I'll use a deck of cards. The deck of cards has a face value and a type (9 of hearts, 4 of spades, etc.). So in our C++ like programming language of "Deck of Cards" we'll say the following:

    HeartCard card = 4; // 4 of hearts!
    

    Now, you know where the 4 of hearts is because by golly, you're holding the deck, face up in your hand, and it's at the top! So in relation to the rest of the cards, we'll just say the 4 of hearts is at BEGINNING. So, if I asked you what card is at BEGINNING, you would say, "The 4 of hearts of course!". Well, you just "pointed" me to where the card is. In our "Deck of Cards" programming language, you could just as well say the following:

    HeartCard card = 4; // 4 of hearts!
    print &card // the address is BEGINNING!
    

    Now, turn your deck of cards over. The back side is now BEGINNING and you don't know what the card is. But, let's say you can make it whatever you want because you're full of magic. Let's do this in our "Deck of Cards" langauge!

    HeartCard *pointerToCard = MakeMyCard( "10 of hearts" );
    print pointerToCard // the value of this is BEGINNING!
    print *pointerToCard // this will be 10 of hearts!
    

    Well, MakeMyCard( "10 of hearts" ) was you doing your magic and knowing that you wanted to point to BEGINNING, making the card a 10 of hearts! You turn your card over and, voila! Now, the * may throw you off. If so, check this out:

    HeartCard *pointerToCard = MakeMyCard( "10 of hearts" );
    HeartCard card = 4; // 4 of hearts!
    print *pointerToCard; // prints 10 of hearts
    print pointerToCard; // prints BEGINNING
    print card; // prints 4 of hearts
    print &card; // prints END - the 4 of hearts used to be on top but we flipped over the deck!
    

    As for classes, we've been using classes in the example by defining a type as HeartCard. We know what a HeartCard is... It's a card with a value and the type of heart! So, we've classified that as a HeartCard. Each language has a similar way of defining or "classifying" what you want, but they all share the same concept! Hope this helped...

    0 讨论(0)
  • 2020-12-03 02:55

    In the case of classes I had three techniques that really helped me make the jump into real object oriented programming.

    The first was I worked on a game project that made heavy use of classes and objects, with heavy use of generalization (kind-of or is-a relationship, ex. student is a kind of person) and composition (has-a relationship, ex. student has a student loan). Breaking apart this code took a lot of work, but really brought things into perspective.

    The second thing that helped was in my System Analysis class, where I had to make http://www.agilemodeling.com/artifacts/classDiagram.htm">UML class diagrams. These I just really found helped me understand the structure of classes in a program.

    Lastly, I help tutor students at my college in programming. All I can really say about this is you learn a lot by teaching and by seeing other people's approach to a problem. Many times a student will try things that I would never have thought of, but usually make a lot of sense and they just have problems implementing their idea.

    My best word of advice is it takes a lot of practice, and the more you program the better you will understand it.

    0 讨论(0)
提交回复
热议问题