sdt topic-10: objects and classes

40
Topic 10:Objects & Classes Software Development Techniques

Upload: pradip-kharbuja

Post on 15-Jul-2015

802 views

Category:

Education


1 download

TRANSCRIPT

Topic 10:Objects & ClassesSoftware Development Techniques

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.

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?

Data Representation

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

• Two 1 dimensional arrays

• Name, Address

• One 2 dimensional array

• Grades

Data Representation

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.

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.

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.

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

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.

Declaring and Manipulating an object

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

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.

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.

The Student Class

Calling a Method

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.

Passing and Returning

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

Objects Using Objects

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.

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.

A Second Constructor Method (Parameterized)

Calling the Parameterized constructor

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.

Method Overloading in Constructor

Method Overloading in Constructor [Contd.]

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.

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.

Accessor Functions (Getter and Setter)

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.

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.

Question

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

Question

Question

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.

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.

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.

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.

End of Topic 10Software Development Techniques