oop in csharp part1

68
1 PDFill PDF Editor with Free Writer and Tools

Upload: mindtree-campus

Post on 24-Mar-2016

245 views

Category:

Documents


4 download

DESCRIPTION

OOP in CShapr Part1

TRANSCRIPT

Page 1: OOP in CSharp Part1

1

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 2: OOP in CSharp Part1

2

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 3: OOP in CSharp Part1

3

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 4: OOP in CSharp Part1

4

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 5: OOP in CSharp Part1

Notes:

A class is essentially a description of how to construct an object that contains fields and methods.. It provides a sort of template for an object.

In C# the data items are called fields and the functions are called methods.

Class declarations define new reference types. A class can inherit from another class, and can implement interfaces.

Generic class declarations have one or more type parameters.

5

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 6: OOP in CSharp Part1

Notes:

So what is an object?

An object embodies its own unique behavior, and each one models some object in the real world (and is therefore something that exists in time and space). A car or a book are examples of such objects. You can describe a car, repair it, drive it, and/or even sell it. You can buy a book, read a book, and/or even provide a review of the book.

But in the real world, tangible items are not the only kind of objects that are of interest to us during software development. A work assignment, for example, is not something you can touch but you can describe it, discuss it, assign it and/or complete it. Book borrowing is also not tangible but you can describe it, monitor it and/or report it.

Basically, anything that you can describe can be represented as an object, and that representation can be created, manipulated and destroyed to represent how you use the real object that it models.

6

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 7: OOP in CSharp Part1

Notes:

Each object defines three basic types of information that it must know:

1. An object has to describe the features that will allow its users to distinguish it from other objects. It needs to have an identity. Even if two objects share the same features, each object has a unique identity.

2. An object must be able to describe itself. This type of information is stored in an object’s attributes and which form the object’s structure.

3. An object must be able to describe its current condition, called its state. Object state is sometimes represented by the values of each of its attributes.

For example, a car can be brand new or worn out. Other times, the state is represented by the presence or absence of key relationships with other objects. For example, a book can be reserved or ordered. A reserved book has a relationship with the person who reserved it. An ordered book has a relationship with an order.

7

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 8: OOP in CSharp Part1

Stack: Section of process memory space where memory spaces for local variables (local to the method, such as Main method) are allocated

Heap: Section of process memory space where memory spaces for objects are allocated

Object of Car class will be created and will be placed in the heap. The reference of the object will be stored in a variable of that class and the memory space of that variable will be allocated in the stack.

8

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 9: OOP in CSharp Part1

9

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 10: OOP in CSharp Part1

10

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 11: OOP in CSharp Part1

11

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 12: OOP in CSharp Part1

12

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 13: OOP in CSharp Part1

13

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 14: OOP in CSharp Part1

14

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 15: OOP in CSharp Part1

15

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 16: OOP in CSharp Part1

16

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 17: OOP in CSharp Part1

Consider a bank account, where account maintains a balance and also supports behavior such as, withdrawal and deposit of money.

So, if you want to represent bank account through OOP, then create a class BankAccount, with a field ‘balance’ and two methods, ‘Withdraw and ‘Deposit’

17

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 18: OOP in CSharp Part1

If you declare balance as private field then you are not allowing balance to be used directly from outside the class, thus essentially blocking the view of the variable.

But, the variable can be used by both the methods in the classs, which are public, hence are accessible outside the class.

18

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 19: OOP in CSharp Part1

19

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 20: OOP in CSharp Part1

No memory space allocation for a property.

Memory space is allocated for the local variable ‘value’ of the ‘set’ accessor

You can think of ‘set’ accessor of ModelName property like the following method:

public void set_ModelName(string value) { modelName = value; }

You can think of ‘get’ accessor of ModelName property like the following method:

public string get_ModelName() { return modelName;}

Actually, a property contains two methods internally like those mentioned above.

Property gives you the illusion of a normal field, that is why it is a natural extension to a field.

