c sharp jn (5)

33
Zeeshan Hanif Software Development Training Program Zeeshan Hanif

Upload: jahanullah

Post on 02-Dec-2014

615 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C Sharp Jn (5)

Zeeshan Hanif

Software Development Training Program

Zeeshan Hanif

Page 2: C Sharp Jn (5)

Zeeshan Hanif

DotNet 3.5-101 Lecture 6

Zeeshan [email protected]

[email protected]

Page 3: C Sharp Jn (5)

Zeeshan Hanif

Object Oriented Programming Object-oriented programming

focuses on the development of self-contained software components, called objects.

Objects in Everyday Life 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.

Page 4: C Sharp Jn (5)

Zeeshan Hanif

Object in Everyday Life

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.

Page 5: C Sharp Jn (5)

Zeeshan Hanif

Object in Programming Objects are useful in programming because

you can set up a software model of real-world system.

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.

An object is a software bundle of variables and related methods.

Page 6: C Sharp Jn (5)

Zeeshan Hanif

Class and Object

In the real world, we often have many objects of the same kind

For example my car is just one of many cars in the world

Using object-oriented terminology, we say that my car object is an instance of the class of objects known as cars.

Page 7: C Sharp Jn (5)

Zeeshan Hanif

Class and Object 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.

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

Page 8: C Sharp Jn (5)

Zeeshan Hanif

Class and Object A software (blueprint or map) for objects is

called a class. You can also say that a class is a template

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.

Page 9: C Sharp Jn (5)

Zeeshan Hanif

Example

public class Car {public int wheels;public string color;public int noOfSeats;

}

Page 10: C Sharp Jn (5)

Zeeshan Hanif

Class Members Instance and Static Fields Instance variables

Each object of the class will have its own copy of each of the instance variables that appear in the class definition

Static variables 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

Page 11: C Sharp Jn (5)

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

wheel

Shared between all objects

Car.Wheel

Car objects

Page 12: C Sharp Jn (5)

Zeeshan Hanif

Class Members

Instance and Static Methods Unlike variables there is not any

separate copy for each methods. Therefore, instance and static

methods are stored only once, and associated with the class as a whole.

Instance variable and methods can not be called from static methods

Page 13: C Sharp Jn (5)

Zeeshan Hanif

Class Members

Car Class

Static Fields

wheel

c1 instance

Instance Fields

color

c2 instance

Instance Fields

color

All Methods

NoOfWheels()

Start()

Page 14: C Sharp Jn (5)

Zeeshan Hanif

Defining Methods

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

Page 15: C Sharp Jn (5)

Zeeshan Hanif

Defining Methods

Parameter List Value Parameters Reference Parameters Output Parameters Class Parameters Structure Parameters

Argument Value Passing

Page 16: C Sharp Jn (5)

Zeeshan Hanif

Encapsulation

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.

Page 17: C Sharp Jn (5)

Zeeshan Hanif

Properties 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

So we have to create two methods such as GetData() and SetData()

C# provides a special property syntax that simplifies this process

Page 18: C Sharp Jn (5)

Zeeshan Hanif

Properties syntax

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

Page 19: C Sharp Jn (5)

Zeeshan Hanif

Method Overloading

If you want to use same method for different type of data processing then you have to overload your method

That is two or more method have same names but different signature

Page 20: C Sharp Jn (5)

Zeeshan Hanif

Method Overloading

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.

Page 21: C Sharp Jn (5)

Zeeshan Hanif

Example

public class MyMath{ public int FindMax(int a, int b){ return a>b? a : b; } public string FindMax(string a, string b){ return a.CompareTo(b) >= 0 ? a : b; }}

Page 22: C Sharp Jn (5)

Zeeshan Hanif

this

Every instance method has a variable with name this which refers to the current object of which method is being called.

It is called implicitly by the compiler when your method refer to any instance variable of the class

Page 23: C Sharp Jn (5)

Zeeshan Hanif

Example

public class Human { private string name; public Human(string name) {

this.name = name; } public void setName(string name){

this.name = name; }}

Page 24: C Sharp Jn (5)

Zeeshan Hanif

Constructor

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.

Constructor is special method with the name of Class and automatically called when an object is created.

Page 25: C Sharp Jn (5)

Zeeshan Hanif

Constructor Constructor is special method that

is automatically called when an object is created.

A Constructor: has no return type Has the same name as the class Usually have public access May take parameters which are

passed when invoking new

Page 26: C Sharp Jn (5)

Zeeshan Hanif

Constructor

public class Human { private string name; private string address; private int age; public Human(string a, string b, int c){

name = a; address = b; age = c; }}

Page 27: C Sharp Jn (5)

Zeeshan Hanif

Multiple Constructor

Default constructor More then one Constructor just like

method overloading Duplicating objects using

constructor Calling constructor from

constructor(eg. This) Static constructor

Page 28: C Sharp Jn (5)

Zeeshan Hanif

Constant and Readonly Fields

const 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.

const variable is implicitly static and its value can not be changed

Page 29: C Sharp Jn (5)

Zeeshan Hanif

const

public class Car { public const int wheel = 4; public void ChangeWheel(){ wheel= 5; // compile-time

error }}

Page 30: C Sharp Jn (5)

Zeeshan Hanif

readonly

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.

readonly can be static or non-static but once readonly variable is initialize it can not be changed

Page 31: C Sharp Jn (5)

Zeeshan Hanif

readonly

public class RegistrationForm { public readonly DateTime time; public RegistrationForm() { // one time initialization time = DateTime.Now;

// now this can not be changed } }

Page 32: C Sharp Jn (5)

Zeeshan Hanif

Variable length parameter Lists

When you to pass undefined number of parameters to any method then you use params keyword

You can pass any number of parameter and it treats it as array.

WriteLine method is an example

Page 33: C Sharp Jn (5)

Zeeshan Hanif

params

public int FindMax(params int[] a){int max = 0;for(int i=0;i< a.Lenght ;i++){

if(a[i] > max)max = a[i];

}return max;

}Calling this method like thisFindMax(4,5,6,7,5,3,2,3,3,4,3);