BTP205 – Görsel Programlama I Object-Oriented Programming Nesne Tabanlı Proglamlama 1.

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Static Members, Structures, Enumerations, Generic Classes, Namespaces Learning & Development Team Telerik Software Academy.
Microsoft.NET Fundamentals. Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
ASP.NET Programming with C# and SQL Server First Edition
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 5 What is object-oriented programming OOP techniques How Windows Forms applications rely on OOP.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Module 7: Object-Oriented Programming in Visual Basic .NET
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Programming Pillars Introduction to Object- Oriented Programming.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
ILM Proprietary and Confidential -
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Programming in Java CSCI-2220 Object Oriented Programming.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Object-Oriented Programming Chapter Chapter
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
C# for C++ Programmers 1.
Creating Your Own Classes
Classes C++ representation of an object
Static data members Constructors and Destructors
2.7 Inheritance Types of inheritance
Module 5: Common Type System
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
OOP’S Concepts in C#.Net
Can perform actions and provide communication
Interfaces.
Can perform actions and provide communication
CIS 199 Final Review.
Classes C++ representation of an object
CS 240 – Advanced Programming Concepts
Presentation transcript:

BTP205 – Görsel Programlama I Object-Oriented Programming Nesne Tabanlı Proglamlama 1

Contents Object-Oriented Programming NESNE TABANLI PROGRAMLAMA (NTP) – Nesneler – Constructors, Destructors – NTP Teknikleri (Interfaces (Arayüz), Inheritance (Miras), Polymorphism, Operator Overloading (Operator üstüne yükleme, Events(Olaylar) 2

Object-Oriented Programming (OOP) Nesne Tabanlı Programlama 3

Contents What is OOP? (NTP nedir?) OOP Techniques (NTP Teknikleri) Using OOP in Console Application (Konsol Uygulamalarında NTP Kullanılması) 4

What is OOP? The type of programming we have covered so far is known as functional (or procedural) programming Şu ana kadar kullanılan programlama tipi fonksiyonel programlamadır. OOP is an approach to creating computer applications which uses objects NTP bilgisayar uygulamalarında nesne kullanan bir yaklaşımdır Code reuse is easier in OOP Kodların tekrar kullanılması NTP da kolaydır. We have already used some objects in our programs (e.g. Console, Exception, etc.) Programlarımızda bazı nesneleri zaten kullanıyorduk. 5

Objects-Nesneler An object has both member variables and methods Bir nesne hem üye değişken hem de metoda sahiptir. – structs in C contain only member variables – C’deki structlar sadece üye değişkeni içerir. Objects are instantiated from classes Objects are also called instances Nesneler aynı zamanda örnek olarak adlandırılırlar. The terms class and object are often confused, and it is important to understand the distinction Nesne ve Sınıf arasındaki farkı anlamanız önemlidir!! 6

Class vs. Object Class Class is only a definition Sınıf bir tanımlamadır Classes are coded in source files Sınıflar kaynak kodlarında kodlanırlar. You write a class once Bir sınıf bir kez yazılır. Classes are abstract Sınıflar soyuttur Object Object is the implementation Nesneler uygulamalardır. Objects are created at runtime Nesneler çalışma zamanında yaratılır. You can create many objects from a class Bir sınıftan bir çok nesne yaratılabilir. Nesneler somuttur. 7

Class vs. Object 8

Everything is an object Herşey bir nesnedir Everything in C# and.NET Framework is an object! C# ve.NET programlarında herşey bir nesnedir. even the int variables are objects too! int değişkenleri bile nesnelerdir! 9

Object Members Properties-Özellikler – Data contained in an object – Veri nesnenin içinde barınır. – Determine the state of the object – Nesnenin durumunu belirle. – They may be read-only, write-only, or both readable and writable – Sadece Okunabilir, Sadece Yazılabilir yada her ikisi de.. – Example: Columbian filter coffee with milk and two sugars – Columbian filtre kahve süt ve şekerle Methods-Methodlar – Functions of objects are called methods – Nesnelerin fonksiyonları method olarak adlandırılır. – Example: AddSugar(), Mix(), etc. 10

Visibility-Görünürlük public – Public variables or methods are accessible by other objects – Umumi değişkenler ya da methodlar diğer nesneler tarafından erişilebilir. private – Private variables or methods are accessible only by the methods of the object itself – Özel değişkenler ya da methodlar sadece nesnelerin methodları tarafından erişilebilir. 11

The Life Cycle of an Object- Bir nesnenin hayat döngüsü Construction – Initialization of the object – Bir nesnenin yaratılması – Implemented by constructors – Constructors tarafından gerçeklenmesi – There may be several constructors – Bir çok constructor olabilir. – The code snippet ctor can be used to create a constructor in Visual Studio – Kod parçacığı ctor Visual Studio’da bir constructor yaratmak için kullanılır. Destruction – Resources used by the object are freed – Kaynaklar nesneler tarafından serbest bırakılır. – Implemented by a destructor function – Destructor fonksiyonu tarafından gerçeklenmesi – Each class may have only one destructor function – Her sınıf sadece bir desructor fonksiyonuna sahip olabilir. 12

Construction CupOfCoffee myCup = new CupOfCoffee(); CupOfCoffee myCup = new CupOfCoffee(“Columbian”); CupOfCoffee myCup = new CupOfCoffee(“Columbian”, true, true, 2); 13 Source Filtered?Milk? Sugar?

Constructor Syntax class MyClass { public MyClass() { // Default constructor code } public MyClass(int myInt) { // Non-default constructor code } 14

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

Destructor Syntax class MyClass { ~MyClass() { // Destructor body } Use the Finalize() method to call the destructor 16

Static and Instance Class Members Static Members- Statik Üyeler – Shared between all instances of a class – Bir sınıfın tüm örnekleri arasında paylaşılır. – You don’t need to instantiate an object to use a static member – Bir statik üyeyi kullanmak için bir nesneyi örneklendirmeye ihtiyaç yoktur. – 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

OOP Techniques Interfaces Inheritance Polymorphism Operator Overloading Events 18

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

Interface Syntax public interface IMyInterface { … } public class MyClass : IMyInterface { … } 20

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

Implementing an Interface in VS 22

Solution (Page 1) enum CarBrand { Mercedes, BMW, Honda, Toyota, Volkswagen, Mazda } class Car : IComparable { // 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

Solution (Page 2) class Program { static void Main(string[] args) { Car[] cars = new Car[] { new Car(CarBrand.Volkswagen, 70000), new Car(CarBrand.Mercedes, ), new Car(CarBrand.Honda, 65000) }; Array.Sort(cars); Console.WriteLine("Cars sorted in price:\n"); foreach (Car car in cars) { car.Display(); } 24

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

Inheritance Syntax public class MyClass : BaseClass { … } class MyClass : BaseClass, Interface1, Interface2 { … } 26

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

Virtual Members Virtual members of the base class can be overridden by the derived class 28

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

Sealed Classes A sealed class may not be used as a base class, so no derived classes are possible 30

Polymorphism Using the same method with different implementations in base and derived classes is called polymorphism Example: ToString() method of the Object class 31

Operator Overloading if (carA.Price > carB.Price) { … } You can use the following code if you overload the operators: if (carA > carB) { … } 32

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

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

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

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

Properties Syntax public int MyIntProp { get { // Property get code. } set { // Property set code. } 37

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

Read-Only Property Example private string FirstName; private string LastName; public string FullName { get { return FirstName + “ ” + LastName; } 39

.:. 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 ‘ ’ operators for the Curve class which compares the curve lengths 40