img

Through my studies at college, I have learned many things about programming. In this post, I want to talk about Classes, Objects, and Inheritance! My primary language being C#, the examples will be coded in that language.

Starting with Classes, what are they? A class is a template for methods and variables. Here is an example for a driver cass that would run the program:

class Driver
{
  private static void main()
  {
    //Code
  }
}
                          

In Object Oriented Programming, we use these classes to create instances. These instances can be used to run functions, access variables, and anything else. In my program Sprocket Ordering Form, I use this principle to make a form where you can ord4er various sprockets.

Here is one of the sprocket classes from my program:

namespace SproketOrderForm;

public class SteelSprocket : Sprocket
{
  //Constructor
  public SteelSprocket(int itemID, int numberOfItems, int numberOfTeeth) : base()
  {
      ItemID = itemID;
      NumberOfItems = numberOfItems;
      NumberOfTeeth = numberOfTeeth;
                                  
      Description = ToString();
                          
      Calc();
  }
                          
  //Calculates total price
  protected override void Calc()
  {
      Price = NumberOfItems * NumberOfTeeth * 0.05f;
  }
                          
  //Returns a description
  public override string ToString()
  {
      return base.ToString() + ", Material: Steel";
  }
}
                          

I then reference this and create an instance of it in the following code:

//calculates the total value of the items in the lists
public void Calc()
{
    //a float for storing the total value 
    float totalVal = 0;

    //loops through the tokens in the list
    foreach (Sprocket sprocket in sprockets)
    {
        //adds the current tokens price to the total value variable
        totalVal += sprocket.Price;
    }

    //saves it to the publuic variable
    TotalPrice = totalVal;
} 
                          

In these two files, I first create a Steel Sprocket class. This class has two functions and one constructor. The functions being Calc() and ToString() These provide functionality to calculate price and return a formatted srring that describes the sprocket. In the second piece of code, it lops through all of the sprockets in a list and calculates the total price based on all of the sprockets.

Now, you might notice that even though my sprocket is called SteelSprocket, I never reference this word in the function. The only thing I do is use a Sprocket object. This is called inheritance.

Inheritance is a programming procedure that allows you to reuse code by referencing the behaviors and data of an object.

From CodeAcademy.

Essentially, this means that we can define a base class with a subset of methods and variables, and let other objects inheritant from that class. This makes it possible to have a reference to that base object and put in any object that inheritants from that class.

In my code, my Steel Sprocket inheritants from the Sprocket class This class is the following:

public abstract class Sprocket
{
    //Variables
    public float Price {get; set;}
    public int ItemID {get; set;}

    private int numberOfItems;
    public int NumberOfItems
    {
        get
        {
            return numberOfItems;
        }
        set
        {
            numberOfItems = value;
        }
    }

    private int numberOfTeeth;
    public int NumberOfTeeth
    {
        get
        {
            return numberOfTeeth;
        }
        set
        {
            numberOfTeeth = value;
        }
    }

    public string? Description {get; set;}

    //Constructors
    public Sprocket()
    {

    }

    public Sprocket(int ItemID, int numberOfItems, int numberOfTeeth)
    {
        this.ItemID = ItemID;
        this.numberOfItems = numberOfItems;
        this.numberOfTeeth = numberOfTeeth;
    }

    //abtsract vouid
    protected abstract void Calc();
    
    //Returns base description
    public override string ToString()
    {
        return $"Order Num: {ItemID}, Number {numberOfItems}, Teeth: {numberOfTeeth}";
    }
}
                          

In this, I define a bunch of variables and functions that any sprocket class can reference and call. Notice the line where I wrote protected abstract void Calc();. This is a piece of code that lets me define a method all sprocket classes can reference, while still leting me implement it in the sprocket classes.

It can be seen at this function in the Steel Sprocket class. I override the abstract function Calc() and define what this function does.

                          
//Calculates total price
protected override void Calc()
{
    Price = NumberOfItems * NumberOfTeeth * 0.05f;
}
                          

Thank you for reading this far about Classes, Objects, and Inheritance. You can find the source code for the repo here.