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.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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++
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
What is UML? A modeling language standardized by the OMG (Object Management Group), and widely used in OO analysis and design A modeling language is a.
Lecture 9 Concepts of Programming Languages
ASP.NET Programming with C# and SQL Server First Edition
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
OOP Languages: Java vs C++
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.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Lecture 5 What is object-oriented programming OOP techniques How Windows Forms applications rely on OOP.
Introduction to Classes SWE 344 Internet Protocols & Client Server Programming.
Lecture 9 Polymorphism Richard Gesick.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Programming Pillars Introduction to Object- Oriented Programming.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
ILM Proprietary and Confidential -
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
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.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Classes, Interfaces and Packages
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
CIS162AD Constructors Part 2 08_constructors.ppt.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Learners Support Publications Constructors and Destructors.
Constructors and Destructors
Creating and Using Objects, Exceptions, Strings
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2014
Static data members Constructors and Destructors
2.7 Inheritance Types of inheritance
Lecture 9 Concepts of Programming Languages
Inheritance Basics Programming with Inheritance
Java Programming Language
Polymorphism Polymorphism
Constructors and Destructors
Computer Programming with JAVA
Class.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

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 an object!!! Statements such as these are not all that helpful when we are struggling to understand the basic concepts of object-oriented programming (OOP). It is better to look as some examples of objects, such as a textbox, a push-button or some user-defined non-GUI object like a cardDeck. Each of these objects has properties that can be accessed and sometimes manipulated. Multiple instances can be defined for them, and there can be different characteristics for each instance. hello there...Start A programming object is a collection of data (variables or literal constants) and a set of functions that operate on the data. Programming objects give us the illusion of material objects and can be used to simplify the task of program design.

Life Cycle of an Object Every object has a clearly defined life cycle. Apart from the normal state of "being in use," this life cycle includes two important stages: Construction: When an object is first instantiated it needs to be initialized. This initializaztion is known as construction and is carried out by a constructor function. Destruction: When an object is destroyed, there are often some clean-up tasks to perform, such as freeing memory. This is the job of a destructor function. Note: The actions of the destructor function may not occur immediately, so we cannot rely on this function to release memory when we need it. Later we will look into the use of disposable objects to help manage critical resources.

Class Members The methods (functions) and data in a class are called class members. Data Members - Data members are instance members unless they are explicitly designated as static. Static data values are associated with the class as a whole. Each instance of a class has its own copy of an instance data value. Data members can be fields, constants, and events. Function Members - Function members perform some operation on the data members of the class. Function members can be methods, properties, constructors, finalizers, operators, and indexers.

Methods Are the standard form for functions and procedures. They may have zero or more arguments, they can be either instance or static, and they may or may not return a data value. public static void SomeMethod() { // stuff } public static int SomeOther() { return // some integer } public int YetAnother(int val) { // so something with val return //some integer } public void OneMore(ref int val) { // change the value of val }

Properties & Fields These are sets of functions that can be accessed from the client through dot-notation. Properties can do the same type of things that methods can do, except they have their own syntax (e.g. get and set). public string SomeProperty { get { return // the value of the property } set { //set the property } public string SomeOtherProperty { get { return // the value }

Constructors public class TheClassName { public TheClassName { // stuff to do the construction } // other stuff } public class AnExample { public AnExample() { // default constructor stuff } public AnExample(int num) { // constructor stuff that uses num } The constructor has the same name as the class and it initializes data or does whatever is needed to be done when a class instance is created. CupOfCoffee myCup = new CupOfCoffee( ); CupOfCoffee myCup = new CupOfCoffee("Maxwell House - Original",false,true); CupOfCoffee(string coffeebrand, bool cream, bool sugar)

Finalizers (Destructors) The finalizer has the same name as the class (preceded by a tilde, ~) and it releases data or does whatever is needed to be done when a class instance is disposed. class SampleClass { ~SampleClass() { // destructor stuff } "Destructors are used by the.NET Framework to clean up after objects. In general, you don't have to provide code for a destructor method; instead, the default operation works for you." We cannot rely on the destructor to free up resources that are used by an object instance, as this may be a long time after the last time the object is used. Instead we can create Disposable Objects.

Disposable Objects = new ();... Using ( ) {... } using ( = new () ) or In both cases below, the variable will be usable within the using code block and will be disposed of automatically at the end (i.e. Dispose( ) is called when the code block finishes).

Operators Operators are infix functions usually defined by one or two symbols. Examples are =, +, >=, &&, and so on. In C# we can overload operators for user-defined data types. public double x,y,z; public Vec_3(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public static Vec_3 operator +(Vec_3 lhs, Vec_3 rhs) { Vec_3 result = new Vec_3(lhs); result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; } public Vec_3(Vec_3 rhs) { x = rhs.x; y = rhs.y; z = rhs.z; }

Indexers Indexers are used like index values to gain access into an array, except that they can refer to an object. Instead of creating a name, you use the this keyword, which refers the the current object. public somedatatype this[int index] { get { // do something return some_value; } set { //set a value in the class related to index }

Inheritance There are two types of inheritance, implementation and interface. We will review implementation inheritance first, since it is the more common form of inheritance. In implementation inheritance, a type derives from a base type, taking all the base type's member fields and functions. class ExampleClass : object { // stuff } class ExampleClass { // same stuff } these are the same

Virtual Methods Declaring a method as virtual allows it to be overidden by any class that is derived from the defining class. Virtual members cannot be private, as this would cause a paradox -- a member cannot be overridden by a derived class and yet be inaccessible from the derived class. class SomeBaseClass { public vitual void SomeVirtualMethod() { // method stuff }

Overridding a Virtual Method in a Derived Class class BaseGraphicsClass { public virtual void DrawLine() { } public virtual void DrawPoint() { } class NewDerivedClass : BaseGraphicsClass { public override void DrawPoint() { } Instances of NewDerivedClass have access to the DrawLine() method as well as the new version of the DrawPoint() method.

Unified Modeling Language (UML)

UML Representation of a Class

Virtual Members

Abstract Classes