Classes and objects object oriented programming

areebakanwal12 9 views 25 slides Oct 08, 2024
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

Oop


Slide Content

1
Lesson 8 — Classes and Objects
Microsoft Visual
Basic .NET, Introduction
to Programming

2
Objectives
Use the Object Browser to explore classes
and objects.
Design and create classes.
Use class modules to declare and use
objects.
Add methods, events, and properties to
classes.

3
Vocabulary
Business rules
ByVal
Data encapsulation
data hiding
Inheritance
Object-oriented
programming (OOP)
Operator overloading
Platforms
Polymorphism
Reusable code
StrConv()

4
Maximum Effect with Minimum Effort
Every programmer wants to be more productive.
"Maximum effect with minimum effort" is an old
saying endorsed by every programmer.
Programmers want a programming environment that
makes it easy to write code. Programmers want their
programs to work on several different platforms.
Working on different platforms means writing
programs that work on different combinations of
hardware and operating systems.
Programmers want to write reusable code, which is
code you can use in more than one program.

5
Object-Oriented Programming (OOP )
The foundation of OOP is data — data and
the operations performed on the data.
An object contains both data and the
operations that can be performed on the
data. For example, a list box is an object. It
contains the data in its Items collection and
the operations that can be performed on the
data. The operations are called methods.

6
Data Encapsulation
Data encapsulation means hiding information from the
user and the programmer. How does a list box store
information in memory? What code does it use to sort its
contents?
In OOP, these questions are not concerns of the
programmer. To communicate with an object, a
programmer writes code that sends messages to the
objects. The messages instruct the objects what
operations to perform on the data they contain.
Communication is restricted to the messages, making the
internal working of the object hidden from the user and the
programmer.

7
Data Hiding
Data hiding allows a programmer to worry
about problem solving rather than about
writing code to handle the details of storing
and operating on data. Objects take care of
the details for you.

8
Inheritance
When you create a new class based on a simpler
existing class, the new class inherits all the
properties of the original class, a principle called
inheritance.
In this new class, you can replace or extend the
properties of the original class. The new class may
add new methods and properties to the
functionality of the old class while retaining all the
original class's functionality.

9
Polymorphism
Polymorphism is redefining methods,
properties, and operations to behave
differently with different objects.

10
Operator Overloading
The lowly plus symbol (+) has many functions:
It adds integers, decimals, and doubles, and it
joins strings together. Each operation calls for
different code to execute the operation within
the context in which it occurs.
Operator overloading is the term that refers to
the ability of the existing operators to have
more than one function depending on the
context.

11
The Object Browser
The Object Browser lets you look at classes
and the properties of the objects created
from the classes.

12
The Object Browser

13
The Find Symbol Dialog Box

14
Creating a Class
The simplest classes are a lot like structures. You use
structures to create new data types. You can use
classes the same way, but more often, you use classes
to create objects of user-defined data types along with
the operations you need to process, store, or display the
data.
The simplest way to define a property in a class is a
simple variable declaration. A Public variable
declaration, like Public Name As String, in the class
definition provides a Name property of type String for
any object created with that class definition.

15
Property Procedure
Property Name As DataType
Get
Code called when the property is read
End Get
Set
Code called when the data are written to the
property
End Set
End Property

16
Classes
Defining public variables in a Class module is
equivalent to adding properties to the objects
created with the class. Adding Public
procedures to a Class module is equivalent
to adding methods to the objects created with
the class.

17
Note
Remember, a method is an action that an
object can perform. For example, the list box
object can execute the Clear method on its
Items collection to clear its contents, or it can
execute the Add method to add a new entry
to the Items collection of the list box.

18
Computer Ethics
Building business rules into class definitions
makes it easier to enforce those rules. If
every programmer in the shop is required to
use the same unmodified class definitions, all
are compelled to use the same business
rules.

19
Adding an Event
First, you must declare an event in the Class
definition.
Then you must write code in the Class
definition to raise the event.
Lastly, in the calling program, you must
declare object variables using the
WithEvents keyword and write an event
procedure to respond to the event when it
occurs.

20
ByVal Keyword
The ByVal keyword in a parameter list sends a
parameter's value to the function. Because the
function does not know the parameter's address, it
cannot change the actual value of the parameter.
The value of the parameter can be used in the
function. The program can change the value of the
variable that represents the parameter, but it cannot
change the actual value of the parameter as defined
in the calling program. The ByRef keyword passes
the parameter by address. This means the program
can make permanent changes to the parameter sent
to the procedure.

21
Summary
Programmers want better programming environments, cross-
platform applications, and reusable code modules.
Visual Basic is the root language of many Microsoft products,
including Microsoft Office.
Object-oriented programming (OOP) focuses on creating
objects that include data and the operations performed on the
data.
Data encapsulation, part of the OOP paradigm, is the ability of
an object to hide data and methods from the user or even the
programmer. This protects data from accidental alteration.
Inheritance is the ability of an OOP class to inherit properties
and methods from other classes.

22
Summary
Polymorphism is the ability to define multiple uses for the same
operators and methods.
You create objects from the templates provided by classes.
The Object Browser lets the programmer look at classes and
the properties of the object created from the classes.
You add class definitions to a project by selecting Project | Add
Class from the menu bar.
Public variables declared in the Declarations section of the
class definition become properties of objects created with the
new class.

23
Summary
Private variables declared in the Declarations section of a class
definition are available within the definition but cannot be
accessed outside the definition.
The program uses Property procedures to gather and provide
properties to objects. The program also uses the procedures to
modify or process the properties.
The StrConv( ) function can be used to convert an input string
to a string where the first character is capitalized and the other
characters are lowercase.
A new object created from a class is an instance, or an
instantiation, of the class.
You can save objects in text files by saving each part of the
object as text in the file.

24
Summary
Public procedures and functions recorded in a class definition
become the methods of the objects created with the class.
A business rule is a rule of the workplace written into the code
of a program. When put into class definitions, business rules
are easy to manage and implement.
The SelectedIndex property of a list box contains the index
value of the item currently selected in the list.
To add an event to a class module, first declare the event in the
Declarations section of the class definition, then write code to
raise the event. The code initiates the event when some
condition is satisfied (or not satisfied) in the class definition.

25
Summary
To use event definitions in a class definition, use the
WithEvents keyword in an application to declare the objects of
that class.
The ByVal keyword in a parameter list sends a parameter's
value to a function, not the parameter's address.
The program uses the RaiseEvent statement in a Class
definition to fire an event procedure.
Class definitions are at their best when used to hide the details
of program operations from a programmer and provide the
programmer with an easy-to-use interface to the data in the
objects created from the class definition.
Tags