C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-01-2008, 01:51 PM   #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.
hockey97 is offline   Reply With Quote
Old 09-01-2008, 02:03 PM   #2
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-01-2008, 02:40 PM   #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.
hockey97 is offline   Reply With Quote
Old 09-01-2008, 03:08 PM   #4
The larch
 
Join Date: May 2006
Posts: 3,086
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.

Quote:
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).
anon is offline   Reply With Quote
Old 09-01-2008, 03:17 PM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.

Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-01-2008, 03:18 PM   #6
Registered User
 
Join Date: Apr 2006
Posts: 1,193
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.

Quote:
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.
Quote:
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.
King Mir is offline   Reply With Quote
Old 09-02-2008, 12:37 PM   #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.
hockey97 is offline   Reply With Quote
Old 09-02-2008, 02:02 PM   #8
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Old 09-02-2008, 02:12 PM   #9
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,122
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.
cpjust is offline   Reply With Quote
Old 09-02-2008, 02:22 PM   #10
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
>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.
Prelude is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
namespaces or static classes ?? you decide Hussain Hani C++ Programming 9 07-11-2009 11:20 AM
im extreamly new help rigo305 C++ Programming 27 04-23-2004 11:22 PM
Exporting VC++ classes for use with VB Helix Windows Programming 2 12-29-2003 05:38 PM
Prime Number Generator... Help !?!! Halo C++ Programming 9 10-20-2003 07:26 PM
include question Wanted420 C++ Programming 8 10-17-2003 03:49 AM


All times are GMT -6. The time now is 01:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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