total oop in c# dot net

Post on 12-Apr-2017

263 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

PROPAGATING IN .NET

By Muhammad Naveed MCS University of AJK, DIT

Instructor Moon Creations School of IT

1

OOP

Function +data In modular programming functions and data is

separated. i.e. student { Student info } Courses { Course info. }

2 10/13/2012 Muhammad Naveed

OOP

So every thing is separated data and operations .

Class is an instance of an object, which defines state and behavior of an object.

Object can interacts with each other by message passing.

3 10/13/2012 Muhammad Naveed

Class

To house (among other things)

In class we provide one or more methods that are designed to perform the class tasks.

For example: a bank account class might contain one or more methods to deposit money, withdraw money and balance inquiry.

Collection of similar objects, with different names

Class in an passive entity.

Class in template or blue print

Class is module or sketch.

Class has data members and functions on which class performs its functionalities.

Data and functions are combined

4 10/13/2012 Muhammad Naveed

Object

We have to build a object of class to actually

gets its fruits. C# called OOP

Object is an active entity.

Object has properties or methods/ attitudes/ functions/operations/data members/fields

So objects has state and behaviors

Students is an object.

Employee is an object

5 10/13/2012 Muhammad Naveed

Methods

Performing a task in an application requires a method. The method describes the mechanism that actually perform its task. The method hides from its user the complex tasks. Just as the accelerator pedal of a car hides from the driver the complex mechanism of making the car go faster.

6 10/13/2012 Muhammad Naveed

Attributes

Every objects muse have some attributes or properties like color, size, shape etc.

Attributes are specified by the class’ instance variables

Properties Get assessors and set assessors

Attributes are not necessarily accessible directly. The car manufacturer does not want drivers to take apart the car’s engine to observe the amount of gas in its tank.

Instead the driver can check the fuel gauge on the dashboard.

So in c# we use get accessors for reading and set accessors for storing values.

7 10/13/2012 Muhammad Naveed

STATIC Methods

Sometimes a method performs a task that does not depend on the contents of any object. So can be called with out creating its object this is called static method.

Syntax

ClassName. MethodName(arguments)

Always called with class name and method name

8 10/13/2012 Muhammad Naveed

9 10/13/2012 Muhammad Naveed

Constructor

Constructor is used to initialize the object of the class.

Behaves like Function

Constructor is just like function, but it is a special type of function.

Same name like class Class student

Public student( )

Defaults constructor has no argument or parameters

10 10/13/2012 Muhammad Naveed

Properties Function

These are get and set functions

Not a fully function but behaves like function.

Used to get and set values of private data members of a class

11 10/13/2012 Muhammad Naveed

int n;

Console.WriteLine("Enter a number");

n = Convert.ToInt32(Console.ReadLine());

if (n > 0)

{

Console.WriteLine("The Number is +ve");

}

else

{

Console.WriteLine("The number is -ve");

}

Console.Read();

int n;

n = 1;

while (n <= 5)

{

Console.WriteLine("Kashmir");

n++;

}

Console.Read();

12 10/13/2012 Muhammad Naveed

Major Concepts

OOP

Classes

Objects

Methods

Properties

Instance Variables

13 10/13/2012 Muhammad Naveed

14 10/13/2012 Muhammad Naveed

15 10/13/2012 Muhammad Naveed

Features of OOP

Inheritance

Polymorphism

Encapsulation

Abstraction

16 10/13/2012 Muhammad Naveed

STRUCTURED VS. OO PROGRAMMING

17

STRUCTURED PROGRAMMING:

10/13/2012 Muhammad Naveed

Structured Programming

Using function

Function & program is divided into modules

Every module has its own data and function which can be called by other modules.

18 10/13/2012 Muhammad Naveed

OBJECT ORIENTED PROGRAMMING

19 10/13/2012 Muhammad Naveed

OBJECT ORIENTED PROGRAMMING

Objects have both data and methods Objects of the same class have the same data elements

and methods Objects send and receive messages to invoke actions

Key idea in object-oriented:

The real world can be accurately described as a collection of

objects that interact.

20 10/13/2012 Muhammad Naveed

Basic terminology

object - usually a person, place or thing (a noun)

method - an action performed by an object (a verb)

attribute - description of objects in a class

class - a category of similar objects (such as automobiles) - does not hold any values of the object’s attributes

21 10/13/2012 Muhammad Naveed

Example for attributes and methods

Attributes: manufacturer’s name model name year made color number of doors size of engine etc.

Methods: Define data items

(specify manufacturer’s name, model, year, etc.)

Change a data item (color, engine, etc.)

Display data items Calculate cost etc.

22 10/13/2012 Muhammad Naveed

WHY OOP?

Save development time (and cost) by reusing code

once an object class is created it can be used in other applications

Easier debugging

classes can be tested independently

reused objects have already been tested

23 10/13/2012 Muhammad Naveed

