Software Development Techniques Topic 10 Objects and Classes

Software Development Techniques Topic 10: Objects and Classes V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 2 Scope of Topic • We have explored the topic of modularity in our programs when we discussed functions. – We needed these in order to make our increasingly complex programs more manageable. • In this lecture, we are going to discuss a second kind of modularity. – Objects and classes. • Languages like Java and C# are object-oriented languages. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 3 Our Pseudocode • The pseudocode we have been developing thus far has been part of one program. – The program may have had different functions, but it was still one program. • There are several problems that come from this approach when applied to real code. – Functions are difficult to re-use. – It is hard to pass around large amounts of data. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 4 Data Representation - 1 • Consider the following scenario – You have to store the details about twenty students. • Name • Address • Matriculation number • Grades over ten modules • How can you represent all of this in a program? – Three 1 dimensional arrays • Name, Address, Matriculation Numbers – One two dimensional array • Grades V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 5 Data Representation - 2 data data names as array (20) of string ages as array (20) of whole number matric as array (20) of string grades as array (20)(10) of whole number which as whole number output "Which student do you want to access? " input which; output "Student name is " + names[which] output "Student age is " + ages[which] output "Student matriculation is " + matric[which] V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 6 Data Representation - 3 • All sorts of problems here. – It is difficult to manipulate • How do you sort this kind of data representation? • How do you add elements in the middle? – It is difficult to extend • What happens if you suddenly need to include student addresses? – It is difficult to pass around • How do you write a function that uses all this data? – You need to pass every bit of it into every function. – It is not maintainable. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 7 In an Ideal World. . . • In an ideal world, we would be able to create a data type of our own. – Like an array, but with compartments of different types. – One where we can give the compartments meaningful names instead of numbers. • Luckily, in modern object-oriented languages, we can do just that. – We define a class that acts as a data type of its own. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 8 Objects and Classes • Classes are more powerful than arrays because they have both variables and functions. – They provide a portable package of data and algorithms. • They also let us give the compartments different names. – And they can be of any combination of types, including arrays. • The class can then be used like a whole number or a string when declaring variables. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 9 A Class in Pseudocode Class Student data name as string data age as whole number data matric as string dim grades as array (10) of whole number End Class • Here, a class looks similar to a function. The main difference at this point for the pseudocode is the syntax we use (Class instead of Function). V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 10 Objects • Objects are a specific instantiation of a class. – A class is the data type – The object would then be the variable created from the class. • Objects are a reference data type, and so when they are first declared they contain null. – Like a string • When we want to put a new object in our variable, we use the special syntax new. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 11 Declaring and Manipulating an object data my. Student as Student my. Student = new Student my. Student->name = "Michael" my. Student->age = 21 my. Student->matriculation = "9876543210" output "My Student's Name is " output my. Student->name V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 12 Objects • The new keyword lets us set up an empty object. – It follows the same rules for default values as a normal program. • Whole numbers get 0 • The string gets null • The array gets null • We can also use the new keyword to create a new array or new string. – We do not have to limit this to the return of functions. • grades = new array (10) of whole number V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 13 Desk-Checking an Object • As with a function, a desk-check of an object should be conducted separately from the rest of the program. • Ideally, you will use separate sheets of paper for each object. – And each function within an object. • The main difference is that the data contained within an object persists while the program exists. – You do not lose it between calls. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 14 Desk-Checking an Object • As such, you should summarise the content of each object separately from the rest of your function. my. Student – class Student V 1. 0 Name “Michael” Age 21 Matric “ 9876543210” Grades null © NCC Education Limited

