Docstoc

oops-in-c_

Document Sample
oops-in-c_ Powered By Docstoc
					Benefits of Object-Oriented Programming?
Structured Design Tendencies Object-Oriented Tendencies
Object-Centered

1 2 3 4 5

Process-Centered

Reveals Data

Hides Data

Single Unit

Modular Units

One-Time Use Ordered Algorithm

Reusable Nonordered Algorithm

Understanding Programming Concepts (Component Based)

Accessibility and Scope
• Access modifiers are used to define the accessibility level of class members
Declaration
public private internal protected protected internal

Definition
Access not limited. It can be used in the current and other assemblies. Access limited to the containing class i.e. to all the statements of current class. Access limited to the current assembly only. Not accessible to other assemblies. Access limited to the containing class and to types derived from the containing class Access limited to the containing class, derived classes, or to members of this program

What Is Encapsulation?
• Hiding internal details
– Makes your object easy to use

• Grouping related pieces of information and processes into self-contained unit
– Makes it easy to change the way things work under the cover without changing the way users interact – Properties in C#(Component Based) Programming provide encapsulation.

What Is Inheritance?
• Inheritance specifies an is-a-kind-of relationship (UML) • Derived classes inherit properties and methods from a base class, allowing code reuse • Derived classes become more specialized
Base Class Bank Account

Saving

Current

Derived Classes

How to Create a Derived Class

!

(contd…)

How to Create a Derived Class
" # # & $ $ %

& # $ $ %

& ''' " # $ (

$ ( '

)

" # *+,-./01 -22222'02

How to Use a Sealed Class
• You cannot derive from a sealed class • Prevents the class from being overridden or extended by third parties

(3& 44

Abstract Classes and Methods?
An abstract class is a generic base class
– Contains an abstract method that must be implemented by a derived class

• An abstract method has no implementation in the base class • Can contain non-abstract members
5 & 3 " 3

#

What Is Polymorphism?
Employee Objects RegularEmp Method Called Behavior
Salary for Regular Emp

CalcSal ()

HourlyPaidEmp

CalcSal ()

Salary for Hourly Paid Emp

ContractBasedEmp

CalcSal ()

Salary for Contracted Emp

How to Write Virtual Methods
• Virtual methods are used to define base # class methods that you expect to be & ' 6 75 overridden in derived classes Base Class Derived Class
7
Animal Virtual Method Eat () Cat Override Method

&

8

& What Happens '
Eat Something

# 6 75 What Happens
Eat Mouse

Eat ()

7

How to Use Base Class Members from a Derived Class
• The base keyword is used to call a method in the base class from a derived class
& # '5 & 7 ' 6 75 8 # 5


				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:33
posted:9/29/2009
language:English
pages:14