Classes

Time Complexity

Space Complexity

N/A

N/A

Note

This page goes over how to implement a class in code. For the concept of what classes are about, see Object Oriented Programming.

A “class” is the name given to an “object” (thing), or type, definition in code. Classes are how you describe data that doesn’t fit into primitive (basic) types such as “int” or “float”. “String” is an example of a class; somewhere in the language code, the string type is defined, and how it interacts with other types and other operations it performs are defined in “member variables” and “member functions”. “Member variables” hold data pertaining to the object, like the different characters in a string. “member functions” define functions the object can perform, such as combining with other strings.

Classes are useful for organizational purposes; instead of making a lot of variables and many different functions, you can define which variables and functions are associated with which object. Here is an example class “person”, with one “member variable” (a “name”) and one “member function” (“sayHi”), along with a definition of how the object (thing) is declared:

#include <iostream>
#include <string>
using namespace std;

class person{

  string name;

  public:

  person(){
    name = "Tim Apple";
  }
  person(string inputName){
    name = inputName;
  }
  // or
  //   person(string inputName = "Tim Apple"){
  //     name = inputName;
  //   }
  // or
  //   person(string inputName = "Tim Apple") : name(inputName) {}

  void sayHi(){
    cout << "Hi, my name is " + name + "." << endl;
  }
};

int main(){

  person Tim;
  person Jeff("Jeff");

  Tim.sayHi();
  Jeff.sayHi();

  return 0;
}
class Program {
  public class person{
    private string name;

    public person(){
      name = "Tim Apple";
    }
    public person(string inputName = "Tim Apple"){
      name = inputName;
    }
    // or
    // public person(string inputName = "Tim Apple"){
    //   name = inputName;
    // }

    public void sayHi(){
      System.Console.WriteLine("Hi, my name is " + name + ".");
    }
  }
  static void Main(string[] args)
  {
    person Tim = new person();
    person Jeff = new person("Jeff");

    Tim.sayHi();
    Jeff.sayHi();
  }
}
public class classes {
  public static class person{
    private String name;

    public person(){
      name = "Tim Apple";
    }
    public person(String inputName){
      name = inputName;
    }

    public void sayHi(){
      System.out.println("Hi, my name is " + name + ".");
    }
  }
  public static void main(String[] args) {
    person Tim = new person();
    person Jeff = new person("Jeff");

    Tim.sayHi();
    Jeff.sayHi();
  }
}
function person(inputName){
  if (typeof(inputName) === 'undefined'){
    this.name = "Tim Apple";
  }else{
    this.name = inputName;
  }
}
person.prototype.sayHi = function sayHi(){
  console.log("Hi, my name is " + this.name + ".");
}

var Tim = new person();
var Jeff = new person("Jeff");

Tim.sayHi();
Jeff.sayHi();
class person{
  constructor(inputName = "Tim Apple"){
    this.name = inputName;
  }
  sayHi(){
    console.log("Hi, my name is " + this.name + ".");
  }
}
var Tim = new person();
var Jeff = new person("Jeff");

Tim.sayHi();
Jeff.sayHi();
class person:
  name = None
  def __init__(self,inputName = "Tim Apple"):
    self.name = inputName
  def sayHi(self):
    print("Hi, my name is " + self.name + ".")

Tim = person()
Jeff = person("Jeff")

Tim.sayHi()
Jeff.sayHi()
class person{
  var name: String

  init(_ name: String = "Tim Apple"){
    self.name = name
  }

  func sayHi(){
    print("Hi, my name is " + self.name + ".")
  }
}

let Tim = person()
let Jeff = person("Jeff")

Tim.sayHi()
Jeff.sayHi()

Notes

The line #include <string> is needed to use strings.

The line public: is needed so that the class members following that keyword are accessible outside the class definition. Everything before that is set to private by default because it is a class, making them inaccessible.

The functions that have the class name and no type (person(...) here) are called “constructors”. They let you change the values in the class when declaring an object from it. The constructor being used has one definition with no variables and one definition with one variable (the name); this is so the object can be made with or without the variable (this is also called “function overloading”).

A constructor with a default for the parameter, as well as a constructor with an “initialization list” on top of that, are given as examples directly following that. They are a sort of shorthand for the constructor that is not commented out, and they do the same thing.

