classes,pointers,etc

This is a discussion on classes,pointers,etc within the C++ Programming forums, part of the General Programming Boards category; ok ive been reading some c++ tutorials and i did the examples for pointers,classes,and templates but nomatter how many tmes ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    classes,pointers,etc

    ok ive been reading some c++ tutorials and i did the examples for pointers,classes,and templates

    but nomatter how many tmes i read i still dont understand them can someone just give me the basic run through of wut they do and why i would use them.

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Temples - So you can use different datatypes with the same block of code: int, char, an object of a class, float, etc. which saves time and lines from making the exact same function or class over again to use a different datatype. e.g. sqrt function templated so with that function you can use float, int, double, etc. instead of making srqt_float function, sqrt_int function, sqrt_double function, w/e.

    Pointers - So you can save space by passing around the id tag of a int/struct/class/whatever, instead of copying all the values to a new int/struct/class whatever. Also allows you to update something you pass by pointer, ie. send a pointer to int thats equal to 5, in the function change that int to 10 and it changes from where it came from too. Whereas if you passed the int 5 to a fresh int and changed the fresh int, it would not change the int you passed, and you would have to return the value, causing extra work and more memory required.

    Classes - So you can put everything (functions, classes) that you want in one block of code. It makes it much clearer to have a class that operates on one thing, and a different class that does a different thing. You can also do operator overloading on classes, so you can + a class together, or/and -, or/and *, or/and /. When you think about it for a second, if you have a class handling their own thing.. that would mean if you seperated that class from the program you could plug it in a different program and it would still work (most likely), yes.. re-usable code is a big advantage to classes. Functions may be reusable, but I've got to say if you have 4 or 5 functions that do the same thing all scattered in your program, some relying on others, it would be a bit more complicated to track them down. Theres more reasons like how they can inherite code from other classes, making them more simple, and that enforces that each class does its own thing. Theres also the fact that if you were using global variables for all of this then if you created a local variable somewhere, it would turn into an error, whereas with classes that doesnt happen, and you know what you're doing (no accidental mistakes) because you have to go ClassObject.variable, and theres also the fact you can make them private, furthering that. Theres more reason also.

    Thats my most basic explanation, without using the correct terms for them (templates are use for generic code, classes are for object-oriented code, which is encapsulation, and allows inheritance, and such.)

    I suggest getting a book where its explained in detail and effectively..
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

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