Note: I used php code braces to preserve syntax only, my pdf is beter though.

Classes and Structures, C++ Vs C

Classes and Structures

C++ is really C with fancy structs and an Object Orientated approach. In C the emphasis was on functions that operate on our data. In C++ we still have functions, that has not changed, what has changed is we put more emphasis on our data and the functions (methods) that deal with our data. In C we had structures, these structures represented a group of data per say. There was one drawback however and that was we could not put functions inside structures, to group the data with the functions that deal with that data. In C++ we can now put our functions inside structs, this is an Object Orientated Programming approach (from now on referred to as OOP). C++ has a new name for structs called classes, well actually class. The keywords class and struct do the same thing in C++ with the exception of a few minor details.

To declare and instantize a structure that defines the corners of a box we might do the following

PHP Code:
struct  Point
{
     
int  x;
     
int  y;
}   
instance1,   instance2
Remembering that the instances are optional. Since we know that C++ has classes that do more or less the same thing we could try the following.

PHP Code:
class Rect
{
     
struct Point mTopL;
     
struct Point mBottomR;
     
int mArea;
myRect
And indeed this is valid C++ however it is no longer required to include the keyword struct when declaring an instance of the structure. We could thus remove the struct keyword and it would make no difference. Now we said classes and structs are basically the same thing, but why when we try to assign myRect.mTopL.x = 3 we get a compiler error? Because C++ introduces something new called Access Modifiers and with it comes three new keywords public, private and protected. These keywords control who has access to what. Trying to assign instance1.x = 3 however does not give us a problem because structs are public by default and classes are private by default. The following change fixes the problem.

PHP Code:
class Rect
{
     public:
        
struct Point mTopL;
        
struct Point mBottomR;
        
int mArea;
myRect
This solves our problem but there is a reason for Access Modifiers and that is Encapsulation. When most people turn the television on they are not interested how it works inside just that they pushed the button and they can now watch their favorite show. This is the idea of encapsulation, to provide an interface to an object without worrying about how it works inside. To support this idea we might define a class Window that contains the private data width and size along with a public method Draw(). In this case Draw() is our interface to a window object, we might have something like this.

PHP Code:
class Window
{
     
int width,   height
    
     public:
        
void Draw();  // Not nessisary to type void for empty argument list

};

int main (int argcchar *argv[] )
{
    
Window myWindow;  // Create instance (object) of Window
     
    
myWindow.Draw();
    
    return 
1;

Of cource this will not compile right away because we are missing a definition for the Draw() method. This does serve however to illistrate a few ideas. The first idea that we can now group functions and data together, and these are called methods. Second that when we create an instance of a class it is referred to as an object. And thirdly that our method Draw provides an interface to the class, I need not wory about how Draw works just that when I call it I get a window drawn on my screen. If the class was inside a header file and was part of a class library I might not even know that it has two variables width and height. However it might be usefull to set the width and height so that I can customize the window that gets drawn. It is common to make accessor methods that provide a way to set and get the private data. Private data is private only to the users of the class and not to the methods of the class. Our Draw() method can quite haopily set and get the values of width and height without problems. Take a look at the following

PHP Code:
class Window
{
     
int widthheight;
     public:
         
void Draw() {};
         
         
// Accessor methods

         
int GetWidth();
         
int GetHeight();
         
void SetWidthint value);
         
void SetHeightint value);
};

int Window::GetWidth()
{
      return 
width;
}

int Window::GetHeight()
{
      return 
height;
}

void Window::SetWidth(int value)
{
      
width value;
}

void Window::SetHeight(int value)
{
      
height value;

Here we see for the first time how we define class methods. We have the function (method) prototype inside the class, and then outside of the class we define our methods but in an unusual way. When defining class methods we prefix the method name with the name of the class to which it belongs followed by the scoping operator. This scoping operator :: is used a lot in C++, it basically indicates belonging. We might for example have two classes, one called Window and one called Button and they might both have a method GetWidth. If they both have a method with the same name, then we would have a problem without scoping. With scoping however there is no problem because Button::GetWidth() and Window::GetWidth() can be distinguished by the compiler. It is also rather sexy but we wont get into that. We also notice that we now have a way to Set and Get the values of myWindow's private data. We might simply assign int windowWidth = myWindow.GetWidth() without any problems. Because GetWidth is a member of the Window class it can read its own private data without problems.

This was a basic overview of classes to get you used to using them, It is imperitive that you understand this material because classes are used all the time in C++ and you will need it to understand the other lessons.