c sharp jn (5)
DESCRIPTION
TRANSCRIPT
Zeeshan Hanif
Software Development Training Program
Zeeshan Hanif
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.
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.
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.
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.
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
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.
Zeeshan Hanif
Example
public class Car {public int wheels;public string color;public int noOfSeats;
}
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
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
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
Zeeshan Hanif
Class Members
Car Class
Static Fields
wheel
c1 instance
Instance Fields
color
c2 instance
Instance Fields
color
All Methods
NoOfWheels()
Start()
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
Zeeshan Hanif
Defining Methods
Parameter List Value Parameters Reference Parameters Output Parameters Class Parameters Structure Parameters
Argument Value Passing
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.
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
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
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
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.
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; }}
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
Zeeshan Hanif
Example
public class Human { private string name; public Human(string name) {
this.name = name; } public void setName(string name){
this.name = name; }}
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.
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
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; }}
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
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
Zeeshan Hanif
const
public class Car { public const int wheel = 4; public void ChangeWheel(){ wheel= 5; // compile-time
error }}
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
Zeeshan Hanif
readonly
public class RegistrationForm { public readonly DateTime time; public RegistrationForm() { // one time initialization time = DateTime.Now;
// now this can not be changed } }
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
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);