Objects and Classes Topic 10 - 10. 15 Desk-Checking an Object • When you call a function, the variables are set up again from scratch. – So every time, variables local to the function get set to their default values. • An object only gets set to its default values when the new keyword is used. – That is when the object is actually created. • The values in an object then are persistent. – And so you need to know what they are at all times. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 16 Object Persistence • Objects persist as long as they are in scope. – If they are declared within a function, they exist until the end of the function. – If they are declared within the main body of a program, they persist until you get to the end of that function. • When they fall out of scope, they are destroyed. – They revert to being null. • Persistence thus is contextual on where the object is declared. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 17 Classes and Methods • Classes can have functions within them. – These are known as methods in object-oriented talk. • These allow you to package the manipulation of your data along with the data itself. • The syntax for declaring a function in a class is identical to what you have done before. – The only distinction is that it is done within the structure for declaring a class. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 18 The Student Class Student data name as string data age as whole number data matric as string dim grades as array (10) of whole number Function increase. Age() returns whole number age = age + 1 return age; End Function End Class V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 19 Calling A Method data my. Student as Student my. Student = new Student my. Student->name = "Michael" my. Student->age = 21 my. Student->matriculation = "9876543210" output "My student's age before is " output my. Student->age call my. Student->increase. Age() output "My student's age before is " output my. Student->age V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 20 Passing Objects • One of the benefits that comes from using objects is that we can easily pass around whole packages of data. – And have them properly relate to each other • This is done much as you would expect from the syntax we have already seen. – We just use the class as the data type in the parameter list and return type. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 21 Passing and Returning Function Setup. Class (needs my. Student as Student) returns Student my. Student = new Student output "Please enter the student's name" input my. Student->name output "Please enter the student's age" input my. Student->age output "Please enter the student's Matriculation" input my. Student->matric my. Student->grades = new array (10) of whole numbers return my. Student V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 22 Objects Using Objects • Part of the power of objects is that they allow for other objects to be part of their makeup. – Obviously, it becomes harder and harder to deskcheck as this becomes the case. • We are currently using an array of whole numbers to store our grade. – But that does no let us know what module to which a grade belongs. • We can fix that now. . . V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 23 Objects Using Objects Class Student data name as string data age as whole number data matric as string dim grades as array (10) of Module. Grade End Class Module. Grade data name as string data grade as whole number End Class V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 24 The New Keyword • The new keyword can be used to do more than create an object. – It also causes a function to be called on the newly formed object. – That function is called a constructor. • We can use this to make sure that our objects are setup, from the start, with sensible defaults. • This function is always called the same thing as the name of the class. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 25 Constructor Methods • Constructor methods allow us to change what the default values of a newly constructed object will be. – We should make sure all the objects are initialised – We should make sure all the arrays are initialised. • Every object should have a constructor to make sure that the work of other people is reduced when they use your objects. – The more work the object does, the less other people have to do. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 26 A Constructor Method Class Module. Grade data name as string data grade as whole number Function Module. Grade name = "Unknown" grade = -1 End Function End Class V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 27 A Second Constructor Method Class Student data name as string data age as whole number data matric as string dim grades as array of Module. Grade Function Student (needs n as string, a as whole number, m as string, int g) name = n age = a matric = m grades = new array (g) of whole number End Function End Class V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 28 Using New With Parameters Function Setup. Class (needs my. Student as Student) returns Student my. Student = new Student ("Michael", 21, "9876543210", 10) return my. Student } V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 29 Method Overloading • We can provide many different versions of a constructor function. – As long as they have different method signatures. • A method signature is made up of the name of the method along with the type and order of parameters. – Two functions with the same name have different signatures if they have different parameter lists. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 30 Method Overloading • When it comes time to run a constructor, we look for the one that matches the method signature of the parameters we have. – If we have New Student (“Michael”, 20), we look for a constructor that needs a string and a whole number (in that order) – If we have New Student (20, “Michael”) we look for one that needs a whole number and a string. • If none match, we record an error. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 31 Constructor Methods • We do not need to provide a constructor method. – The default case is that we just set up starting values according to the usual rules. – However if we provide any constructor for our object, we do not get the empty constructor any more. • We would need to explicitly add it in. • This is because sometimes we use a constructor to force an object to be set up with starting values. – And we cannot do that if a default constructor is always available. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 32 Object Design - 1 • Objects are tremendously powerful. – More powerful than we have time to properly explore. • It is also very easy to let them make the program more complicated than it should be. • We need to have some discipline to make sure that our objects enhance our programs rather than detract from them. – There are some informal guidelines for this. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 33 Object Design - 2 • Whenever we have data that is related (such as in our student case), we use an object to link that data. • Whenever we have functions that act on the data inside a class, we place them inside that class. • If we need access to the data in an object in another function, we pass in the full object. – Not just parts of it. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 34 Object Design - 3 • The reason for this is that objects represent the best way to ensure reusability of our programs. – If we have a Student class, we can use it in any program that needs a Student without having to change anything. • However, reusability does not come for free. – We need to design our classes properly to ensure that. • Ideally too, programs will never access variables directly from a class. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 35 Accessor Functions - 1 Class Student data name as string function set. Name (needs n as sring) { name = n end function query. Name () returns string return name end function End Class V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 36 Accessor Functions - 2 • These are known as accessor functions. – Or sometimes, ‘setters’ and ‘getters’ • We should do this because it improves the maintainability of our objects. – If we want to change the way a name is represented, we can do that because all changes have to be done through our accessor functions. • It is not something to worry about too much at the moment. – Just get into the habit of doing it. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 37 Conclusion • Objects represent the next big leap in modularity for our programs. – It is important, because it is the way languages like Java and C# represent almost all their functionality. • Classes are the blueprint of an object. – Essentially, the object’s data type • Objects are the instantiation of a class. – They are the variable created from a class. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 38 Terminology • Class – A custom data type we create. • Object – A variable created from our data type • Instantiation – Creating an object from a class • Accessor – A function used to set or get a variable in a class. V 1. 0 © NCC Education Limited

Objects and Classes Topic 10 - 10. 39 Topic 10 – Objects and Classes Any Questions? V 1. 0 © NCC Education Limited
- Slides: 39