need help wiht using classes...namespaces...

This is a discussion on need help wiht using classes...namespaces... within the C++ Programming forums, part of the General Programming Boards category; Ok.. I have alot of experience with php and html ect. I knew some c++ but never really gain experience ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    32

    need help wiht using classes...namespaces...

    Ok.. I have alot of experience with php and html ect.

    I knew some c++ but never really gain experience and haven't practice in a while since I been working on a website.

    I would like someone to explain to me what classes are used for. I did read what classes are on here bu still confused.

    I am guessing class are ways to organize code. Like for example I make a class house.

    this would mean any function in class house would be all functions and code for a house.

    like door () would be open close lock ect.

    am I somewhat right that class is like organizing code so if I were making a computer game I can have the house models and code to be under a house class??

    I would like also someone to explain me about namespaces.


    explain to me by just giving a example that would show a picture of what it's uses are for.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    You are pretty correct about classes. They are used in order to "create" an object "of" something, like a house, a door, a car, a washmachine, etc.
    They are designed in such a way as to mimick reality. They have a public interface (like your car has a wheel, and pedestals), and takes care of its own ticking (ie, the internal details of what makes the car run is handled by the class).
    They're also great for code reuse.

    Namespaces are, at least to me, a way to organize data.
    Think of it as storage boxes. Each box is named. Then you put stuff into those boxes. Then to access that stuff, you either tell the compiler to look inside the boxes or you explicitly tell the compiler what box it should look inside.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    32
    ok I am not sure if I got confused with name spaces with classes.

    so namespaces is like a storage area like I can say namespace house this will allow me to grab the class house if saved in that namespace.


    SO are classes like functions??? but basicly a tree of functions???


    I mean what I thought that for example the house class. The house class would just be a name of a class which this class should have all code functions and vars ect everything related to a house.

    I thought of it more like a tree of functions but an arganized way of putting functions together in one place.

    so when you need to call the function you just use a pointer to that class and that function.


    the class house would hold all the code for the house it's just a way of grouping all the functions into a group and you use that group as a reference when you need to run the functions.

    that is what I thought.

    I am not sure if classes are really like a big function.

    it's kinda confusing for me but I am still trying to learn.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Namespaces are a way to decorate names, so that you can avoid issues with name collisions. (Alternatively you might uses prefixes to names, but namespaces have other advantages over that.)

    If library A and library B both have a class called Bitmap and you need to use both libraries, then provided at least one of them uses namespaces you have a way to tell which Bitmap you mean in each instance.

    Namespaces aren't essential for C++ code, though. You can well declare classes and functions that are not in any namespace (just globally visible).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    Quote Originally Posted by hockey97 View Post
    so namespaces is like a storage area like I can say namespace house this will allow me to grab the class house if saved in that namespace.
    No, not quite.
    Classes and namespaces are unrelated.
    Think of that postbox example. Namespaces can be used for categorizing. You can stuff a lot of functions into a namespace called House so you know these functions are used for manipulating houses.

    SO are classes like functions??? but basicly a tree of functions???
    No, a class is a blueprint of an object.
    You create an instance of this blueprint which represents an object.
    Say a class House. When you create a House, it IS the house itself, or is meant to represent that.

    I mean what I thought that for example the house class. The house class would just be a name of a class which this class should have all code functions and vars ect everything related to a house.
    It would have everything to make the house work.

    I thought of it more like a tree of functions but an arganized way of putting functions together in one place.
    That's a namespace.

    so when you need to call the function you just use a pointer to that class and that function.
    Not quite, because classes represent objects, as thus you must CREATE them first, allowing you to have many houses or many cars.

    the class house would hold all the code for the house it's just a way of grouping all the functions into a group and you use that group as a reference when you need to run the functions.
    Again, that's namespaces.

    I am not sure if classes are really like a big function.
    Nope, classes are meant to represent an object in a realistic way.

    Quote Originally Posted by anon View Post
    Namespaces are a way to decorate names, so that you can avoid issues with name collisions. (Alternatively you might uses prefixes to names, but namespaces have other advantages over that.)

    If library A and library B both have a class called Bitmap and you need to use both libraries, then provided at least one of them uses namespaces you have a way to tell which Bitmap you mean in each instance.

    Namespaces aren't essential for C++ code, though. You can well declare classes and functions that are not in any namespace (just globally visible).
    That's one use. I like to use them to organize data, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by hockey97 View Post
    so namespaces is like a storage area like I can say namespace house this will allow me to grab the class house if saved in that namespace.
    Correct. NameSpaceName::House will give you the house object found n that namespace. This may be different from another house object in another namespace.

    SO are classes like functions??? but basicly a tree of functions???

    I mean what I thought that for example the house class. The house class would just be a name of a class which this class should have all code functions and vars ect everything related to a house.

    I thought of it more like a tree of functions but an arganized way of putting functions together in one place.

    so when you need to call the function you just use a pointer to that class and that function.

    the class house would hold all the code for the house it's just a way of grouping all the functions into a group and you use that group as a reference when you need to run the functions.

    that is what I thought.
    Classes do group functions together relating to that class. But the model used is of "What can a house do?" or "What can I do with a house?" instead of being a conglomerate of all function relating to houses. Generally functions are better grouped in namespaces rather than classes, unless those functions fit the above questions.

    But the main purpose of a class is to store state.
    I am not sure if classes are really like a big function.
    Classes are more like one big collection of data, along with a few operations on them. They behave a lot like primitives types like int, double, ect., except you can add other functions to them besides these provided by built in operators.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    32
    Thanks for the replies it help me understand classes.

    so basically the class house will have all data and functions of the house.

    so like the class house could have functions and var of doors,oven,a/c.

    well I will go back on this site and take a look at the coding of classes some more.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >NameSpaceName::House will give you the house object found n that namespace.
    >This may be different from another house object in another namespace.
    A good real world example of namespaces is area codes in the United States.

    Telephone numbers have seven digits. Two people might have the same seven digit telephone number, and the area code can then be used to disambiguate them. Sometimes the area code is assumed and you can continue to use the seven digit telephone number. You can also use area codes to organize and categorize telephone numbers all over the country.
    My best code is written with the delete key.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Prelude View Post
    A good real world example of namespaces is area codes in the United States.
    Hey, we have area codes in Canada too. We're not that small.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Hey, we have area codes in Canada too. We're not that small.
    Canada is a part of the United States and simply refuses to admit it...just like the rest of the world.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. namespaces or static classes ?? you decide
    By Hussain Hani in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2009, 12:20 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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