You are on page 1of 25

‡What is Object-oriented programming ‡How OO model applies to programming ‡Difference between classes and objects.

‡How to create classes and object. ‡How to use objects.

Real-World Model
‡ Object orientated programming is a software

development methodology that is based on modeling a real-world system. ‡ An object oriented program consists of classes and objects. ‡ When we talk about generic thing for example vehicle which can be any vehicle such as motorcycle,bus,truck and bike etc. then vehicle in this case is class and specific things like bike ,motorcycle, bus and trucks are called as its objects. ‡ Let us understand the terms class and objects through diagram.

Real-World Model Vehicle Class Motorcycle Bike Truck Objects .

y Engine contains following Function and Data. all these are objects and motorcycle is bigger object. y Any object have function and data. Function Igniting fuel Moving piston Procedure Opening valves Ejecting burnt fuel Rotating crank Data Oil Temp Oil density RPM Timing of fuel Types of fuel PROPERTIES .gearbox.Real-World Model y Object may contain other objects for example motorcycle has engine.brake and Lights.

Classes and Objects Class Car Suzuki Reno Toyota Camry Honda Acura Objects .

Class Employee name age set() .show() . y Class is a blueprint or Template that is used to create and identify object. y Object is specific data. y Class is generic data.Difference between class and objects. show() object e1 object e2 Sanjay 30 set(). show() Sameer 32 set().

s .mm.yy gettime() setdate() settime() h.More Classes and Objects y DateTime class Functions getdate() Data dd.m.

gd() Object d2 10/02/2009 11:10:15 sd().y Object d1 13/12/2007 12:25:30 sd() . gd() .

year and hour minutes second. y We have object d1 with 13/12/2007 as specific date and 12:25:30 is a specific time and have setdate() and getdate() similarly gettime() and settime() . y Similarly object d2 with specific date and specific time and have setdate() and getdate() similarly gettime() and settime() . y From same class we can create multiple objects. It also contain functions like getdate()and gettime() to access data whereas setdate() and settime() allows to manipulate the data.month. .More classes and objects y In previous slide we have create DateTime class which contain data like day.

Data structure is means of arranging the data in memory. LIFO means whatever items are added last in the list comes out first. . Stack is one of data structures which LIFO list(Last in first Out) list.Working of Stack y Stack is used in recursion and evaluation of some y y y y arithmetic expression. To keep track of last item in the list we have pointer known as TOP.

Stack Operation y Two operation can be perform on the list one is Push which means insertion and another is Pop which means deletion. If stack is implemented by array the TOP pointer with no element will have value -1. stack can also be implemented by linked list y Diagrammatic explanation of PUSH operation of stack Top TOP 10 EMPTY TOP PUSH 10 20 10 PUSH 20 .

Pop operation of Stack TOP Pop Pop 20 .

Stack Class & Stack Objects. Stack Class Functions ‡ PUSH() ‡ POP() ‡ Printall() ‡ Data Arrays Top .

pop() .pop() Object s2 7 elements Top=6 push().Object s1 5 elements Top=4 push().

public float j.Creating of Classes & Objects class Sample { public int i. } .

Creating of Classes & Objects public class ClassDemo { public static void Main() { Sample a.i=20.14f.j=3. Console. +b.WriteLine(a.i=10.28f. } } +a.j).b=. a. a=new Sample().b.j=6.WriteLine(bi + Console.i + b=new Sample().j). . a. Console. b.ReadLine().

Explaination of Creation of classes and objects. a and b are reference to object of type sample. Sample is a class which is user-Defined . These program have two classes .so we define class Sample which contains public int I and public float j. . Within Main(). Reference hold the address of the objects. For example a=new Sample().Sample and ClassDemo.we y y y y say Sample a. y class ClassDemo contain Main(). To create object we use keyword new. a is holding the address of nameless object that has been created.b.

.i=10 and a.j=3.j=6.28f and finally print those values.y a.we say b. Similar for b =new Sample().14f means I and j belonging to object to which a is pointing.i=20 and b.

Better Ways. j=jj.WriteLine( i + +j). public float j. class Sample { public int i. } } . float jj) { i=ii. public void SetData(int ii. } public void ShowData() { Console.

14f).ShowData(). a=new Sample().6. } } .28f).3.b.SetData(20. a. public class ClassDemo { public static void Main() { Sample a.Better Ways.ReadLine().ShowData(). b. b=new Sample().SetData(10. b. Console. a.

Better Ways y In previous program .

class Sample { public int age.Which is better y //Program 1 using System. } .

int x. s1=new Sample(). } } .s2.class Demo { public static void Main() { sample s1. if(x>0) s1. if(x>0) s2. s2=new Sample().age=x.age=x.

class Sample { public int age. public void SetData(int i) { if(x>0) age=x.y //Program 2 using System. } } .

class Demo { public static void Main() { sample s1. s1.SetData(x).SetData(x).s2. int x. } } . s2=new Sample(). s2. s1=new Sample().