The Basics of Programming

Structures and Classes

In some computer languages, such as C and C++ you can combine variables into groups called structures or classes. A structure is basically a grouping of values that have a common purpose such as the attributes of window on a computer screen, a character in a game, a customer in a database, a product item, and so on. In C, you would use the keyword struct. The types of variables can be mixed so you can have integers (int) character strings (char) and boolean (bool) variables in the same structure.


struct Window {
 int left_position;
 int top_position;
 int width;
 int height;
 char[20] window_title;
 bool close_gadget;
 bool minimize_gadget;
 bool resize_gadget;
struct customer {
 char[20] customer_number;
 char[20] title;
 char[20] firstname;
 char[20] lastname;
 char[20] street;
 char[20] city;
 char[20] county;
 char[20] country;
 char[20] postcode;
 char[20] telephone_number;
 char[20] balance;

To use a structure, you specify the word struct, the name of structure and the name of the variable e.g. struct customer customer1;
To use the individual values of a structure you would use the format variable.fieldname, where a dot seperates the variable and field name for example, customer1.street.
customer1.customer_number = "100101";
customer1.title = "Mr";
customer1.firstname = "Simon";

Classes are similar to structure but classes are using in languages such as C++ that use Object Orientated programs that define the objects to use in a program and groups values and subroutines and functions (called methods) in to a class. Typical subroutines, would be to create an instance of a class or delete an instance or set or get values of an instance. An instance is when you create a specific entry such as a person that is a customer. The routine that creates an instance of a class is called a constructor, and one that deletes one is called a destructor (the method starts with a tilde or ~ character).

Examples, (this is simplified code):

class Customer {
 string customer_number;
 string title;
 string firstname;
 string lastname;
 string street;
 string city;
 string county;
 string country;
 string postcode;
 string telephone_number;
 string balance;
Customer(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11) {
   customer_number = p1;
   title = p2;
   firstname = p3;
   lastname = p4;
   street = p5;
   city = p6;
   county = p7;
   country = p8;
   postcode = p9;
   telephone_number = p10;
   balance = p11;

To create a new instance of a customer you would call the class' creation subroutine, called a constructor, and supply the necessary values.

Customer mycustomer; /* declare instance of class customer called mycustomer */

mycustomer = new Customer("C000100", "Mr", "James", "Smith", "22 Lee Road", "Leeds", "West Yorkshire", "England", "LS1 1PQ", "0113 456821", "50.00"); /* set up a new customer using constructor */

where mycustomer is the instance of the class customer, the word new is used when creating an instance, and customer is the name of the constructor of the class, also called customer.

See CPlusPlus pages for more about classes.

Next Page