BİL527 – Bilgisayar Programlama I
description
Transcript of BİL527 – Bilgisayar Programlama I
![Page 1: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/1.jpg)
BİL527 – Bilgisayar Programlama I
Object-Oriented Programming
1
![Page 2: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/2.jpg)
Contents
• Object-Oriented Programming– Objects– Constructors, Destructors– OOP Techniques (Interfaces, Inheritance,
Polymorphism, Operator Overloading, Events)
2
![Page 3: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/3.jpg)
Object-Oriented Programming (OOP)
3
![Page 4: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/4.jpg)
Contents
• What is OOP?• OOP Techniques• Using OOP in Console Application
4
![Page 5: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/5.jpg)
What is OOP?
• The type of programming we have covered so far is known as functional (or procedural) programming
• OOP is an approach to creating computer applications which uses objects
• Code reuse is easier in OOP• We have already used some objects in our
programs (e.g. Console, Exception, etc.)
5
![Page 6: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/6.jpg)
Objects
• An object has both member variables and methods– structs in C contain only member variables
• Objects are instantiated from classes• Objects are also called instances• The terms class and object are often
confused, and it is important to understand the distinction
6
![Page 7: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/7.jpg)
Class vs. Object
Class• Class is only a definition
• Classes are coded in source files
• You write a class once
• Classes are abstract
Object• Object is the
implementation• Objects are created at
runtime• You can create many objects
from a class• Objects are concrete
7
![Page 8: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/8.jpg)
Class vs. Object
8
![Page 9: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/9.jpg)
Everything is an object
• Everything in C# and .NET Framework is an object!
• even the int variables are objects too!
9
![Page 10: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/10.jpg)
Object Members
• Properties– Data contained in an object– Determine the state of the object– They may be read-only, write-only, or both readable
and writable– Example: Columbian filter coffee with milk and two
sugars• Methods– Functions of objects are called methods– Example: AddSugar(), Mix(), etc.
10
![Page 11: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/11.jpg)
Visibility
• public– Public variables or methods are accessible by
other objects• private– Private variables or methods are accessible only
by the methods of the object itself
11
![Page 12: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/12.jpg)
The Life Cycle of an Object
• Construction– Initialization of the object– Implemented by constructors– There may be several constructors– The code snippet ctor can be used to create a
constructor in Visual Studio• Destruction– Resources used by the object are freed– Implemented by a destructor function– Each class may have only one destructor function
12
![Page 13: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/13.jpg)
Construction
• CupOfCoffee myCup = new CupOfCoffee();• CupOfCoffee myCup = new
CupOfCoffee(“Columbian”);• CupOfCoffee myCup = new
CupOfCoffee(“Columbian”, true, true, 2);
13
Source Filtered? Milk? Sugar?
![Page 14: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/14.jpg)
Constructor Syntax
class MyClass{ public MyClass() { // Default constructor code } public MyClass(int myInt) { // Non-default constructor code }}
14
![Page 15: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/15.jpg)
Destruction
• You can make some extra operations when an object is about to be destroyed– e.g. Saving the object data into a file
• Generally the default destructor does all the work for you and you don’t need to write a destructor
15
![Page 16: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/16.jpg)
Destructor Syntax
class MyClass{ ~MyClass() { // Destructor body }}• Use the Finalize() method to call the destructor
16
![Page 17: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/17.jpg)
Static and Instance Class Members
• Static Members– Shared between all instances of a class– You don’t need to instantiate an object to use a static member– You can access static member with the class name– Examples: Math.Sin(), Main(), Console.WriteLine(), int.Parse(),
Convert.ToDouble(), etc.– static methods can access only the static members of a class
• Instance Members– All objects have separate instance members– Instance members require an instance to be used– You can access instance members with the name of the objects– Examples: Length() (of strings and arrays), ToString(), etc.
17
![Page 18: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/18.jpg)
OOP Techniques
• Interfaces• Inheritance• Polymorphism• Operator Overloading• Events
18
![Page 19: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/19.jpg)
Interfaces
• An interface is a collection of methods and properties that are grouped together to encapsulate specific functionality
• Interfaces are only some definitions, and they should be implemented in classes– i.e. the class supports all functionality defined in the interface
• You can’t instantiate an interface• Interfaces cannot contain any code• Interface names generally start with ‘I’• A class may implement more than one interfaces
19
![Page 20: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/20.jpg)
Interface Syntax
public interface IMyInterface{ …}public class MyClass : IMyInterface{ …}
20
![Page 21: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/21.jpg)
Exercise: Sorting Cars
• Create an enumeration of ‘CarBrands’• Create a class ‘Car’• Add two members, ‘Brand’ and ‘Price’ to ‘Car’ class definition• Create a constructor (use ‘ctor’ code snippet)• Create a ‘Display()’ method in ‘Car’ class• Create an array of cars in Main• Change ‘Car’ definition so that it implements IComparable
interface– IComparable objects can be sorted
• Implement the CompareTo() method• Sort the cars in Main
21
![Page 22: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/22.jpg)
Implementing an Interface in VS
22
![Page 23: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/23.jpg)
Solution (Page 1) enum CarBrand { Mercedes, BMW, Honda, Toyota, Volkswagen, Mazda } class Car : IComparable<Car> { // Properties: CarBrand Brand; decimal Price;
// Constructor: public Car(CarBrand brand, decimal price) { this.Brand = brand; this.Price = price; }
// Methods public void Display() { Console.WriteLine(this.Brand + " - " + this.Price + " TL."); }
public int CompareTo(Car other) { return (int)this.Price - (int)other.Price; } }
23
![Page 24: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/24.jpg)
Solution (Page 2)class Program { static void Main(string[] args) { Car[] cars = new Car[] { new Car(CarBrand.Volkswagen, 70000), new Car(CarBrand.Mercedes, 100000), new Car(CarBrand.Honda, 65000) };
Array.Sort(cars);
Console.WriteLine("Cars sorted in price:\n"); foreach (Car car in cars) { car.Display(); } } }
24
![Page 25: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/25.jpg)
Inheritance
• Any class may inherit from another• Inherited class will have all members of base
class• Classes in C# may derive only from a single
base class directly (No multiple-inheritance)• Interfaces may inherit from other interfaces
(maybe multiple)• Syntax: class InheritedClass : BaseClass { … }
25
![Page 26: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/26.jpg)
Inheritance Syntax
public class MyClass : BaseClass{ …}class MyClass : BaseClass, Interface1, Interface2{ …}
26
![Page 27: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/27.jpg)
Visibility in Inheritance
• Public members of the base class are accessible from the derived class
• Private members of the base class are not accessible from the derived class
• Protected members of the base class are accessible from the derived class but not accessible from other classes
27
![Page 28: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/28.jpg)
Virtual Members
• Virtual members of the base class can be overridden by the derived class
28
![Page 29: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/29.jpg)
Abstract Classes
• Abstract classes can’t be instantiated directly• You have to derive another class from the
abstract class and then the derived class can be instantiated
• Abstract classes may have abstract members, which have no implementation in the base class, so an implementation must be supplied in the derived class
29
![Page 30: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/30.jpg)
Sealed Classes
• A sealed class may not be used as a base class, so no derived classes are possible
30
![Page 31: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/31.jpg)
Polymorphism
• Using the same method with different implementations in base and derived classes is called polymorphism
• Example: ToString() method of the Object class
31
![Page 32: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/32.jpg)
Operator Overloading
if (carA.Price > carB.Price) {…}• You can use the following code if you overload
the < and > operators:if (carA > carB) {…}
32
![Page 33: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/33.jpg)
Events
• When you click a button or move the mouse in a Windows program, events are raised
• When an event is raised, an event handler method is executed
• You can add custom events into your own classes
33
![Page 34: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/34.jpg)
Reference Types vs. Simple Types
• Simple Types– int, float, double, etc.– string– object– struct
• Reference Types: Contents are stored in a separate memory location (heap)– Classes (Objects created with the new keyword)– Arrays
34
![Page 35: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/35.jpg)
Shallow Copying vs. Deep Copying
• Shallow Copying– When you make a copy of an object, value types are
copied correctly but reference types point to the previous copies
– Accomplished by MemberwiseClone method of the object class• MemberwiseClone() is a protected member of the object
class
• Deep Copying– Reference types are copied too– Implement the ICloneable interface
35
![Page 36: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/36.jpg)
Properties
• You can’t make range-check on public members– Age of a person can only be positive– If negative values are assigned, program may crash
• By using properties, you can check values before they are assigned
• You can create read-only or write-only properties
36
![Page 37: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/37.jpg)
Properties Syntax
public int MyIntProp{ get { // Property get code. } set { // Property set code. }}
37
![Page 38: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/38.jpg)
Properties Example
private int m_Age;public int Age{ get { return m_Age; } set { if (value < 0) m_Age = 0; else m_Age = value; }}
38
![Page 39: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/39.jpg)
Read-Only Property Example
private string FirstName;private string LastName;public string FullName{ get { return FirstName + “ ” + LastName; }}
39
![Page 40: BİL527 – Bilgisayar Programlama I](https://reader030.fdocuments.net/reader030/viewer/2022012905/56814efd550346895dbc8ba4/html5/thumbnails/40.jpg)
.:. Application .:.
• Create a Curve class which stores a list of points• Create a Point class to be used in the Curve class• Add PrintPoints() and AddPoint() methods into the Curve
class• Make the Curve class cloneable
– try shallow and deep copies• Create a property named CurveLength• Overload the ‘–’ operator so that Point1 – Point2 gives the
distance between them• Overload the ‘<’ and ‘>’ operators for the Curve class which
compares the curve lengths40