1 / 17

Object-Oriented Programming

Object-Oriented Programming. Agenda. Static Const Overloading Inheritance. Static. Each instance of a class (called an object) has a copy of the attributes Changing an attribute in one object doesn’t affect the attribute of another object

seanreed
Download Presentation

Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming

  2. Agenda • Static • Const • Overloading • Inheritance

  3. Static • Each instance of a class (called an object) has a copy of the attributes • Changing an attribute in one object doesn’t affect the attribute of another object • But what if we want persistence (shared) among all instances?

  4. Static Variables void PrintNumbers () { static int count = 0; // this is only set to 0 ONCE Console.WriteLine(count); count++; } static void Main() { ... for(i=0; i < 50; i++) PrintNumbers(); ... }

  5. Static Attributes class BMW_Z4 { private static int VehicleId = 0; public int MyID; public BMW_Z4 () { MyID = VehicleId; VehicleId++; } ... } static void Main() { ... BMW_Z4 my_z4 = new BMW_Z4(); // has VehicleId of 0 BMW_Z4 your_z4 = new BMW_Z4(); // has VehicleId of 1 ... }

  6. Const • Variables are just that – variable • They can be changed programmatically via the assignment operator (=) • But there are times when some values should be immutable • Preface the declaration with “const” • Cannot be changed – EVER!

  7. Const Example class BMW_Z4 { public const int MaxSpeed = 185; private int currentSpeed; public void Accelerate { currentSpeed += 5; if (currentSpeed > MaxSpeed) currentSpeed = MaxSpeed; } ... }

  8. Overloading • Overloading involves using the same method/function name • Vary the number of parameters • Vary the type of parameters • Cannot just change return type (ambiguity in invocation) • Useful to keep things simple • Squaring a number…

  9. Overloading Example int Square (int i) { return i * i; } float Square (float i) { return i * i; } static void Main() { ... float x = Square(5.3); int y = Square(9); ... }

  10. OperatorOverloading class BMW_Z4 { ... public BMW_Z4 () { Initialize(0, 2004, false); } public BMW_Z4 (int my) { Initialize(0, my, false); } private Initialize(int cs, int my, bool tu) { currentSpeed = cs; ModelYear = my; TopUp = tu; } ... } Notice same methodname, differentparameters Place commoninitialization in aseparate function

  11. Inheritance • Inheritance allows one class to take on the properties of another • Superclass-subclass relationship • Sometimes called parent-child relationship • Use the keyword extends to express this relationship • Subclass will “inherit” certain attributes and methods • Benefit: good design, reuse of code

  12. Class Hierarchy Mammal int weight giveBirth( ) LandMammal int numLegs Question: how many attributes does Dog have? Dog boolean rabid Chihuahua SheepDog

  13. Things to Note • LandMammal is a superclass to Dog, but a subclass to Mammal • Dog has three attributes • weight, numLegs and rabid • Two from inheritance, one it declared itself

  14. Visibility and Inheritance • Public – allows all to see • Private – allows only class in which defined to see • Protected – allows class and all subclasses that inherit to see • Consequently, we’ll now use protected instead of private by default… (common)

  15. C# Syntax class Person { ... } class Student : Person { ... } class Professor : Person { ... } Notice the use of “:”

  16. The Base Class: “Object” • All classes in C# inherit (sometimes implicitly) from Object • Includes common set: • ToString() • GetType() • Equals() • Often useful to override (implement) these virtual functions • “public override string ToString()…”

  17. FIN

More Related