cet203 software development session 1a revision of classes
TRANSCRIPT
CET203SOFTWARE DEVELOPMENT
Session 1ARevision of Classes
Objectives
• Introduction to Object Oriented Programming• Model a basic class using the UML class
diagram notation• Implement a class in C#• Introduce new naming conventions and C#
properties• Design and implement a simple multi-class
system
Object Orientation
Class
Object 1 Object 2 Object 3 Object 4
A ‘class’ is a software design which describes the general properties of something which the software is modelling.
Individual ‘objects’ are created from the class design for each actual thing
Example
• A class Car used in a computer system for a second-hand car showroom
• The class would be a model of the important characteristics of a car for the purposes of the business
• For each car in the showroom the system would have a separate object – an instance of class Car.
Class Diagram
Class name
Instancevariables
Methods
The type of object theclass is defining(e.g. Car)
The information (properties)the objects will record(e.g. model, price)
The actions the classcan perform(e.g. sell)
An example class - Employee
• We will create a class to model an employee of a company• The company needs to store the following information about
each employee– Name– Department– Salary
• The company determines the salary of an employee based on their profits for the year
• Therefore the salary can go up or down by a percentage amount each year
• We will model this in a UML class diagram
UML Class Diagram
Employee
name : Stringdepartment : Stringsalary : int
IncreaseSalary(percent : int)DecreaseSalary(percent : int)
Methods with parameters
Instance variables
(note the data type comes after the name in the class diagram!)
Sections of a class• We can consider a class as containing two distinct
sections• The interface is the details that the programmer of
another class needs to know in order to use the class• The implementation is the section that defines how
the class actually works• A programmer using the class has no need to know
about the implementation• In fact it is better that they don’t know (or make use
of) the details of the implementation• Information hiding makes use of this separation
Access modifiers• C# provides two keywords to enable information hiding• We apply these to the methods and instance variables of the
class• We use public to identify the interface of the class• We use private to identify the implementation of the class• These keywords are known as access modifiers• In good O-O programming
– Instance variables should be private– Methods which are called from outside the class are public– Methods which are only used within the class (helper methods) are
private
Access Modifiers on a class diagram
Employee
- name : String- department : String- salary : int
+ IncreaseSalary(percent : int)+ DecreaseSalary(percent : int)
+ denotes public
- denotes private
(note the data type comes after the name in the class diagram!)
Accessor/Mutator methods• In CET101 we gained access to an instance variable from outside the class
using two special methods• An accessor method or “getter” returned the value of an instance variable
public String getName(){ return name;}
• A mutator method or “setter” modified the value of an instance variablepublic String setName(String pName){ name = pName;}
An alternative approach in C#
• Accessor (getter) and mutator (setter) methods can be replaced with an alternative called Properties
• Properties are defined with a special code syntax for getting/setting the instance variable
• Properties often have the same name as the instance variable but with an upper-case first letter
• Our convention will be to remove the m_ and the data type character from instance variables (as used in CET101) and use case to distinguish between the instance variable and the property
• For example the property associated with instance variable name will be called Name
Defining a propertyprivate String name;
public String Name{ get { return name; } set { name = value; }}
Instance variable
Property
Obtain value from instance variable
Assign value to instance variable
Auto-implemented properties
• Any number of statements can be included in the get/set accessors but often they simply return or assign a value
• C# 3.0 introduced a more concise format for properties
private String name;public String Name {get; set};
• The properties implementation is created automatically with the assumptions– The set accessor simply assigns a value to the appropriate field– The get accessor simply returns the value of the appropriate field
Creating objects and using properties
Employee emp1 = new Employee();
emp1.Name = "Liz Gandy";
Console.WriteLine("Employee name:” + emp1.Name);
Create object
Assign value to instance variable via property
Obtain value from instance variable via property
Calling Methods
• To call a method we link it to the reference name using the “dot” notation:emp1.IncreaseSalary();
• If the method requires parameters we place these in the brackets (as we would for functions):emp1.IncreaseSalary(10);
• If the method requires multiple parameters then we separate them with commasemp1.IncreaseSalary(10, "monthly");
Constructors
• Constructors are special methods which are called when an object of the class is first created (using new)
• They are always defined as public and have the same name as the class
• They should not return a value• They may require parameters which are used to initialise
instance variables or they may initialise them to default values• Classes can contain multiple constructors with different
parameter sets• The programmer can then choose whichever is most
appropriate for their needs
Employee Class Constructors//constructor with no parameterspublic Employee(){ name = "No name"; department = "Not set"; salary = 0;}
//constructor with parameterspublic Employee(String name, String dept, int salary){ this.name = name; this.department = dept; this.salary = salary; }
Note the use of this to distinguish instance variable from parameter
Creating objects using constructors
• Using the constructor with no parameters: Employee emp1 = new Employee();
• Using the constructor with parametersEmployee emp2 = new Employee("Jane Smith", "Accounts", 21500);
Demonstration
EmployeeClass.sln
Designing multiple class systems
• We have seen that object oriented systems model the real world
• Systems in the real world normally contain many entities inter-linked with each other
• In an OO model these entities will be represented by classes
• A real OO system will therefore comprise multiple classes
• These classes will have “relationships” between them
The Bank/Employee System
• We will now incorporate our Employee class into a banking/employee system
• A bank will contain branches, each located in a particular town or city
• Each branch will employ staff• These employees will be assigned to a particular
department and receive a particular salary• Depending on the banks profits and employee
performance their salaries may increase or decrease each year
The Branch class
Branch
- location : String- employeeCount : int
+ AddEmployee(employee : Employee)+ GetEmployee(index : int) : Employee
Relationships• We need to identify the
relationships in our system• The Branch “uses”
Employee(s)• We can represent this on a
simplified class diagram• The arrow indicates the
relationship between the classes
Branch
Employee
References• Relationships are represented by a special type of variable• We call this a reference to another class• If ClassA uses ClassB then we provide a reference in ClassA• References are declared just like other variables• The data type of the reference variable will be the name of
the class it refers to • The Branch class requires a reference to the Employee class• In this case because multiple employees may work for a
branch we need the reference to be an arrayEmployee [] employeeList;
• We will use employeeList to store Employee objects and access the methods of the Employee class
The full Class Diagram
Branch
- location : String- employeeCount : int
+ AddEmployee(employee : Employee)+ GetEmployee(index : int) : Employee
Reference is placed here
Employee
- name : String- department : String- salary : int
+ Employee(name : String, dept : String, salary : int)+ IncreaseSalary(percent : int)+ DecreaseSalary(percent : int)
employeeList[ ]
Note: parameterised constructors are normally shown on the class diagram
Coding the Branch class
• The reference is declared like any other instance variableprivate Employee[] employeeList;private String location;private int employeeCount;
• The actual object is created in the constructorpublic Branch(String location){ employeeList = new Employee[MAXEMPLOYEES]; this.location = location; employeeCount = 0;}
Adding new employees• Objects can be passed into methods as parameters and used like any
other variable/parameterpublic bool AddEmployee(Employee employee){ if (employeeCount < MAXEMPLOYEES) { employeeList[employeeCount] = employee; employeeCount++; return true; } else { return false; }}
Because the employee list is an array we need make sure we don’t overflow it’s size!
Returning a chosen employee
• Methods can also return values which are objects public Employee GetEmployee(int index){ if (index < employeeCount) { return employeeList[index]; } else { return null; }}
Demonstration
Bank.sln