You are on page 1of 10
Object Based Programming OOP Concepts /bject Based Programming * Osama.M.Abu Elnasr Object Based Programming i oop Structured Programming * Based on Object oriented, ¢ Based on Action Oriented. * Program consists of a set of ¢ Program consist of : a set of classes. method , each of which perforn| a specific function . ¢ Collect variables (data) with a! : set of related methods. * Separated data from method. OOP based on four basic concepts : 1, Encapsulation. 2. Information Hiding. 3. Inheritance. 4. Polymorphism. Encapsulation : 7 AlSI G2 Ge gsi aly neice i methodsulsally Variables clit! gusiihe o = Class Aes pass paiall 10g ¢ Aisne Lage Sits Lola cl sally cial Data Hiding ttsll list faye Gyisd Abul » sis Go qed Information Hiding :hide implementation from object . 1 GUL glial gee IBS Ys GIS Ayleal sadineall Ge LL lial o OAS be Class J! ee abeill iy Cua padinnall Ge! Class Ul dae y: deal lial } = Sahalitll (185 dgaly Inheritance : Y you can derive a new class that inherit all class features (members data and functions) in addition to adding new: features to the derived class . : Wg «Mell jpee gle ally cs sind de gue SI Class, os Bae Class littl le os bane Classes eli 52 92.s4ll Classes ul plas! date! gle 48) 5 Si acted V7 gilt Jualill gle sing gill a SN GS Sale! tI dala aject Based Programming 1 Osama.M.Abu Elnasr Polymorphism : 1 1 DSLY! aed \ i gall Gee gle alin His joy al VI Lait le JISAY anni acted g A ho ct gle I a AIS Hy Dayal gd Jl oni Gags gta Method : atlas Ge dell Aad Oa JAS sad Bel al Lal p85 Gilly « CHEM Atl Jlyall a alla Object cist It can be public or private : Public methods are called access method that read or display data. “+ Predicate Method : a common use of access method that check the truth of of condition , such as : isFull (): test if object is full or not. ‘ + Private method.>>>Helper Method : (Utility Method ) oA private method within class , called only by other methods of that class . o__ The purpose of is to support the operation of class's other methods, Overload : _ lide jal ly OSs dgulitie clea lel 9S Gb JLall Method Jl wo als se 4 oN Ghul site aly diya Gilbey + atlas Methods Variables i sh pl ot jue oy ad ' 1 dilaill faye cole laliie. ae 2 deal gl ds gl de Class Ahad : yiiey gluaid eee D Saat ¢ 6) eae «Abia — cats SG : ot Galan gl ae — pal alle paglt AlSby gia gly . “4 a ? Yana lig oo 2 rps — ISG: ol guleinny Cig) — ao — pul elle : «aaa : : ails lags « salle ee ere | Object eel «A i — USO OF gaat Gd — jae — pul: ‘ sii oh (Chay « Ghat ye saan tee ject Based Programming Class A class is just a collection of variables (often with different types) combined with a set of related methods. Apple | Lilie LUIS pL) AIA Gy py ill: Ci An object is an individual instance of a class. ost gill Ct Ul gall ya Zo gant Gale Gating af ia Und SS 3a ga Uh gd Object ce Ji oil glint oaly + Class d) Jala Wastes pay iB yy Ciily Gye Aes Lidl Atel) Gailadll ol daly Class oll clint Ui! Jyii ull ject JI yauadi ay sl AY! a O_o class class_name { // class variables and methods declared here } Class_name Object = new Class_name() ; I sts pal Ha 45 gl Object ut Gals gy -main method should be set as private. | value . determines what the objects of your ‘Member variables are known as data member: the variables in your class tha? Static members belongs to the class itself & object don't have access its Member functions are known as methods, the function in your class that lass can do & must be set as public. Private Public Private data member can be accessed only within methods of the class itself Public data member can be accessed through any object of the class . * All members of a class--data and m ethods--are private by default. Protected protected data member can be accessed directly by object of any inherited class . Aject Based Programming Osama.M.Abu Elnasr Class Human { ClassName ObjectName = new ClassName() ; oe ‘ Human h1 = new Human () ; Public int age ; ObjectName .’VarName = Value ; Public string name ; Public void speak () oi : . Method u} eles y Seen Human speak ObjectName . MethodName() ; } h1.speak() ; } Constructor vs. Destructor > Constructor create and initialize objects | > Destructors clean up and of your class, free any memory you might | have allocated. | > Constructor class method with the same | > Destructor class method with name as the class itself. the same name as the class , preceded by a tilde (~). > Constructor can take parameters as needed, it] > Destructor take no cannot have a return value, not even void. arguments & have no return value . Class Ul (pi Ad wall Gl stall Alsi af ellae| gi pada Class J! Jala Method o zl ol gd Method ut (ats we jes Constructor! USly 0 "class oI eu! sii = void is Ys iad aa» Human () Constructor Destructor rae 7 Human () i i i } } | Osama, He et Jbject Based Programming M.Abu Eihase class class ‘name ' : class class_name initialization Jed ( f oN { Tid WE ool itl aad Constructor «gil i // class variables and Clad sllel //class variables & methods here methods declared sMelasial ae Sis Closehiane (Parameters) h : ; CT Ver value; } Public Human () } { Public Human () { iy #4 Constructor can be overloaded Ji | Public Human (int a) WSs Chas Heat aon Side css | ; i ele sia : Public Human (int a, string b) 1 CDdeall ase gi ! { 1 age= a; name =b ; } Human hi = new Human (22); Console.writeline("age"+h1 .age) : I age:22 ost Human h2 = new Human (30,"ahmed"); Console writeline("age"+h2.age+"name"'+h2.name Dc i name : ahmed J! s.age:30 of sai ce A pines « > sua a LOU Class Ut ea dal gil sep hte pal esp fal... § Sli dale gle Ulin Memory = null ; aga Ul ele stad oy 8 gy h2= null ; aos » Asi 8). Destructor eB pl oay System.GC.Collect ( ); ‘ ject Based Programming Properties Osama,M.Abu Elnasr. ole Gill yi. Programmer di dbalys ob 5 « Cl peiall ASI ad ele | Ais ge Lid Mia yeh ol etal af user J} Lda AslSel get accessor set accessor o Enables objects to read data . | o Obtain the values of private data o Enables objects to modify data © Assign values to private data. o ‘Ensure that the new value is appropriate for the data member . /! property Hour public int Hour get { return hour; ; set { hour = value; LL Write a program that declare a class called player . class should contain three private variables: age, weight, and speed . The class should contain a constructor that initializes the three data members. The class should contain variable that keep track of number of players, then Define a Player object; assign 23 to age, and output speed . , ' Using system; namespace playerdesign Class player { Private int age Private int speed ; Private int weight ; FE —Ce 2bject Based Programming Public Static int count ; Public player(int age , int speed, int weight ) { this.age= age; this.speed=speed; this.weight=weight: this.count++; } Public int Age { set { This.age=value; 3 } 1 | Public int Speed { get ' { return speed ; } } // End class player Class test { Static void main(string [] args) { Player pl=new player( 20,30,40) : | Pl. Age=23 ; Console.writeline("player's speed is: "+pl.Speed ) //End main method }//End class test 3//End namespace Osaria.M.Abu Ene oa eet Jbject Based Programming \ # Create a class called Complex for performing arithmetic +with complex numbers. Write a driver program to test your class . Use floating-point variables to represent the private data of the class. Provide constructor that enables an object of this class to be initialized when it is declared. Provide.a no-argument constructor with a default values in case no initializers are provided. Provide public methods for each of the following : . a) Addition of two Complex numbers, The real parts are added together and the imaginary parts are added together . b) Subtraction of two Complex numbers. The real parts of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. c) Printing of Complex numbers in the form (a,b) , where ais the real; part and b is the imaginary part . ' Class Complex ' { Private float realPart, Private float imaginaryPart, //Constructor : default , with no-argument .; Public Complex( ) 1 { } //Constructor, initialize values ' , Public Complex float real, float imaginary) this. realPart = real; i: this. imaginaryPart = imaginary ; } Jbject Based Programming Public complex add (complex.c) { this.realpart+=c.realpart ; this.imaginarypart+=c.imaginarry ? Public complex sub (complex c) { this.realpart -= c.realpart ; this.imaginarypart -=c.imaginary : { Public string display () { Console.writeline( “(+this.realpart+","+this.imaginarypart+")" ); ) ! | Static void main(string [] args) { | | Complex c = new Complex ( 4,5); _ c.add(new Complex ( 2,3)) ; | csub(new Complex ( 2,3)) ; c.display( ) : /End main method Complex c = new Complex ( 4,5); Complex d = new Complex (2,3) ; c.add(d) ; c.sub(d) ; c.display( ) : V// End class Complex

You might also like