Monthly Archives: December 2009

Classes

Well, I was a bit caught up with some last minute homework last week, so I haven’t posted in a couple days. Tsk Tsk.


So what I went over today, was the Classes and Classes II page, at both of these links:
http://cplusplus.com/doc/tutorial/classes/
http://cplusplus.com/doc/tutorial/classes2/


I gained an understanding of why classes are useful, and I also found out that they are almost exactly the same as structures, except that the default access of the members of a class is private juxtapose to public.


It was a lot of reading, and some tinkering, to understand both of these pages pretty well. I haven’t created any code with the knowledge gained from these two pages, except for just some small modifications to make sure I understood what I was reading. Here is an example: I wrote a small circle class, and then printed the area of my circle:



#include
#include
using namespace std;


class CCircle {
    float x;
    public:
    void set_values (float);
    float area () {return (x*3.14159265*x);}
};


void CCircle::set_values (float a) {
    x = a;
}


int main () {
    CCircle circ, circb, circc;
    circ.set_values (3);
    circb.set_values (5);
    circc.set_values (7);
    cout << "circ area: " << circ.area() << endl;
    cout << "circb area: " << circb.area() << endl;
    cout << "circc area: " << circc.area() << endl;
    system(“PAUSE”);
    return EXIT_SUCCESS;
}


I plan to go over the next two pages in this tutorial, which are on friendship//inheritance, and polymorphism. Hopefully, I can read through those, and create some small programs like the one I made today. So tomorrow, I will create another small program using a class, and I will also use pointers and dynamic memory to do so. After this, I will read those next to article pages. I also have quite a bit of homework to do tomorrow, so we’ll see how far I get.

Lately the articles and blogs on both gamasutra and gamedev.net have been a bit bland, so sadly, I have no interesting articles for anyone to read this time :P

Tokenizing Example: Square

Well, I did it. I created a tokenizing program, which tokeninzes a square. I had a reference file, but the thing was coded so poorly that I didn’t even bother reading or understanding the darn thing. Here is the source code to the terribly written file: http://cplusplus.com/files/fraction.zip


This program took me about 30 minutes to write, and 2 hours to debug. I had multiple bugs, and wrote the loop that creates the “middle” of the square about 4 times before I finally created something clean looking and easy to read. A common error I made, was during the draw function, I would often reset the value of a or b to the incorrect corresponding variables width and height. If I were to label my variables as something extremely intuitive, I could have saved myself a lot of trouble :P


Here is the code for my square tokenizing program, it should look nothing like my reference (which I didn’t ever reference), and be easier to read:



/*Tokenizing Square*/

#include
using namespace std;

#define n ‘\n’;

void InputData (int &height, int &width)
{
//Stores data input by the user.
    cout << "Enter in the height of your square: ";
    cin >> height;
    cout << n;
    cout << "Enter in the width of your square: ";
    cin >> width;
    cout << n;
}

void Draw (int &height, int &width)
{
/*Draws the square. This function operates using the c variable to control wich
of the loops fires.*/
    int a, b, c = 0;
  
    a = height;
    b = width;
    c = 3;
  
/*The following two if statements check to make sure the dimensions aren’t too
high.*/
  
    if(height > 30)
    {
        cout << "Too large of a height, enter in something smaller!";
        cout << n;
        cout << n;
        c = 0;
    }
  
    if(width > 30)
    {
        cout << "Too large of a width, enter in something smaller!";
        cout << n;
        cout << b;
        c = 0;
    }
  
    while (b > 0, c == 3)
    {
        if (b == width)
        {
            cout << " ";
        }
      
        cout << "__";
        b–;
      
        if (b == 0)
        {
            cout << n;
            c = 2;
            b = width;
        }
    }
  
    while (a > 0, c == 2)
    {
        cout << "|";
      
        for (b = width; b > 0; b–)
        {
            cout << "__";
        }
      
        cout << "|";
        cout << n;
        a–;
        b = width;
      
        if (a == 0)
        {
            c = 1;
            a = height;
        }
    }
  
    while (b > 0, c == 1)
    {
        if (b == width)
        {
            cout << "|";
        }
      
        cout << "__";
        b–;
      
        if (b == 0)
        {
            c = 0;
            b = width;
            cout << "|";
            cout << n;
            cout << n;
        }
    }
}

int main()
{
//This is the main loop, which is a do while loop.
    char answer;
  
    do
    {
        int height, width, a, b;
      
        InputData(height, width);
        Draw(height, width);
  
        cout << "Would you like to draw another square (y/n) ";
        cin >> answer;
        cout << n;
  
        }while ((answer == ‘y’) || (answer == ‘Y’));
      
    return 0;
}


I also read quite a few different blogs and featured articles at http://www.gamasutra.com/, none of which are directly relevant to basic programming, such as what I am currently doing.

However, I want to repost this link: https://www.digipen.edu/uploads/media/digipen_podclass_issue_37.mp3

That is a podclass made by DigiPen. It is an interview of two programmers, and is extremely interesting and relevant to anyone aspiring to become a professional programmer for the games industry. Right click on the link, and select “Save target as”, or “Save link as”.

Tomorrow, I will be going back to school. I plan to still do a little coding, so expect to have another blog post to read.