Download presentation
Presentation is loading. Please wait.
Published byElizabeth Riley Modified over 9 years ago
1
Object-Oriented Programming
2
Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish between a data member and a method. Define the term encapsulation. Explain the purpose of a driver program.
3
Objectives (Continued) Define the term instantiation. Explain the purpose of a constructor and a destructor. Distinguish between parameters and arguments. Define the terms overloading and overriding.
4
Objectives (Continued) Explain what is meant by inheritance. Explain what is meant by polymorphism. Distinguish between a base class and a derived class.
5
Introduction Traditional procedure-oriented program focus on what is being done. Increasing complexity makes these programs more difficult to understand. Modern programming is largely GUI- oriented. Difficult to design procedure-oriented programs to handle multiple windows.
6
Object-Oriented Programming Methodology which bases application design around the data and operations on the data. Characteristics of data defined in abstract form. One program may include many windows. Window abstraction implemented by means of a construct called a class.
7
Classes Object-oriented design based on organizing program around a collection of classes. A class is a template from which objects are created. Class definition contains both data (what object looks like) and behavior (what it does).
8
Classes (Continued) Data can be referred to as properties, data members (of the class), or instance variables. Behaviors can be referred to as methods or member functions.
9
Grade Book Class Defined by program designer. Made up of five data members: Name, Grade1, Grade2, Grade3, and Average. Could also contain a method to input student name and grades, one to compute the student’s average, and one to output the student’s name and average.
10
Grade Book Class Abstraction Class Name Data Members Methods GradeBook Name Grade1 Grade2 Grade3 Average GetStudent ComputeAverage ShowStudent
11
Implementation 1 Class GradeBook private Name private Grade1 private Grade2 private Grade3 private Average public GetStudent Input Name, Grade1, Grade2, Grade3 End public ComputeAverage Average = (Grade1 + Grade2 + Grade3) / 3 End public ShowStudent Display Name, Average End End Class
12
GradeBook Class (Continued) Members and methods are encapsulated within the class - meaning the data can only be accessed and manipulated using one of the methods defined in the class. Data members defined as private cannot be accessed outside the class. Methods are public, meaning they can be accessed from outside the class.
13
Use of Class Once defined, a class can be used when building a program. Program begins by creating an instance of this class, called an object. In this example the programmer might create two GradeBooks - two objects belonging to the GradeBook class.
14
Driver Program Creates and interfaces with objects. Gets things going - “drives” them. Creates and manipulates objects of a particular class. Creating an instance of an object is called instantiation.
15
GradeBook Class Driver Start GradeBook: GradeBook1 GradeBook: GradeBook2 GradeBook1.GetStudent GradeBook2.GetStudent GradeBook1.ComputeAverage GradeBook2.ComputeAverage GradeBook1.ShowStudent GradeBook2.ShowStudent Stop
16
Constructors Memory is allocated automatically when an object is created, by a special program called a constructor. Default constructor called at object creation time does not necessarily initialize any of the data members. This could cause problems in computations if methods invoked incorrectly.
17
Build Your Own Constructor We can create our own constructor to prevent this problem. This constructor is simply another method in the class except it has the same name as the class. Matching name identifies method as a constructor. Example follows:
18
GradeBook Constructor public GradeBook Name = “Any Student” Grade1 = 0 Grade2 = 0 Grade3 = 0 Average = 0 End Not very interesting, is it?
19
Constructor with Parameters public GradeBook(AnyName,AnyGr1,AnyGr2,AnyGr3) Name = AnyName Grade1 = AnyGr1 Grade2 = AnyGr2 Grade3 = AnyGr3 Average = 0 End
20
Use of Parameters This constructor makes use of a list of parameters in parentheses after the constructor name. Parameters are a list of names which act as placeholders. Modify the driver program so that when a new object is created, the driver program provides initial values for the first four data members in the list.
21
Example The statement: GradeBook: AnotherBook(“Sally”,100,90,80) causes a new object called AnotherBook to be created. When the constructor is executed, the four values in parentheses (arguments) are assigned to the four variables in the constructor parameter list: AnyName = “Sally” AnyGr1 = 100 AnyGr2 = 90 AnyGr3 = 80
22
Example (Continued) This kind of constructor provides more flexibility - now the values of the first four data members can be set when the object is created. We can still use the GetStudent method to input data values during program execution.
23
Overloading See Figure 10-7 on page 235. Note there are two constructors (same name). Which one do we use? It depends … … on the number or type of arguments used when the method is invoked. This is called overloading.
24
Destructors Just as constructors are used when an object is created, destructors are used when an object is destroyed. Default one used if none specified - deallocates memory used by object. We can write our own but they are not commonly used. You only need to know what they are.
25
Inheritance Object-oriented design offers the power of class reusability - classes can be saved in a class library and reused when needed - saves “reinventing the wheel.” May be cases when a saved class is close to, but not quite what we need. We can create a subclass based on the original one.
26
Inheritance (Continued) A subclass contains all data members and methods of the original class, plus any additional members and methods the programmer includes. This means by which one class acquires the data and methods of another is called inheritance. See Figure 10-10 on page 238 for an example of inheritance.
27
Base and Derived Classes Refer to Figure 10-10 again. Employee class is the base class (or parent class or super class) containing data and methods that apply to all employees. Faculty and Staff classes are derived from the Employee base class - they are derived classes (also called a child class or subclass).
28
Base and Derived Classes (Continued) Each class has a constructor and several other methods. All three classes have two methods called GetEmployee and ShowEmployee. A subclass can reimplement or redesign any method from a base class - this is called overriding.
29
Base and Derived Classes (Continued) Methods not re-implemented in a subclass will be reused (same as they are in the parent class). Both subclasses include a ComputePay method not inherited from the parent class. ComputePay is implemented differently for each class. This is an example of polymorphism.
30
Driver Program - Employee Example Start Faculty: Faculty1 Staff: Staff1 Faculty1.GetEmployee Faculty1.ComputePay Faculty1.ShowEmployee Staff1.GetEmployee Staff1.ComputePay Staff1:ShowEmployee Faculty1.ZapEmployee Stop
31
Object-Oriented Programming Benefits Object-oriented programs are easier to maintain: –Class definition changes are picked up by all programs that use an object of that class. –No need to find and change all occurrences of class.
32
Benefits (Continued) Object-oriented programming leads to higher productivity: –Class libraries save much work. –Programmer need not concentrate on technical detail but on business requirement. –Objects can be defined in easy-to-use scripting languages.
33
Benefits (Continued) Object-oriented programming facilitates design and code reuse: –Class can be reused even if it is not a perfect fit. –Programmer can use overriding to tailor a previously-defined class to his or her needs.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.