DESIGN PRINCIPLES OF OOP

Four main design principles of Object-Oriented Programming(OOP):

Encapsulation(information hiding) Abstraction(hide unnecessary details ) Polymorphism(same thing behave differently) Inheritance(derived class inherit from base class)

24 10/13/2012 Muhammad Naveed

ENCAPSULATION

Also known as data hiding Only object’s methods can modify information in the

object. Data and Function Encapsulated within a single

package ‘class’. Analogy:

ATM machine can only update accounts of one person or object only.

25 10/13/2012 Muhammad Naveed

Abstraction

Focus only on the important facts about the problem at hand

to design, produce, and describe so that it can be easily used without knowing the details of how it works.

Analogy: When you drive a car, you don’t have to know how

the gasoline and air are mixed and ignited. Instead you only have to know how to use the

controls. Draw map

26 10/13/2012 Muhammad Naveed

POLYMORPHISM

the same word or phrase can mean different things in different contexts Analogy:

In English, bank can mean side of a river or a place to put money

move -

27 10/13/2012 Muhammad Naveed

Function Overloading

The operation of one function depends on the

argument passed to it.

Example: Fly(), Fly(low), Fly(150)

28 10/13/2012 Muhammad Naveed

INHERITANCE

Inheritance—a way of organizing classes Term comes from inheritance of traits like eye color,

hair color, and so on. Classes with properties in common can be grouped so

that their common properties are only defined once. Superclass – inherit its attributes & methods to the

subclass(es). Subclass – can inherit all its superclass attributes &

methods besides having its own unique attributes & methods.

29 10/13/2012 Muhammad Naveed

AN INHERITANCE HIERARCHY

30

Vehicle

Automobile Motorcycle Bus

Sedan Sports Car School Bus Luxury Bus

What properties does each vehicle inherit from the types

of vehicles above it in the diagram?

Superclass

Subclasses

10/13/2012 Muhammad Naveed

OBJECT-ORIENTED PROGRAMMING

LANGUAGES

Pure OO Languages

C#, Smalltalk, Eiffel, Actor, Java

Hybrid OO Languages

C++, Objective-C, Object-Pascal

31 10/13/2012 Muhammad Naveed

Review: Introduction to Object

Orientation What are the four basic principles of object

orientation? Provide a brief description of each.

What is an Object and what is a Class? What is the difference between them?

What is an Attribute? What is an Operation? What is inheritance? What is polymorphism? Describe the strengths of object orientation.

32 10/13/2012 Muhammad Naveed

Review: Introduction to Object

Orientation

State 2 differences between functional programming and OOP.

What are the four basic principles of object orientation? Provide a brief description of each.

What is an Object and what is a Class? What is the difference between them?

What is an Attribute? What is an Operation? Describe the strengths of object orientation.

33 10/13/2012 Muhammad Naveed

Polymorphism

Compile time polymorphism(Overloading)

Run time Polymorphism( Overriding )

34 10/13/2012 Muhammad Naveed

Compile Time

Polymorphism(Overloading)

Can be Achieved by

Function Overloading

Operator Overloading

Constructor Overloading

35 10/13/2012 Muhammad Naveed

Compile Time

Polymorphism(Overloading)

Function Overloading It means same thing behaves differently Public void get() Public void get(int r) Public void get(int r, string n) Public void get(int r, string n, string rem….)

So same function behaves differently by increasing number of parameters or arguments i.e. no argument, one argument or two or may be three or even more.

So this is how we can achieve Compile time polymorphism that is overloading

Also function type or signature can be changed, so that it can be define with same name but with different parameters.

36 10/13/2012 Muhammad Naveed

Example

37 10/13/2012 Muhammad Naveed

Compile Time

Polymorphism(Overloading)

Operator Overloading

Some operators can also behaves differently called operator overloading (polymorphism). Operator can be redefined to achieve the operator overloading.

38 10/13/2012 Muhammad Naveed

Operator Overloading

39 10/13/2012 Muhammad Naveed

Example 2

40 10/13/2012 Muhammad Naveed

41 10/13/2012 Muhammad Naveed

42 10/13/2012 Muhammad Naveed

43 10/13/2012 Muhammad Naveed

44 10/13/2012 Muhammad Naveed

Compile Time

Polymorphism(Overloading)

3. Constructor Overloading

45 10/13/2012 Muhammad Naveed

46 10/13/2012 Muhammad Naveed

47 10/13/2012 Muhammad Naveed

48 10/13/2012 Muhammad Naveed

49 10/13/2012 Muhammad Naveed

Lecture

Intro to GUI using C#

Windows Applications

Windows Forms

50 10/13/2012 Muhammad Naveed

Run Time Polymorphism

(Overriding)

We have learned about compile time polymorphism which is overloading.

Function Overloading

Public void get(int n)

Operator Overloading

Public static room operaotr +(room rm1, room rm2)

