Lessons Home

Introduction to Classes

 

Programmer-Defined Objects

 

Introduction

Consider an object such a book. It is made of different characteristics such as its title, the name of the author, the number of pages, the type of cover, etc. To create a program that treats such an object, you can declare a variable for each of these characteristics. Here is an example:

var Title     : String;
var Author  : String;
var Pages : int;
var CoverType   : char;

Title     = "JScript .NET Application Programming";
Author  = "Jeffry Ndoma";
Pages = 944;
CoverType   = 'P';

print("Book Characteristics");
print("Title:  ", Title);
print("Author: ", Author);
print("Pages:  ", Pages);
print("Cover:  ", CoverType);

This would produce:

Book Characteristics
Title:  JScript .NET Application Programming
Author: Jeffry Ndoma
Pages:  944
Cover:  P

So far, we have used such variables to group variables that otherwise belong to the same entity. A better solution is to group such variable into a new variable.

Creating a Class

To create a class, use the class keyword followed by a name for the object. The name of a class follows the rules we have applied so far for variable and function names. As a name that represents a group of items, a class has a body that would be used to define the items that compose it. The body of a class starts with an opening curly bracket "{" and ends with a closing one "}".

Since a class is built from combining variables, you will list each variable inside of the body of the class. Each item that composes the class is represented as a complete variable declared with a data type and a name. As a variable, each declaration must end with a semi-colon.

Continuing with our book class, you could create it using the class keyword as follows: 

class Book
{
    var Title     : String;
    var Author  : String;
    var Pages : int;
    var CoverType  : char;
}

The items that compose a class are called members of the class.

Declaring a Class

To use a class in your program, you can first declare a variable from it, using the same syntax we have been using so far. Here is an example:

class Book
{
    var Title : String;
    var Author : String;
    var Pages : int;
    var CoverType  : char;
}

var BrandNew : Book;

Unlike regular data types, a variable of a class must be explicitly allocated a memory space, big enough to contain its data. This is done using the new operator. Here is an example:

class Book
{
    var Title : String;
    var Author : String;
    var Pages : int;
    var CoverType : char;
}

var BrandNew : Book;

BrandNew = new Book;

You can also allocate memory when declaring the variable, that is, on the same line. Here is an example:

class Book
{
    var Title : String;
    var Author : String;
    var Pages : int;
    var CoverType : char;
}

var BrandNew : Book = new Book;

After declaring a variable of the class, you can access any of its members using the period operator ".". First, type the name of the variable, followed by a period, followed by the name of the member you want to access. For example, to access the member Title of the above class, you would write:

BandNew.Title

Using this syntax, you can display the value of a class member using print().

 

 

Class Variable Initialization

To initialize a class, you can access each member and assign it an appropriate value. Here is an example:

class Book
{
    var Title     : String;
    var Author    : String;
    var Pages     : int;
    var CoverType : char;
}

var BrandNew : Book;

BrandNew = new Book;

BrandNew.Title     = "Webber Databases With JScript .NET";
BrandNew.Author    = "Catherine Mamfourh";
BrandNew.Pages     = 1225;
BrandNew.CoverType = 'H';

print("Book Characteristics");
print("Title:  ", BrandNew.Title);
print("Author: ", BrandNew.Author);
print("Pages:  ", BrandNew.Pages);
print("Cover:  ", BrandNew.CoverType);
 

Classes and Member Functions

 

Introduction

The primary motivation of using classes in a program is to create objects as complete as possible. An object must be able to handle its own business so that the other objects of the program or of another program would only need to know which object can take care of a particular need they have.

A regular variable, as a member of an object, cannot handle assignments; this job is handled by particular functions declared as members of a class. A function as a member of a class is also called a Method. In our lessons, the words “method” and “function”, when associated with a class, will refer to the same thing: a member function of the class. 

Creating Member Functions

To create a member function of a class, type the function keyword followed by a name, the necessary parentheses, and the curly brackets that delimit the body of a function. Here is an example:

class FRectangle
{
  var Length : double;
  var Height : double;
  function Perimeter()
  {
  }
}

To implement a method, use the same techniques we used to define regular functions. When a method is a class' member, it has access to the member variables of the same class; this means that you don't need to pass the variables as arguments. You can just use any of them as if it were supplied. Here is an example:

class FRectangle
{
  var Length : double;
  var Height : double;

  function Perimeter() : double
  {
      return (Length + Height) * 2;
  }

  function Area() : double
  {
      return Length * Height;
  }
}
 

Class Members Interactions

Regardless of how the member methods of a class are implemented, any method can call another without using an access operator. This allows a class' methods to exchange information among themselves easily. Furthermore, unlike regular functions where a function must be declared prior to another function calling it, the method members of a class don't abide by that rule: one method can call another method before or after the other has been implemented, as long as it is defined somewhere. Here is an example:

class FRectangle
{
    var Length : double;
    var Height : double;

    function Perimeter() : double
    {
        return (Length + Height) * 2;
    }

    function Area() : double
    {
        return Length * Height;
    }

    function ShowCharacteristics()
    {
        print("Rectangle Characteristics");
        print("Length:     ", Length);
        print("Height:      ", Height);
        print("Perimeter: ", Perimeter());
        print("Area:         ", Area());
    }
}

var Recto : FRectangle = new FRectangle;

Recto.Length = 24.55;
Recto.Height = 20.75;
Recto.ShowCharacteristics();

Once an object is defined and behaves as complete as possible, the other function or objects of the program can make the appropriate calls trusting that the called object can handle its assignments efficiently. This ability allows you to (tremendously) reduce the work overload of the other components of a program.

 

Previous Copyright © 2004 FunctionX, Inc. Next