Help - Search - Members - Calendar
Full Version: Classes?
BleepingComputer.com > Software > Programming
   
IKnew
I am using "C++ All-In-One Desk references for Dummies" to learn C++, i am having problems finding out when to use classes and what they are for... theres many examples of classes in the book but it seems that its lot easier to do the same thing as what the examples are doing using just functions then the class...
groovicus
Classes are what contain those functions, and you can not use the functions unless you are using the class. Classes make up libraries, and can be thought of as specialized units. Classes are a concept associated with object oriented programming....
IKnew
isnt functions like...

int myfunction()
{
return 5;
}

int main()
{
cout<<myfunction()<<endl;
return 0;
}


while a class is like

class myclass
{
public:
int getvalue() {return itsvalue;}
void setvalue(int value) {itsvalue=value;}

private:
int itsvalue;
};


i know that a class is made up of functions but i also know that functions doesn't have to be in a class, i dont get when i should use the classes...
groovicus
Let's start out with this question.....If functions are not in a class, then where are they?

The concept of using classes is so that you can have chunks of code that are independent of each other, but can be put together in such a way that they make a whole program. Simple programs only consist of one class. All classes are objects, or rather, all objects are created from classes. An object is an instance of a class, and all Objects have fundamental methods, or functions, associated with them, which is maybe why you are under the impression that functions don't have to be in classes.

One uses multiple classes when a program needs to perform multiple, unrelated functions. Take this web page as a loose example. One class would be to display the stats at the bottom of the page. One class would generate the list of forums. One class generates the text boxes where you make responses. One class interacts with the database. None of these classes know anything about each other. A final class assembles all of the information and displays it as a web-page. In reality, there are dozens and dozens of classes that make up a complex program.

Does that explain it any better? Understanding classes, objects, and instances is incredibly confusing at first.... at least it was for me.
IKnew
thank you, i sort of understand it now, though my head hurts... wacko.gif
groovicus
Here, try this for making your head hurt:
http://www.c3.lanl.gov/mega-math/workbk/in...ty/inhotel.html

hysterical.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.