Classes in C++ need to be declared before they are used; the compiler reads sequentially. For instance, if the class person was declared after the main function, there would be an error because there was no definition for person yet. Typically, classes are put in a seperate “header” file to avoid this.

Class members are accessed with the dot (.) operator. E.g. Tim.sayHi(); for a function, or Tim.name; for a variable.

C# code will typically have “access modifiers” on everything. These are typically the words public or private. What this does is limit the accessibility of whatever variable or function is being modified; if a function is public it can be used anywhere. if a function is private it can only be used inside the local scope or definition. By default, C# will assign the most restrictive access modifier.

The functions that have the class name and no type (person(...) here) are called “constructors”. They let you change the values in the class when declaring an object from it. The constructor being used has one definition with no variables and one definition with one variable (the name); this is so the object can be made with or without the variable (this is also called “function overloading”).

A constructor with a default for the parameter is given as an example following the constructor that is not commented out. This effectively combines the two previous definitions, giving a value for inputName if there is none given. They do the same thing.

Class members are accessed with the dot (.) operator. E.g. Tim.sayHi(); for a function, or Tim.name; for a variable.

Java code will typically have “access modifiers” on everything. These are typically the words public or private. What this does is limit the accessibility of whatever variable or function is being modified; if a function is public it can be used anywhere. if a function is private it can only be used inside the local scope or definition. The default access modifier in Java only allows use in a local scope (you can think of it as private).

The functions that have the class name and no type (person(...) here) are called “constructors”. They let you change the values in the class when declaring an object from it. The constructor being used has one definition with no variables and one definition with one variable (the name); this is so the object can be made with or without the variable (this is also called “function overloading”).

Class members are accessed with the dot (.) operator. E.g. Tim.sayHi(); for a function, or Tim.name; for a variable.

JavaScript does not have “classes” per se. This is because there are no types; everything is just an object in JavaScript. However, JavaScript can simulate the functionality of classes, with what is called a “prototype”. You can think of it as typical classes with funny syntax (a prototype is an extension of another object in actuality).

Regardless, a “class” is made by defining the constructor (class name function which may take arguments to define the object) and then defining class members with the “prototype” syntax as shown above. This is atypical, but is changed in ES6 (JavaScript as of 2015, the next example) to have normal class syntax. However, not all JavaScript in mass use is that updated. This should, in most cases, work like classes in other languages.

Class members are accessed with the dot (.) operator. E.g. Tim.sayHi(); for a function, or Tim.name; for a variable.

A popular language which has been built around JavaScript but features types was made by Microsoft called “TypeScript”. You can see that here.

See the other tab for details, but this is the updated JavaScript syntax for classes. This looks and works like the other main languages.

Here is a compiler to practice with if your JavaScript isn’t updated.

Also, “ES6” stands for “ECMAScript 6”, the standard JavaScript follows.

Class members are accessed with the dot (.) operator. E.g. Tim.sayHi(); for a function, or Tim.name; for a variable.

Python is surprisingly wordy with class syntax. You define the “constructor”, the function that runs when the object is made, with the function __init__. It is always that function. Also, whenever there is a member function (including __init__), you need to include self as a parameter so it can access itself. Furthermore, whenever you want to access a member variable, you need to use the syntax self.memberVar, which refers to itself (self), then the dot (.) or member operator, then the variable itself.

Setting a parameter equal (= operator) to something in the parameter field () is just giving it a default value (value given if nothing is given in the function call).

A variable can be made without really assigning a value by assigning the None value.

Aside from those things, class definitions and usage is the same as other languages. However, there is no concept of “encapsulation”. So, no private anything. Everything is accessible to everyone. See the reasoning behind that in this news letter, where the developers literally say “we’re all adults here”.

Class members are accessed with the dot (.) operator. E.g. Tim.sayHi() for a function, or Tim.name for a variable.

Syntax should be self explanatory here. Some things to note:

init is always the constructor, setting a parameter equal (= operator) to something in the parameter field () is just giving it a default value (value given if nothing is given in the function call), and the syntax var name: String defines the type of a variable without defining the value in a class setting.

Class members are accessed with the dot (.) operator. E.g. Tim.sayHi() for a function, or Tim.name for a variable.