Constructor Overloading

Public student(string n,…)

51 10/13/2012 Muhammad Naveed

Run Time Polymorphism

(Overriding)

Now in this case we have to change the signature or nos of parameters to get function overloading. Which we have done.

But in some situations, we have to use same function with the same name signature this is called overriding. this situation can be used in parent class relationship i.e.

Where we have to use virtual function, That needs to be redefined again and again. It gives the 1st definition of that function,

52 10/13/2012 Muhammad Naveed

Run Time Polymorphism

(Overriding)

Because same name with same signature needs to be redefined using virtual keyword in the parent class.

And to access this in the child class we have to use override keyword.

53 10/13/2012 Muhammad Naveed

Example

54 10/13/2012 Muhammad Naveed

OOP: Inheritance

Inheritance a form of software reuse.

Lets you save much of your time in software development.

The existing class from which a new class inherits is called base class, and the new class is called derived class

Each derived class can become the base class for next extended class

And new data members can be added.

10/13/2012 Muhammad Naveed 55

OOP: Inheritance

Base Class Derived Class

Student Graduate Student, Undergradulate

Shape Oval, Rectangle, elpse

Loan CarLoan, HomeLoan

BankAccount Currant, Saving etc

10/13/2012 Muhammad Naveed 56

OOP: Inheritance

10/13/2012 Muhammad Naveed 57

Community Member

Employee

Faculty

Admin Teacher

Staff

Student Alumnus

OOP: Inheritance

Protected Members

Commonly we used Public and Private

Public data members are accessible whenever the application has a reference to an object of that class or one of its derived class.

Private data members are inherited from its derived class, but are not accessible by derived class and methods.

10/13/2012 Muhammad Naveed 58

OOP: Inheritance

Protected :

Provides an intermediate level of accessibility b/w public and private. These can be accessible by members of that base class and by members of its derived class

10/13/2012 Muhammad Naveed 59

Relationship b/w base

classes and derived classes

10/13/2012 Muhammad Naveed 60

10/13/2012 Muhammad Naveed 61

10/13/2012 Muhammad Naveed 62

10/13/2012 Muhammad Naveed 63

10/13/2012 Muhammad Naveed 64

10/13/2012 Muhammad Naveed 65

The above examples show polymorphism, inheritance, constructor overloading and function overloading, private and public data members w.r.t access.

Reference Book

C# 2010 for programmers.

Chapter 11, OOP Programming: inheritance page:320 etc

10/13/2012 Muhammad Naveed 66

summery

Abstract Class and Methods

We can not create object of the Abstract Class.

The purpose of the abstract class is primarily to provide and appropriate base class from which other classes can inherit and thus share a common design.

Public abstract void show()//abstract method

10/13/2012 Muhammad Naveed 67

Case Study

Scenario

10/13/2012 Muhammad Naveed 68

Payroll System Using

Polymorphism

A company pay in employees on a weekly basis. The employees are of four types. Salaried Employees: are paid a fixed weekly salary

regardless of the number of hours worked. Hourly Employees: are paid by hour and receive

“time and a half” overtime pay for their sales. Salaried commission employees: are paid a

percentage of their sales. For the current pay period the company has decides

to reward salaried commission employees by adding 10% to their base salaries.

Company demands a C# application.

10/13/2012 Muhammad Naveed 69

Solution

We use abstract class as employee for general concept.

The other class that extends from employee abstract class are: SalariedEmployee, CommissionEmployee and HourlyEmployee.

ClassBasePlusCommissioEmployee Which extends from CommissionEmployee.

UML

10/13/2012 Muhammad Naveed 70

Employee

SalariedEmployee CommissionEmployee

BasePlusCommissionEmployee

HourlyEmployee

10/13/2012 Muhammad Naveed 71

Cases

Classes Earnigns ToString

Employee Abstract The abstract class for all other classes as a foundation first name,last name, social security number SSN

Salaried Employee weeklySalary Salaried employee:employee Weekly salary:

Hourly Employee If hours<=40 Wage=hours Ifhours>40 40*wage+(hours-40)+wage=1.5

Hourlty employee:employee Houtly wage :wage Hours work:hours

Comission Employee commissionRate*grossSales

Inherit from employee Gross sale:grassSales Comission rate:comissionRate

BasePlusComissionEmployee

comissionRate*grossSales+baseSalry

Base Salaried comsiion employee: 10/13/2012 Muhammad Naveed 72

Solution Book page no344

10/13/2012 Muhammad Naveed 73

Exception Handling

Try

{

Throuth exception()

}

Catch

{

Catch exception()

}

Finally

{

Show final()

}

10/13/2012 Muhammad Naveed 74

Overview

10/13/2012 Muhammad Naveed 75

GUI : Windows Forms

10/13/2012 Muhammad Naveed 76

Events And Handling them

10/13/2012 Muhammad Naveed 77

top related