Thread: Just need some things cleared up.

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Just need some things cleared up.

    I'm new to C++ and really want to learn it, I'm not new to coding so I kinda know what I'm doing, but there are some things I just don't get, and I want 'em cleared up before I go any further.
    I have read through the forums and FAQ but just didn't find or understand what I wanted.
    So, why use pointers? Why use classes?
    I've read through the tutorial on pointers throughout, but I just can't see any reason in using pointers, why don't just use the variables or memory they point to?
    I've read the tutorial on classes, but I havn't really understood them yet, but I keep wondering why use them? Is it just for more clean code?
    Thanks in advance, and sorry if these questions have been asked before.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    C++ has quite a few features for "hiding" pointers (references, standard containers, etc.) But a couple things:
    1. (More classical) I'll use an example that was here on the forum today: a person wanted to make a channel list a la IRC. So each item on the list knows (1) a name and (2) a pointer to the next person on the list. This way when person #4 leaves, you don't have to move #s 5 through 30 down one spot; you just update the pointer on #3 to skip the leaving individual.
    2. (Definitely C++) You might have, say a base class (there's that word again) of Person, and from that base class have derived classes of Teacher, Assistant, and Student. Then even though I have a bunch of different types of people, I can store pointers to the base classes to keep in an array or suchlike. Thus I can store pointers-to-Person, even though each pointer might actually point to a Teacher, or a Student.

    Classes are how C++ implements OOP; an instance of a class is an object.

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Still don't get it, sorry :/
    Pointers point to variable, but if you know that variable, why do you need something to point you to it? It's like asking a person if he/she knows where another person is, but you already know where that person is...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Pointers do not have to point to variables. They can point to allocated memory blocks that do not have a "normal" name.

    Edit: An example:
    Code:
    #include <iostream>
    
    int main() {
        int size;
        std::cout << "How many numbers do you need? ";
        std::cin >> size;
        int *pointer_to_int = new int[size]; //Set aside enough memory for size ints.  It has no name, but is pointed to.
        std::cout >> "Off you go: ";
        for (int i = 0; i < size; i++) 
            std::cin >> pointer_to_int[i]; //index notation works on pointers too
        for (int i = 0; i < size; i++) 
            std::cout << pointer_to_int[i] << std::endl;
        delete[] pointer_to_int; //give the memory back to the operating system
        return 0;
    }
    I only typed the code in the box, I didn't compile it, but I didn't intentionally put any errors in.
    Last edited by tabstop; 10-18-2008 at 07:01 PM.

  5. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Oh... So they just make the coding easier and faster?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> Thanks in advance, and sorry if these questions have been asked before.

    Well, these threads are rather instructive:

    https://cboard.cprogramming.com/showthread.php?t=105808


    I hope that shed some light on pointers.
    These will also give you an overview of OOP and places to learn, but this isn't even really the surface. It can be difficult if impossible to cover a paradigm in one paragraph. You'll have to do some learning on your own. Should explain to you the idea of a class somewhere.

    https://cboard.cprogramming.com/showthread.php?t=106665
    https://cboard.cprogramming.com/showthread.php?t=16981
    https://cboard.cprogramming.com/showthread.php?t=92882

    The question of "why use a class?" can be a bit harder. I like to think that once you know what they are, you can try to write something... like a manager for your music files. As you think about the application and its features you'll likely need a couple objects, if you decide to use OOP in the program. A lot of the times an algorithm that uses objects is simpler to write and comprehend, but it depends on what you know.
    Last edited by whiteflags; 10-18-2008 at 07:16 PM. Reason: accidently linked the wrong thing, should be okay now.

  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Well, thanks for the fast and awsome replies you both ^^
    I'm kinda getting closer to pointers and classes now, I'll just read like a maniac until I fully know what it is and why to use them

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You're welcome...

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you want to know why pointers exist then I totally suggest writing your own linked-list. That will teach you not only the necessity of pointers, but a lot of other things as well, that every computer programmer should learn.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ok, I kinda get pointers and classes now, but just need something cleared up on linked lists.
    Could a linked list be used as a person search?
    Like, you input the name of a person, then can the linked list be used in such a way that it goes through area nodes? As in, nodes like London, Liverpool, Manchester and those nodes include all the names of people living in those places?
    I'm not thinking about doing that, just an example and sorry if I'm unclear.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> Could a linked list be used as a person search?
    I don't know, I'm pretty sure that the best search you could do is linear search which is too slow if there are more than a few dozen nodes. You'll want a better data structure if you're going to be looking up addresses a lot, like a tree or a hash table.

  12. #12
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I'm just trying to find out what linked lists could be used for, since the tutorial ain't the best :/

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One classic example would be a list of shoppers at a checkout. The list gets longer when more people join the queue, and shorter from the other end as customer's groceries are processed.
    Or a more practical example that's similiar: Incoming network requests at a web server.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ohh... Now I think I get it ^^ Thanks!

  15. #15
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct basket{
    	int worth;
    
    	basket *next;
    };
    
    int main()
    {
    	int totalIncome;
    
    	basket *root;
    	basket *payed;
    
    	//First basket of the day
    	root = new basket;
    	root->next = 0;
    	root->worth = 100;
    	totalIncome = root->worth + totalIncome;
    	payed = root;
    	//No idea what happenes here
    	if( payed != 0 ) {
    		while ( payed->next != 0 )
    			payed = payed->next;
    	}
    	//Start understanding again
    	//Second basket
    	payed->next = new basket;
    	payed = payed->next;
    	payed->next = 0;
    	payed->worth = 82;
    	totalIncome = payed->worth + totalIncome;
    	//Third basket
    	payed->next = new basket;
    	payed = payed->next;
    	payed->next = 0;
    	payed->worth = 128;
    	totalIncome = payed->worth + totalIncome;
    	//Output totalIncome onto the screen
    	cout<<"You earned: " << totalIncome << " today from your grocery store";
    }
    Ok, did I understand Linked Lists correctly?
    And I don't get a compiler error from that, but some pop up error when I run the .exe saying totalIncome is being used without being initialized, why's that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  3. Funny things happening outside of function
    By drb2k2 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 02:26 PM
  4. things to think about... [from www.nuclearwastesite.com]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-11-2001, 12:47 AM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM