|
Post by Malagrond on Feb 27, 2011 21:10:52 GMT -6
Classes (and by extension, objects) are one of the most useful features of C++. They are an evolution of the struct from C. The basic form of a class is: class ClassName {
};
Inside the class, you define variables and methods (functions specific to the class) to help manipulate the data being stored in and used by the object. Within a class, there are various scopes when defining members. - public - Can be accessed directly from outside the class
- private - Can only be accessed by other members of the class
- protected - Can be used by friend functions, objects that inherit from the class, and the class's members.
We'll get into friends and inheritance some other time. When defining a class, it's customary to keep all of your data private and the important methods public like so: class MyClass { private: int myInt; char myChar; public: int getMyInt(); char getMyChar(); };
When you define the methods, you must use the scope resolution operator to define which class the method belongs to. Using the above class example, you would define getMyInt() like so: int myClass::getMyInt(){
// Instructions here.
return myInt; }
We'll get into constructors and destructors in the next part.
|
|