77
PROPAGATING IN .NET By Muhammad Naveed MCS University of AJK, DIT Instructor Moon Creations School of IT 1

Total oop in c# dot net

Embed Size (px)

Citation preview

Page 1: Total oop in c# dot net

PROPAGATING IN .NET

By Muhammad Naveed MCS University of AJK, DIT

Instructor Moon Creations School of IT

1

Page 2: Total oop in c# dot net

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

Page 3: Total oop in c# dot net

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

Page 4: Total oop in c# dot net

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

Page 5: Total oop in c# dot net

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

Page 6: Total oop in c# dot net

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

Page 7: Total oop in c# dot net

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

Page 8: Total oop in c# dot net

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

Page 9: Total oop in c# dot net

9 10/13/2012 Muhammad Naveed

Page 10: Total oop in c# dot net

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

Page 11: Total oop in c# dot net

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

Page 12: Total oop in c# dot net

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

Page 13: Total oop in c# dot net

Major Concepts

OOP

Classes

Objects

Methods

Properties

Instance Variables

13 10/13/2012 Muhammad Naveed

Page 14: Total oop in c# dot net

14 10/13/2012 Muhammad Naveed

Page 15: Total oop in c# dot net

15 10/13/2012 Muhammad Naveed

Page 16: Total oop in c# dot net

Features of OOP

Inheritance

Polymorphism

Encapsulation

Abstraction

16 10/13/2012 Muhammad Naveed

Page 17: Total oop in c# dot net

STRUCTURED VS. OO PROGRAMMING

17

STRUCTURED PROGRAMMING:

10/13/2012 Muhammad Naveed

Page 18: Total oop in c# dot net

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

Page 19: Total oop in c# dot net

OBJECT ORIENTED PROGRAMMING

19 10/13/2012 Muhammad Naveed

Page 20: Total oop in c# dot net

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

Page 21: Total oop in c# dot net

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

Page 22: Total oop in c# dot net

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

Page 23: Total oop in c# dot net

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

Page 24: Total oop in c# dot net

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

Page 25: Total oop in c# dot net

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

Page 26: Total oop in c# dot net

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

Page 27: Total oop in c# dot net

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

Page 28: Total oop in c# dot net

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

Page 29: Total oop in c# dot net

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

Page 30: Total oop in c# dot net

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

Page 31: Total oop in c# dot net

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

Page 32: Total oop in c# dot net

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

Page 33: Total oop in c# dot net

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

Page 34: Total oop in c# dot net

Polymorphism

Compile time polymorphism(Overloading)

Run time Polymorphism( Overriding )

34 10/13/2012 Muhammad Naveed

Page 35: Total oop in c# dot net

Compile Time

Polymorphism(Overloading)

Can be Achieved by

Function Overloading

Operator Overloading

Constructor Overloading

35 10/13/2012 Muhammad Naveed

Page 36: Total oop in c# dot net

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

Page 37: Total oop in c# dot net

Example

37 10/13/2012 Muhammad Naveed

Page 38: Total oop in c# dot net

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

Page 39: Total oop in c# dot net

Operator Overloading

39 10/13/2012 Muhammad Naveed

Page 40: Total oop in c# dot net

Example 2

40 10/13/2012 Muhammad Naveed

Page 41: Total oop in c# dot net

41 10/13/2012 Muhammad Naveed

Page 42: Total oop in c# dot net

42 10/13/2012 Muhammad Naveed

Page 43: Total oop in c# dot net

43 10/13/2012 Muhammad Naveed

Page 44: Total oop in c# dot net

44 10/13/2012 Muhammad Naveed

Page 45: Total oop in c# dot net

Compile Time

Polymorphism(Overloading)

3. Constructor Overloading

45 10/13/2012 Muhammad Naveed

Page 46: Total oop in c# dot net

46 10/13/2012 Muhammad Naveed

Page 47: Total oop in c# dot net

47 10/13/2012 Muhammad Naveed

Page 48: Total oop in c# dot net

48 10/13/2012 Muhammad Naveed

Page 49: Total oop in c# dot net

49 10/13/2012 Muhammad Naveed

Page 50: Total oop in c# dot net

Lecture

Intro to GUI using C#

Windows Applications

Windows Forms

50 10/13/2012 Muhammad Naveed

Page 51: Total oop in c# dot net

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

Page 52: Total oop in c# dot net

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

Page 53: Total oop in c# dot net

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

Page 54: Total oop in c# dot net

Example

54 10/13/2012 Muhammad Naveed

Page 55: Total oop in c# dot net

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

Page 56: Total oop in c# dot net

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

Page 57: Total oop in c# dot net

OOP: Inheritance

10/13/2012 Muhammad Naveed 57

Community Member

Employee

Faculty

Admin Teacher

Staff

Student Alumnus

Page 58: Total oop in c# dot net

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

Page 59: Total oop in c# dot net

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

Page 60: Total oop in c# dot net

Relationship b/w base

classes and derived classes

10/13/2012 Muhammad Naveed 60

Page 61: Total oop in c# dot net

10/13/2012 Muhammad Naveed 61

Page 62: Total oop in c# dot net

10/13/2012 Muhammad Naveed 62

Page 63: Total oop in c# dot net

10/13/2012 Muhammad Naveed 63

Page 64: Total oop in c# dot net

10/13/2012 Muhammad Naveed 64

Page 65: Total oop in c# dot net

10/13/2012 Muhammad Naveed 65

Page 66: Total oop in c# dot net

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

Page 67: Total oop in c# dot net

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

Page 68: Total oop in c# dot net

Case Study

Scenario

10/13/2012 Muhammad Naveed 68

Page 69: Total oop in c# dot net

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

Page 70: Total oop in c# dot net

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

Page 71: Total oop in c# dot net

Employee

SalariedEmployee CommissionEmployee

BasePlusCommissionEmployee

HourlyEmployee

10/13/2012 Muhammad Naveed 71

Page 72: Total oop in c# dot net

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

Page 73: Total oop in c# dot net

Solution Book page no344

10/13/2012 Muhammad Naveed 73

Page 74: Total oop in c# dot net

Exception Handling

Try

{

Throuth exception()

}

Catch

{

Catch exception()

}

Finally

{

Show final()

}

10/13/2012 Muhammad Naveed 74

Page 75: Total oop in c# dot net

Overview

10/13/2012 Muhammad Naveed 75

Page 76: Total oop in c# dot net

GUI : Windows Forms

10/13/2012 Muhammad Naveed 76

Page 77: Total oop in c# dot net

Events And Handling them

10/13/2012 Muhammad Naveed 77