Software Development  Training Program Zeeshan Hanif Zeeshan Hanif
DotNet 3.5-101 Lecture 6 Zeeshan Hanif Zeeshan Hanif [email_address] [email_address]
Object Oriented Programming <ul><li>Object-oriented programming focuses on the development of self-contained software comp...
Object in Everyday Life <ul><li>All these real world objects have two characteristics:  state  and  behavior . For example...
Object in Programming <ul><li>Objects are useful in programming because you can set up a software model of real-world syst...
Class and Object <ul><li>In the real world, we often have many objects of the same kind </li></ul><ul><li>For example my c...
Class and Object <ul><li>Cars have some state (current gear, one engine, four wheels) and behavior (change gears, brake) i...
Class and Object <ul><li>A software (blueprint or map) for objects is called a  class. </li></ul><ul><li>You can also say ...
Example <ul><li>public class Car { </li></ul><ul><li>public int wheels; </li></ul><ul><li>public string color; </li></ul><...
Class Members <ul><li>Instance and Static Fields </li></ul><ul><li>Instance variables </li></ul><ul><ul><li>Each object of...
Zeeshan Hanif Class Definition public class Car{ public static int wheel; public string color; public bool isAutomatic; } ...
Class Members <ul><li>Instance and Static Methods </li></ul><ul><li>Unlike variables there is not any separate copy for ea...
Class Members Zeeshan Hanif c1 instance Instance Fields color c2 instance Instance Fields color All Methods NoOfWheels() S...
Defining Methods Zeeshan Hanif public int start(int a, string b,......) { // Executable code } The type of the value to be...
Defining Methods <ul><li>Parameter List </li></ul><ul><ul><li>Value Parameters </li></ul></ul><ul><ul><li>Reference Parame...
Encapsulation <ul><li>A powerful benefit of encapsulation is the hiding of implementation details from other objects. This...
Properties <ul><li>The encapsulation principle leads us to typically store data in private fields and provide access to th...
Properties syntax Zeeshan Hanif public class Student { private string name; public string  GetName () { return name; } pub...
Method Overloading <ul><li>If you want to use same method for different type of data processing then you have to overload ...
Method Overloading <ul><li>Two methods have the same signature if they have the same number of parameters, the parameters ...
Example <ul><li>public class MyMath{ </li></ul><ul><li>public int FindMax( int a, int b ){ </li></ul><ul><li>return a>b? a...
this <ul><li>Every instance method has a variable with name this which refers to the current object of which method is bei...
Example <ul><li>public class Human { </li></ul><ul><li>private string name; </li></ul><ul><li>public Human(string name) { ...
Constructor <ul><li>The purpose of a constructor is to provide you with the means of initializing the instance variables u...
Constructor <ul><li>Constructor is special method that is automatically called when an object is created. </li></ul><ul><l...
Constructor <ul><li>public class Human { </li></ul><ul><li>private string name; </li></ul><ul><li>private string address; ...
Multiple Constructor <ul><li>Default constructor </li></ul><ul><li>More then one Constructor just like method overloading ...
Constant and Readonly Fields <ul><li>const </li></ul><ul><li>If a field is declared as const then it is not really a varia...
const <ul><li>public class Car { </li></ul><ul><li>public  const  int wheel = 4; </li></ul><ul><li>public void ChangeWheel...
readonly <ul><li>readonly keyword gives a bit more flexibility than const, allowing for the case in which you might want a...
readonly <ul><li>public class RegistrationForm { </li></ul><ul><li>public  readonly  DateTime time; </li></ul><ul><li>publ...
Variable length parameter Lists <ul><li>When you to pass undefined number of parameters to any method then you use params ...
params <ul><li>public int FindMax( params int[] a ){ </li></ul><ul><li>int max = 0; </li></ul><ul><li>for(int i=0;i< a.Len...
Upcoming SlideShare
Loading in …5
×

C Sharp Jn (5)

402 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
402
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C Sharp Jn (5)

  1. 1. Software Development Training Program Zeeshan Hanif Zeeshan Hanif
  2. 2. DotNet 3.5-101 Lecture 6 Zeeshan Hanif Zeeshan Hanif [email_address] [email_address]
  3. 3. Object Oriented Programming <ul><li>Object-oriented programming focuses on the development of self-contained software components, called objects . </li></ul><ul><li>Objects in Everyday Life </li></ul><ul><ul><li>Objects are key to understanding object oriented technology. You can look around you now and see many examples of real-world objects: dog, car, television set, bicycle. </li></ul></ul>Zeeshan Hanif
  4. 4. Object in Everyday Life <ul><li>All these real world objects have two characteristics: state and behavior . For example car have states (current gear, number of gears, color, number of wheels) and behavior (braking, accelerating, slowing down, changing gears ) etc. </li></ul>Zeeshan Hanif
  5. 5. Object in Programming <ul><li>Objects are useful in programming because you can set up a software model of real-world system. </li></ul><ul><li>Software objects too have state and behavior. A software object maintains its state in one or more variable . A software object implements its behavior with methods . </li></ul><ul><li>An object is a software bundle of variables and related methods. </li></ul>Zeeshan Hanif
  6. 6. Class and Object <ul><li>In the real world, we often have many objects of the same kind </li></ul><ul><li>For example my car is just one of many cars in the world </li></ul><ul><li>Using object-oriented terminology, we say that my car object is an instance of the class of objects known as cars. </li></ul>Zeeshan Hanif
  7. 7. Class and Object <ul><li>Cars have some state (current gear, one engine, four wheels) and behavior (change gears, brake) in common. However, each car’s state is independent and can be different from that of other cars. </li></ul><ul><li>A construction company have a blueprint of house but my house is created on the basis of that blueprint. So that blueprint is class and my house is object of that class </li></ul>Zeeshan Hanif
  8. 8. Class and Object <ul><li>A software (blueprint or map) for objects is called a class. </li></ul><ul><li>You can also say that a class is a template </li></ul><ul><li>After you've created the car class, you can create any number of car objects from the class. Each instance gets its own copy of all the instance variables defined in the class. </li></ul>Zeeshan Hanif
  9. 9. Example <ul><li>public class Car { </li></ul><ul><li>public int wheels; </li></ul><ul><li>public string color; </li></ul><ul><li>public int noOfSeats; </li></ul><ul><li>} </li></ul>Zeeshan Hanif
  10. 10. Class Members <ul><li>Instance and Static Fields </li></ul><ul><li>Instance variables </li></ul><ul><ul><li>Each object of the class will have its own copy of each of the instance variables that appear in the class definition </li></ul></ul><ul><li>Static variables </li></ul><ul><ul><li>A given class will only have one copy of each of its class variable. The class variable exists even if no objects of the class have been created </li></ul></ul>Zeeshan Hanif
  11. 11. Zeeshan Hanif Class Definition public class Car{ public static int wheel; public string color; public bool isAutomatic; } Car1 Color isAutomatic Car2 Color isAutomatic Each object gets its own copy Shared between all objects Car.Wheel Car objects wheel
  12. 12. Class Members <ul><li>Instance and Static Methods </li></ul><ul><li>Unlike variables there is not any separate copy for each methods. </li></ul><ul><li>Therefore, instance and static methods are stored only once, and associated with the class as a whole. </li></ul><ul><li>Instance variable and methods can not be called from static methods </li></ul>Zeeshan Hanif
  13. 13. Class Members Zeeshan Hanif c1 instance Instance Fields color c2 instance Instance Fields color All Methods NoOfWheels() Start() Car Class Static Fields wheel
  14. 14. Defining Methods Zeeshan Hanif public int start(int a, string b,......) { // Executable code } The type of the value to be returned Name of the method The specification of the parameters for the method. If the method has no parameters, leave the parentheses empty Access modifier This is called the body of method Method Signature
  15. 15. Defining Methods <ul><li>Parameter List </li></ul><ul><ul><li>Value Parameters </li></ul></ul><ul><ul><li>Reference Parameters </li></ul></ul><ul><ul><li>Output Parameters </li></ul></ul><ul><ul><li>Class Parameters </li></ul></ul><ul><ul><li>Structure Parameters </li></ul></ul><ul><li>Argument Value Passing </li></ul>Zeeshan Hanif
  16. 16. Encapsulation <ul><li>A powerful benefit of encapsulation is the hiding of implementation details from other objects. This means that the internal portion (variables) of an object has more limited visibility than the external portion (methods). This will protect the internal portion against unwanted external access. </li></ul>Zeeshan Hanif
  17. 17. Properties <ul><li>The encapsulation principle leads us to typically store data in private fields and provide access to this data through public accessor methods that allow us to set and get values </li></ul><ul><li>So we have to create two methods such as GetData() and SetData() </li></ul><ul><li>C# provides a special property syntax that simplifies this process </li></ul>Zeeshan Hanif
  18. 18. Properties syntax Zeeshan Hanif public class Student { private string name; public string GetName () { return name; } public void SetName (string a) { name = a; } } public class Student { private string name; public string Name { get { return name; } set { name = value; } }} Using Property Using Methods
  19. 19. Method Overloading <ul><li>If you want to use same method for different type of data processing then you have to overload your method </li></ul><ul><li>That is two or more method have same names but different signature </li></ul>Zeeshan Hanif
  20. 20. Method Overloading <ul><li>Two methods have the same signature if they have the same number of parameters, the parameters have the same data types, and the parameters have the same modifiers (none, ref, out). The return type does not contribute to defining the signature of a method. </li></ul>Zeeshan Hanif
  21. 21. Example <ul><li>public class MyMath{ </li></ul><ul><li>public int FindMax( int a, int b ){ </li></ul><ul><li>return a>b? a : b; </li></ul><ul><li>} </li></ul><ul><li>public string FindMax( string a, string b ){ </li></ul><ul><li>return a.CompareTo(b) >= 0 ? a : b; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Zeeshan Hanif
  22. 22. this <ul><li>Every instance method has a variable with name this which refers to the current object of which method is being called. </li></ul><ul><li>It is called implicitly by the compiler when your method refer to any instance variable of the class </li></ul>Zeeshan Hanif
  23. 23. Example <ul><li>public class Human { </li></ul><ul><li>private string name; </li></ul><ul><li>public Human(string name) { </li></ul><ul><li>this.name = name; </li></ul><ul><li>} </li></ul><ul><li>public void setName(string name){ </li></ul><ul><li>this.name = name; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Zeeshan Hanif
  24. 24. Constructor <ul><li>The purpose of a constructor is to provide you with the means of initializing the instance variables uniquely for the object that is being created. </li></ul><ul><li>Constructor is special method with the name of Class and automatically called when an object is created. </li></ul>Zeeshan Hanif
  25. 25. Constructor <ul><li>Constructor is special method that is automatically called when an object is created. </li></ul><ul><li>A Constructor: </li></ul><ul><ul><li>has no return type </li></ul></ul><ul><ul><li>Has the same name as the class </li></ul></ul><ul><ul><li>Usually have public access </li></ul></ul><ul><ul><li>May take parameters which are passed when invoking new </li></ul></ul>Zeeshan Hanif
  26. 26. Constructor <ul><li>public class Human { </li></ul><ul><li>private string name; </li></ul><ul><li>private string address; </li></ul><ul><li>private int age; </li></ul><ul><li>public Human(string a, string b, int c) { </li></ul><ul><li>name = a; </li></ul><ul><li>address = b; </li></ul><ul><li>age = c; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Zeeshan Hanif
  27. 27. Multiple Constructor <ul><li>Default constructor </li></ul><ul><li>More then one Constructor just like method overloading </li></ul><ul><li>Duplicating objects using constructor </li></ul><ul><li>Calling constructor from constructor(eg. This) </li></ul><ul><li>Static constructor </li></ul>Zeeshan Hanif
  28. 28. Constant and Readonly Fields <ul><li>const </li></ul><ul><li>If a field is declared as const then it is not really a variable at all. It is treated as a fixed hard coded value in the program. </li></ul><ul><li>const variable is implicitly static and its value can not be changed </li></ul>Zeeshan Hanif
  29. 29. const <ul><li>public class Car { </li></ul><ul><li>public const int wheel = 4; </li></ul><ul><li>public void ChangeWheel(){ </li></ul><ul><li>wheel= 5; // compile-time error </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Zeeshan Hanif
  30. 30. readonly <ul><li>readonly keyword gives a bit more flexibility than const, allowing for the case in which you might want a field be to constant, but need to carry out some calculations to determine its initial value. </li></ul><ul><li>readonly can be static or non-static but once readonly variable is initialize it can not be changed </li></ul>Zeeshan Hanif
  31. 31. readonly <ul><li>public class RegistrationForm { </li></ul><ul><li>public readonly DateTime time; </li></ul><ul><li>public RegistrationForm() { </li></ul><ul><li>// one time initialization </li></ul><ul><li>time = DateTime.Now; // now this can not be changed </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Zeeshan Hanif
  32. 32. Variable length parameter Lists <ul><li>When you to pass undefined number of parameters to any method then you use params keyword </li></ul><ul><li>You can pass any number of parameter and it treats it as array. </li></ul><ul><li>WriteLine method is an example </li></ul>Zeeshan Hanif
  33. 33. params <ul><li>public int FindMax( params int[] a ){ </li></ul><ul><li>int max = 0; </li></ul><ul><li>for(int i=0;i< a.Lenght ;i++){ </li></ul><ul><li>if(a[i] > max) </li></ul><ul><li>max = a[i]; </li></ul><ul><li>} </li></ul><ul><li>return max; </li></ul><ul><li>} </li></ul><ul><li>Calling this method like this </li></ul><ul><li>FindMax( 4,5,6,7,5,3,2,3,3,4,3 ); </li></ul>Zeeshan Hanif

×