Top Banner
Topic 10:Objects & Classes Software Development Techniques
40

SDT Topic-10: Objects and Classes

Jul 15, 2015

Download

Education

Pradip Kharbuja
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SDT Topic-10: Objects and Classes

Topic 10:Objects & ClassesSoftware Development Techniques

Page 2: SDT Topic-10: Objects and Classes

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.

• It is hard to pass around large amounts of data.

• Functions are difficult to re-use.

Page 3: SDT Topic-10: Objects and Classes

Data Representation

• Consider the following scenario.

• You have to store the details about twenty students.

• Name

• Address

• Grades of ten modules

• How can you represent all of this in a program?

Page 4: SDT Topic-10: Objects and Classes

Data Representation

• How can you represent all of this in a program?

• Two 1 dimensional arrays

• Name, Address

• One 2 dimensional array

• Grades

Page 5: SDT Topic-10: Objects and Classes

Data Representation

Page 6: SDT Topic-10: Objects and Classes

Problem in Previous PseudoCode

• 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 age?

• 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.

• In short, it’s a bad solution.

Page 7: SDT Topic-10: Objects and Classes

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 languages, we can do just that.

• In Structured languages like C, we have something like a structure.

• In object-oriented languages like C++, Java, C#, we have the more versatile class that acts as a data type of its own.

Page 8: SDT Topic-10: Objects and Classes

Classes

• Class is similar to data type like int, float, etc.

• 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.

Page 9: SDT Topic-10: Objects and Classes

A Class in Pseudocode

• Here, a class looks similar to a function.

• The main difference is we use class instead of function.

• The variables declared inside class are known as data members or attributes or properties

Page 10: SDT Topic-10: Objects and Classes

Objects

• Objects are a specific instantiation of a class.

• Instantiation is a fancy way of saying “creating an object from a class”.

• A class is the data type or the blue print of an object.

• The object would then be the variable created from the class.

• When we want to put a new object in our variable, we use the special keyword new.

Page 11: SDT Topic-10: Objects and Classes

Declaring and Manipulating an object

Page 12: SDT Topic-10: Objects and Classes

Objects

• Objects are a reference data type, and so when they are first declared they contain null.

• Like a string or array

• 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

Page 13: SDT Topic-10: Objects and Classes

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.

Page 14: SDT Topic-10: Objects and Classes

Classes and Methods

• Classes can have functions within them.

These are known as methods in object-oriented language.

• Methods allow you to manipulate the data of the class.

• 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.

Page 15: SDT Topic-10: Objects and Classes

The Student Class

Page 16: SDT Topic-10: Objects and Classes

Calling a Method

Page 17: SDT Topic-10: Objects and Classes

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.

Page 18: SDT Topic-10: Objects and Classes

Passing and Returning

Page 19: SDT Topic-10: Objects and Classes

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 desk-check 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...

Page 20: SDT Topic-10: Objects and Classes

Objects Using Objects

Page 21: SDT Topic-10: Objects and Classes

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.

• The constructor is the special method which has the name same as the name of the class. It will not have return type not even a void. It is called automatically during the object declaration.

Page 22: SDT Topic-10: Objects and Classes

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 initialized.

We should make sure all the arrays are initialized.

Page 23: SDT Topic-10: Objects and Classes

A Second Constructor Method (Parameterized)

Page 24: SDT Topic-10: Objects and Classes

Calling the Parameterized constructor

Page 25: SDT Topic-10: Objects and Classes

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.

Page 26: SDT Topic-10: Objects and Classes

Method Overloading in Constructor

Page 27: SDT Topic-10: Objects and Classes

Method Overloading in Constructor [Contd.]

Page 28: SDT Topic-10: Objects and Classes

Object Design

• There are some informal guidelines to enhance our programs using objects.

1. Whenever we have functions that act on the data inside a class, we place them inside that class.

2. If we need access to the data in an object in another function, we pass in the full object. Not just parts of it.

Page 29: SDT Topic-10: Objects and Classes

Object Design [Contd.]

• The objects represent the best way to ensure reusability of our programs.

• However, reusability does not come for free.

• We need to design our classes properly to ensure that.

• Ideally, programs will never access variables(properties) directly from a class.

Page 30: SDT Topic-10: Objects and Classes

Accessor Functions (Getter and Setter)

Page 31: SDT Topic-10: Objects and Classes

Accessor Functions (Getter and Setter)

• 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.

Page 32: SDT Topic-10: Objects and Classes

Questions

1. Explain the difference between a Class and an Object and demonstrate using a suitable example .

2. Define what is meant by a constructor method and give an example of why you would choose to use one.

3. Explain what is meant by method overloading.

Page 33: SDT Topic-10: Objects and Classes

Question

• Give a pseudocode outline of a class called Car. Its attributes are manufacturer, model, registration number, and price.

Page 34: SDT Topic-10: Objects and Classes

Question

Page 35: SDT Topic-10: Objects and Classes

Question

Page 36: SDT Topic-10: Objects and Classes

Question

• Using the class above, add a constructor method that allows for all four of these values to be set when the object is instantiated.

Page 37: SDT Topic-10: Objects and Classes

Questions

1. Give a class definition for a dumbbell, including its weight, colour and price. Include a constructor method for the class.

2. Give a pseudocode outline of a class called Book. Its attributes are the author, title, genre and number of pages.

3. Using the class above, add two constructor methods. The first should take in the author and title values only and should initialise the other attributes to default values, and the second should take in all four values.

Page 38: SDT Topic-10: Objects and Classes

Conclusion

• 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.

Page 39: SDT Topic-10: Objects and Classes

Terminologies

• 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.

Page 40: SDT Topic-10: Objects and Classes

End of Topic 10Software Development Techniques