Also, property internally behaves like a method, that is why it is known ‘Property Function’

20

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 21: OOP in CSharp Part1

21

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 22: OOP in CSharp Part1

22

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 23: OOP in CSharp Part1

23

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 24: OOP in CSharp Part1

24

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 25: OOP in CSharp Part1

25

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 26: OOP in CSharp Part1

26

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 27: OOP in CSharp Part1

27

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 28: OOP in CSharp Part1

In this example:

1. Account is class which represents accounts of different persons

2. It has two fields, namely ‘owner’ and ‘balance’

3. Withdraw method debits some amount from your account and returns the updated current balance

4. Deposit method credits some amount to your account and returns the updated current balance

5. To objects of Account class has been created, one for Mr. Ramesh and another for Mr. Suresh

6. Some amount is there in their account as Balance

7. Some amount has been debited from each of their account and updated balance has been returned by Withdraw() method

8. Notice: Balance is different for different object (as well as Owner data)

9. Balance (and Owner) is instance data, contains different values for different instances

28

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 29: OOP in CSharp Part1

29

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 30: OOP in CSharp Part1

30

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 31: OOP in CSharp Part1

31

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 32: OOP in CSharp Part1

In this example:

1. Account is class which represents accounts of different persons

2. It has three fields, namely ‘owner’ and ‘balance’ and ‘interestRate’.

3. Interest rate is same for all account holders. So, it is not necessary to be a instance field, rather it is a static field.

4. Sinec, ‘iterestRate’ has been declared as a static field, it will not be part of any instance. It will be accessed using class name.

5. A static method has been provided to set the value for interest rate, too. In static method you can only use static data.

6. CalculateInterestAmountonBalance method calculates total interest amount generated for the current balance for the account holder.

7. To objects of Account class has been created, one for Mr. Ramesh and another for Mr. Suresh

8. Some amount is there in their account as Balance

9. Interest amount has been calculated based on the same interest rate for the available balance.

10. So, static member is shared amongst different user. It is not stored as a part of the instance.

11. Notice: Balance is different for different object (as well as Owner data)

12. Balance (and Owner) is instance data, contains different values for different instances

32

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 33: OOP in CSharp Part1

33

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 34: OOP in CSharp Part1

34

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 35: OOP in CSharp Part1

35

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 36: OOP in CSharp Part1

36

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 37: OOP in CSharp Part1

37

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 38: OOP in CSharp Part1

38

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 39: OOP in CSharp Part1

Note:

1. A derived class inherits most elements of its base class

2. A derived class cannot be more accessible than its base class

39

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 40: OOP in CSharp Part1

40

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 41: OOP in CSharp Part1

41

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 42: OOP in CSharp Part1

42

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 43: OOP in CSharp Part1

43

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 44: OOP in CSharp Part1

44

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 45: OOP in CSharp Part1

45

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 46: OOP in CSharp Part1

46

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 47: OOP in CSharp Part1

47

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 48: OOP in CSharp Part1

48

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 49: OOP in CSharp Part1

49

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 50: OOP in CSharp Part1

50

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 51: OOP in CSharp Part1

51

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 52: OOP in CSharp Part1

52

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 53: OOP in CSharp Part1

Method overloading: The same class has many methods with same name

Method overriding: The method name resides in the base class and the method implementations reside in the derived classes

53

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 54: OOP in CSharp Part1

54

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 55: OOP in CSharp Part1

55

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 56: OOP in CSharp Part1

56

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 57: OOP in CSharp Part1

57

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 58: OOP in CSharp Part1

58

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 59: OOP in CSharp Part1

59

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 60: OOP in CSharp Part1

60

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 61: OOP in CSharp Part1

61

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 62: OOP in CSharp Part1

62

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 63: OOP in CSharp Part1

63

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 64: OOP in CSharp Part1

64

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 65: OOP in CSharp Part1

65

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 66: OOP in CSharp Part1

66

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 67: OOP in CSharp Part1

67

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Page 68: OOP in CSharp Part1

68

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s