Classes and objects


Published on

Need of object orientation, Classes, Objects, Constructor, Destructor, Properties, and Automatic Properties.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Classes and objects

  1. 1. Classes and Objects Bhushan Mulmule
  2. 2. For video visit
  3. 3. Agenda Need of Object Orientation Classes and Objects Constructor Properties Automatic Properties Destructor
  4. 4. Need of Object Orientation
  5. 5. static void Main(string[] args) { int empNo; string name; float basicSalary, hra, da, pf, grossSalary; ... ... } Real world Digital world empNo Name basicSalary hra da pf grossSalary
  6. 6. Real world Digital world struct Employee { int empNo; string name; float basicSalary, hra, da, pf, grossSalary; } static void Main() { Employee emp; } empNo name basic Salary hra da pf gross Salary emp
  7. 7. Real world Digital world class Employee { public int empNo; public string name; public float basicSalary, hra, da, pf, grossSalary; public void CalculateSalary() { hra = 15 * basicSalary / 100; da = 10 * basicSalary / 100; pf = 5 * basicSalary / 100; grossSalary = basicSalary + hra + da - pf; } } class Program { static void Main() { Employee emp = new Employee(); } } empNo name basic Salary hra da pf gross Salary emp
  8. 8. e2 3000 _empNo _name _salary e1 2000 2000 3000 Main Program Employee this 30002000 _empNo _name _salary Heap
  9. 9. Class  A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events.  A class is like a blueprint. It defines the data and behavior of a type.  Client code can use it by creating objects or instances which are assigned to a variable.
  10. 10. Object  An object is a block of memory that has been allocated and configured according to the blueprint (typically class).  A program may create many objects of the same class.  Objects can be stored in either a named variable or in an array or collection.  Client code is the code that uses these variables to call the methods and access the public properties of the object.  In an object-oriented language such as C#, a typical program consists of multiple objects interacting dynamically.
  11. 11. Constructors
  12. 12. Constructor  Constructors are special methods with the same name as of class  Typically used to initialize the data members of the new object.  Invoked by the new operator immediately after memory is allocated for the new object.  Class can have multiple constructors that take different arguments (overloading)  Constructor that takes no parameters is called a default constructor.  If no constructor is provided, C# creates one by default.
  13. 13. e2 3000 _empNo _name _salary e1 2000 2000 3000 Main Program Employeethis 3000 _empNo _name _salary (int empno, string name, float salary) Employee
  14. 14. Properties
  15. 15. Properties  A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.  Properties can be used as if they are public data members, but they are actually special methods called accessors.  This enables data to be accessed easily and still helps promote the safety and flexibility of methods.  A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels.  Properties that do not implement a set accessor are read only.
  16. 16. e2 3000 _empNo _name _salary e1 2000 2000 3000 Main Program Employeethis 30002000 _empNo _name _salary EmpNo value get set
  17. 17. Automatic Properties  Improves readability  Reduces coding efforts  Are useful when validation and calculations are not required in properties. public int EmpNo { get; set; } private int _EmpNo; public int EmpNo { get { return this._EmpNo; } set { this._EmpNo = value; } } Compiler converts into
  18. 18. Destructor
  19. 19. Destructor  Destructor is special method with same name as of class name prefixed by tilde (~)  A class can only have one destructor.  Destructors cannot be inherited or overloaded.  Destructors cannot be called. They are invoked automatically by garbage collector just before destroying the object (if provided).  A destructor does not take modifiers or have parameters.  Destructors are also called when the program exits.  Can be used to write clean up code such as releasing unmanaged resources (if used in class)
  20. 20. e2 3000 _empNo _name _salarye1 2000 2000 3000 Main Program _empNo _name _salary fun1
  21. 21. Object Life Cycle Memory Allocation Constructor UseDestructor Memory Deallocation (by Garbage Collector)
  22. 22. _empNo _name _basic Salary _hra _da _pf _gross Salary e2 3000 _empNo _name _basic Salary _hra _da _pf _gross Salary e1 2000 2000 3000 Main Program Employee this 30002000
  23. 23. Bhushan Mulmule