0
Classes and Objects
Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com
For video visit
www.dotnetvideotutorial.com
Agenda
Need of
Object
Orientation
Classes and
Objects
Constructor Properties
Automatic
Properties
Destructor
www.dotnetvideotutorial.com
Need of Object
Orientation
static void Main(string[] args)
{
int empNo;
string name;
float basicSalary, hra, da, pf, grossSalary;
...
...
}
Real worl...
Real world Digital world
struct Employee
{
int empNo;
string name;
float basicSalary, hra, da, pf, grossSalary;
}
static v...
Real world Digital world
class Employee
{
public int empNo;
public string name;
public float basicSalary, hra, da, pf, gro...
www.dotnetvideotutorial.com
e2
3000
_empNo _name _salary
e1
2000
2000
3000
Main
Program
Employee
this
30002000
_empNo _nam...
Class
 A class is a construct that enables you to create your own custom types by
grouping together variables of other ty...
Object
 An object is a block of memory that has been allocated and configured
according to the blueprint (typically class...
Constructors
www.dotnetvideotutorial.com
Constructor
 Constructors are special methods with the same name as of class
 Typically used to initialize the data memb...
www.dotnetvideotutorial.com
e2
3000
_empNo _name _salary
e1
2000
2000
3000
Main
Program
Employeethis
3000
_empNo _name _sa...
Properties
www.dotnetvideotutorial.com
Properties
 A property is a member that provides a flexible mechanism to read, write, or
compute the value of a private f...
www.dotnetvideotutorial.com
e2
3000
_empNo _name _salary
e1
2000
2000
3000
Main
Program
Employeethis
30002000
_empNo _name...
Automatic Properties
 Improves readability
 Reduces coding efforts
 Are useful when validation and calculations are not...
Destructor
www.dotnetvideotutorial.com
Destructor
 Destructor is special method with same name as of class name prefixed by tilde (~)
 A class can only have on...
e2
3000
_empNo _name _salarye1
2000
2000
3000
Main
Program
_empNo _name _salary
fun1
Object
Life
Cycle
Memory
Allocation
Constructor
UseDestructor
Memory
Deallocation
(by Garbage
Collector)
www.dotnetvideotu...
_empNo _name _basic
Salary
_hra _da _pf _gross
Salary
e2
3000
_empNo _name _basic
Salary
_hra _da _pf _gross
Salary
e1
200...
Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com
Classes and objects
Classes and objects
Classes and objects
Classes and objects
Classes and objects
Classes and objects
Upcoming SlideShare
Loading in...5
×

Classes and objects

382

Published on

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

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
382
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
63
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Classes and objects"

  1. 1. Classes and Objects Bhushan Mulmule bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com
  2. 2. For video visit www.dotnetvideotutorial.com
  3. 3. Agenda Need of Object Orientation Classes and Objects Constructor Properties Automatic Properties Destructor
  4. 4. www.dotnetvideotutorial.com 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 www.dotnetvideotutorial.com
  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 www.dotnetvideotutorial.com
  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 www.dotnetvideotutorial.com
  8. 8. www.dotnetvideotutorial.com 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. www.dotnetvideotutorial.com
  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. www.dotnetvideotutorial.com
  11. 11. Constructors www.dotnetvideotutorial.com
  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. www.dotnetvideotutorial.com
  13. 13. www.dotnetvideotutorial.com 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 www.dotnetvideotutorial.com
  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. www.dotnetvideotutorial.com
  16. 16. www.dotnetvideotutorial.com 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. www.dotnetvideotutorial.com 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 www.dotnetvideotutorial.com
  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) www.dotnetvideotutorial.com
  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) www.dotnetvideotutorial.com
  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 bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×