Zebra0.com

csharp objects

There are many Classes that we could create that are a Person plus additional attributes. Here we have created an Employee class. The Person class is the base class, and Employee is a derived class. The Employee class inherits all the attributes of hte Person class except the constructors.

Code for the Employee Class:.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Employees
{
    // Inheritance: An Employee is a Person.
    // Employee has all the attributes of a Person plus a rate. 
    class Employee: Person
    {
        const Double MINIMUM_RATE= 9.25;
        private Double Rate;

        public Employee()
        {
            SetName("unknown");
            SetBirthday(1900, 1, 1);
            Rate = MINIMUM_RATE; // Starting wage
        }
        public Employee(String name, Double rate)
        {
            SetName(name);
            SetBirthday(1900, 1, 1);
            if (rate > MINIMUM_RATE) Rate = rate;
            else Rate = MINIMUM_RATE; // Starting wage
        }

        public void SetRate(Double rate)
        {
            if (rate > MINIMUM_RATE) Rate = rate;
        }
       public Double GetRate()
        {
            return Rate;
        }
        public Double GetPay(int hours)
        {
            return Rate * hours;
            // To do: adjust for overtime.
        }
    }
}

Code in form load to create an instance of the Employee class:

private void Form1_Load(object sender, EventArgs e)
{
   // Create a new Employee
   Employee worker = new Employee("Mark", 10.00);
   Text =worker.GetName() + " earns " + 
     worker.GetPay(40).ToString("$0.00")+ " per week";
}

To Do: Create another class such as Customer, Client, Student, etc. that is derived from the Person class.

End of lesson, Next lesson: Zebra Notes: Open, Save, Save As, and Format with a Rich Text Box