Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.

Similar presentations


Presentation on theme: "Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no."— Presentation transcript:

1 Unit 2

2 Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no explicit return type. Syntax: Access class _name() { }

3 Simple constructor class rectangle { Public int length; Public int width; Public rectangle(int x,int y) // defining Constuctor { length = x; Width=y; } Public int RectArea() { Return(length*width) } Class RectangleArea { Public static void main() { rectangle rect1 =new Rectangle(15,10); // calling constructor int area1 =rect1.RectArea(); Console.WriteLine(“Area1 = “+area1); }

4 Overloaded constructor To create overloaded constructor method provide several different constructor definition with different parameter class Room { Public double length; Public double breadth; Public Room(double x,double y) // defining Constuctor 1 { length = x; breadth=y; } Public Room(double x) // defining Constuctor 2 { length=breadth=x; } { return(length*width) }

5 Types Static constructors Private constructors Copy constructors

6 Static constructor A static constructor is called before any object of the class is created. It is usually to assign initial values to static data members. Ex: Class Abc { Static Abc() // no parameter { ……………….// set values for static member here } Here no access modifier.

7 Copy constructor It creates an object by copying variables from another object. Ex: Public item (Item item) { Code=item.code; Price=item.price; } Copy constructor is invoked by instantiating an object of type Item and passing it the object to be copied. Ex: Item item2=new Item (item 1); Item2 is copy of item1

8 Private constructor C# does not have a global variables or constants. This constructor must be contained in a class only.

9 Destructors A destructor is opposite to a constructor. It is a method called when an object is no more required. Name as same in class name. Ex: Class fun { …….. ~Fun()// No aruguments { ……. }

10 Finalizers Finalizers are used to clean up any resources used by a class object when the object is destroyed.

11 garbage collector the finalizer will be called when the garbage collector decides that the object instance is no longer needed

12 INHERITANCE New class is derived from the existing class. Types: 1.Simple inheritance 2.Hierarchical inheritance 3.Multiple inheritance 4.Mutilevel inheritance

13 Polymorphism It is ability to take more than one form. Types: 1.Inclusion Polymorphism 2.operation Polymorphism

14 1.Inclusion Polymorphism

15 2.operation Polymorphism

16 indexers Indexers are location indicators and are used to access class objects. The indexer takes an index argument and looks like an array. The indexer is declared using the name this Ex: Public double this[int idx] { get { //return desired data } Set { //set desired data }

17 Delegates One person acting for another person. Method acting for another method. Steps: Delegate declaration Delegate methods definition Delegate instantiation Delagate invocation

18 Delegate declaration defines a class using the class system. delegate as a base class. Delegate methods are any functions whose signature matches the delegate signature exactly. Syntax: Modifier delegate return-type delegate name(parameter) Modifiers are New Public Protected Internal private

19 Examples Delegate void Simple Delegate(); Delegate int MathOperation(int x,int y); Public Delegate int CompareItems(obj o1,obj o2);

20


Download ppt "Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no."

Similar presentations


Ads by Google