Lectern

Search

Search IconIcon to open search

Class

Last updated Nov 7, 2022

A blueprint for creating objects, often a feature of many object-oriented programming languages.

# Attributes and variables

More or less an interchangeable term used to describe low-level storage slots attached to an object.

# Properties

In most OO languages, properties are used to define getter and setter functions to change the value of or read and return the value of an attribute in a class. The way properties are defined varies across programming languages.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class SavingsAccount
{
	// Variables are made private to prevent illegal accessing out of the object
	private string accountNo;
	public string AccountNo
	{
		get { return accountNo; }
		set { accountNo = value; } // Can be extended to have additional checks or validation
	}
	
	private string accountName;
	public string AccountName { get; set; } // Shorthand
}

# Methods

There are three types of methods in a user-defined class:

# Constructors

A special function used to create objects of a class. Usually a required method in many OO programming languages.

The following code chunks below show some examples for different languages:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class SavingsAccount {
	// The name of the class and constructor must match.
	// Default constructor — does not have parameters
	public SavingsAccount() { }
	
	// Parameterised constructor with parameters
	public SavingsAccount(string no, string name, double bal)
	{
		AccountNo = no;
		AccountName = name;
		Balance = bal;
	}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class SavingsAccount:
	# The name of the constructor must be __init__.
	# Default constructor — does not have parameters
	def __init__(self):
		pass
	
	# Parameterised constructor with parameters
	def __init__(self, no, name, bal):
		self.accountNo = no
		self.accountName = name
		self.balance = bal

# Printing methods

Because we won’t know what exactly to print out, we may need to define what to print out when a language’s printing function is called on an object of a class. The name of the function varies across programming languages.

1
2
3
4
5
6
7
8
9
class SavingsAccount {
	// The name of the function must be ToString.
	public override string ToString()
	{
		return "AccountNo: " + AccountNo +
			" AccountName: " + AccountName +
			" Balance: " + Balance;
	}
}
1
2
3
4
5
6
class SavingsAccount:
	# The name of the function must be __str__.
	def __str__(self):
		return "AccountNo: " + self.accountNo +
			" AccountName: " + self.accountName +
			" Balance: " + self.balance