Class or Structs?

This article is for meant to give a basic understanding of what structs, and classes are. This article is directed towards those with basic knowledge of programming or those with basic knowledge of programming languages that use either of both concepts but not both concepts.

To begin, let’s define both terms from a general perspective.

  • Classes: A class is a blueprint for creating objects (a particular data structure), providing initial values for members of the object called member state or member variables or object attributes, and the implementations of the behaviour of those members called member functions or methods.

  • Struct: A struct is a user-defined data type that can contain data members and maybe member methods (- the behaviour) which can be of any data type (primitive or custom defined).

A struct is basically from the word structure. A struct defines the data should look like. With struct, you define the shape of the data and not the behaviour of that data. This is where you begin to see the difference between a class and a struct.

A class is a blueprint for creating both the data and the behaviour of that data.

Let’s see some real-world examples of classes and structs.

If you are familiar with high-level languages like Python and JavaScript, you will most probably have seen classes in action where you can define related-data and define how those data will behave.

class Rectangle:
  def __init__(self, width, height):
    print("This is a class that defines the structure of a rectangle and it's behaviour")
    self.width = width
    self.height = height

  def area(self):
    return self.width * self.height

An example of a class is the Python class above, where you define that the rectangle takes in two values or data (the width and height data), and you define the behaviour for the defined data (- The area method).

Now let’s see how structs is defined in most languages that implement structs.

struct Rectangle {  // Solidity definition of a struct
  uint width;
  uint height;
}
struct Rectangle {  // Rust definition of a struct
  width: u32,
  height: u32,
}

As you see from both the solidity and Rust implementation above, struct is used to define how Rectangle will be used in the code. Rectangle is a user-defined type, or a custom type that you writing the code decides to define. Structs generally are structure definition. The definition might vary in syntax in programming languages just as in the code above, but the use-case remains the same. Structs allows you define data-types that will be used the same way as programming language’s built-in data-types.

As the official Rust book puts it,

A struct, or structure, is a custom data type that lets you package together and name multiple related values that make up a meaningful group. If you’re familiar with an object-oriented language, a struct is like an object’s data attributes.

One thing to note is that languages that have no built-in class type have a way of implementing behaviour for the structs you have defined. Here’s an example of this.

struct Rectangle {
  width: u32,
  height: u32,
}

impl Rectangle {
  pub fn area(&self) -> u32 {
    self.width * self.height
  }

  fn isSquare(&self) -> u32 {
    self.width == self.height
  }
}

Certain languages like Rust allows you to define the implementations of the struct you have defined using an external keyword. Other languages allow you to use the struct defined in a class. Basically, struct and class work to ensure data consistency in your code development.

Notice the use of pub in the Rust code above. pub here means that the code is marked public. Which brings up another important feature - Accessibility.

Implemented methods of a struct can be private or public depending on the language. In C++, the member methods of a struct are public by default. In Rust, the implemented member methods public within the file they are defined but can be marked with pub for use when outside the file the struct was defined. So, the accessibility of struct methods vary with the language but works to give the code structure.

These are all the subtle changes between structs and classes. This knowledge allows you to define user-defined types that serve a particular use case in your code logic, even in high-level languages like Python and JavaScript. This strict data-type system will make your code more concise and certain, and you will write better code.

Because just like you know that int will throw error if used in place of strings in your code, your user-defined structs will also help control using invalid types where they are being used.

Summary

  • Classes: A class is a blueprint for creating objects (a particular data structure), providing initial values for members of the object called member state or member variables or object attributes, and the implementations of the behaviour of those members called member functions or methods.

  • Struct: A struct is a user-defined data type that can contain data members and maybe member methods (- the behaviour) which can be of any data type (primitive or custom defined).

You can check out my other articles about programming, web, blockchain to learn more programming concepts.

Thank you for reading. 🙂