Object Oriented Programming

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Structures Spring 2013Programming and Data Structure1.
Plab 2003 Lecture 41 C++ - Classes. Plab 2003 Lecture 42 Motivating Example Goal: Graphics package Handle drawing of different shapes Maintain list of.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Object Oriented Programming Elhanan Borenstein Lecture #6.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Object Oriented Programming Elhanan Borenstein Lecture #5.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Object Oriented Programming Elhanan Borenstein Lecture #7.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Yan Shi CS/SE 2630 Lecture Notes
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Classes C++ representation of an object
User-Written Functions
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
A First C++ Class – a Circle
Review: Two Programming Paradigms
Classes & Objects.
Introduction to Classes
Class: Special Topics Copy Constructors Static members Friends this
Student Book An Introduction
Lecture 4-7 Classes and Objects
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
6 Chapter Functions.
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Introduction to Classes and Objects
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
Functions Reasons Concepts Passing arguments to a function
Pointers, Dynamic Data, and Reference Types
Standard Version of Starting Out with C++, 4th Edition
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS1 Introduction to C++.
SPL – PS3 C++ Classes.
Presentation transcript:

Object Oriented Programming Lecture #2 Elhanan Borenstein borens@tau.ac.il copyrights © Elhanan Borenstein

Agenda Classes & Objects Data Members Pointers to Objects Definition & Motivation Data Members Data Members vs. Function variables Private & Public Permissions Objects as Data Members Pointers to Objects Methods (Member Functions) Member Functions vs. Global Functions The “this” Pointer Working with Files Object Size and Local Objects copyrights © Elhanan Borenstein

Classes & Objects copyrights © Elhanan Borenstein

Classes & Objects Introduction A class represents a data entity. Classes could (and should) be regards as new types, just like the fundamental types: int, float, char, etc. Each class defines: Data Members - The data we want to store. Methods (member functions) - The operations that could be applied to this data. The application will define/create various Objects according to the available classes. It will then be able to assign values to its data members and activate its methods. copyrights © Elhanan Borenstein

Classes & Objects Definition A class is defined (declared) and used as follows: class MyClass { [private:] variables (data members) … functions (methods) … public: variables (data members) … functions (methods) … }; void main() { // define objects of type // class_name MyClass MyObject1; MyClass MyObject2; // call a member function MyObject1.func1(…); // assign value to data members MyObject1.Index = 12; } copyrights © Elhanan Borenstein

Classes & Objects Example – CPoint The class CPoint represents a point in the 2D space… class CPoint { int m_x , m_y; public: void Init() m_x = 0; m_y = 0; } void Set(int ax, int ay) m_x = ax; m_y = ay; void Print() cout<<"x = "<<m_x<<", y = "<<m_y<<endl; }; #include <iostream.h> void main() { CPoint p1, p2; p1.Init(); p2.Set(4,6); p1.Print(); p2.Print(); } copyrights © Elhanan Borenstein

Data Members Data Members vs. Function variables Member functions “know” all the data members of the same object. When activating a member function of an object, it can operate on the data member of that object. Unlike local variables (that “lives” only until the function terminates) the data members are alive as long as the object is alive. copyrights © Elhanan Borenstein

Data Members Private and Public Permissions Members (data & functions) can have either a public or a private permission. Public members can be accessed at any point in the code (if you have access to the object they are in). Private members can be accessed only by member functions of that class. The permissions can be set using the keywords “public:” and “private:” and will apply until the next occurrence of public:/private: . The default permission of a class is private. Permissions can be altered as many time as we want. copyrights © Elhanan Borenstein

Data Members Why Private? Prohibiting direct access to the data members by a programmer using the class. But why? (encapsulation, modularity, safety) Example : CPoint2 Rule: There should be a good reason to define data member as public. There is usually no such reason!!!! So, how can we access private data members? Get and Set functions… ByRef return value… copyrights © Elhanan Borenstein

Classes vs. Structs Formal In Practice A struct is a class whose default permission is public (reminder: the default permission in a class is private). Thus: struct { private: … } is equivalent to class { …} class { public: …} is equivalent to struct { … } In Practice The use of structs in C++ is scarce. They will be mainly used to define data collections with no methods (very similar to their usage in C). copyrights © Elhanan Borenstein

Objects as Data Members Data members can also be: Objects Array of objects Example: Rectangle Pointers to objects Example: Teachers and Courses copyrights © Elhanan Borenstein

Objects as Function arguments Note: When sending an object as a function argument: It is preferred to send it ByRef (efficiency). If the function should not change the object – add “const”. (We actually did the same in C, sending pointers to structures instead of the struct itself) copyrights © Elhanan Borenstein

Pointers to Objects In an analogue manner to the definition of pointers to fundamental data type in C (i.e. int *pIndex), we can define pointers to objects (i.e. CPoint *pMyPoint). Important: Pointers to objects do not necessarily represent real objects. We should: Assign it with a pointer to an existing object, or… allocate a new object to this pointer. Example: pmain What about memory allocation and freeing of data members? copyrights © Elhanan Borenstein

Methods (Member Functions) Member Functions vs. Global Functions Member functions (methods) represents the services each class offers. they operate on the data members of the object and can be access only through the object. encapsulation modularity Global functions will be used for general purpose operations that can not be assigned to any specific class. examples? A note about function names… copyrights © Elhanan Borenstein

Methods (Member Functions) Using the pointer “this” Inside a member function we operate on a specific object (the one which activated the function), although, we do not know its name. (unlike a global function) The method can use the pointer “this”, which points to the operating object. Sending the object to a different function… Returning the object as return value… Example: this copyrights © Elhanan Borenstein

Methods (Member Functions) External Function Implementation Functions that are being implemented inside the class definition are automatically defined as inline functions. Loops or otherwise complicated functions are not appropriate. Inline function should be used only for simple functions Most functions should be implemented externally. Within the class definition we shall only include their prototypes. To indicate that a function implementation is a member function, we can use the scope resolution operator “::” . copyrights © Elhanan Borenstein

Methods (Member Functions) Working with Multiple Files Most classes will be implemented in two files: header file (.h) – class definition: data members and prototypes. code file (.cpp) – a collection of function implantations. cpoint.h cpoint.cpp main.cpp class CPoint { int m_x , m_y; public: void Init(); bool Set(int ax, int ay); void Print(); int GetX() { return m_x; } int GetY() { return m_y; } }; #include “cpoint.h” void CPoint::Init() { … } bool CPoint::Set(int ax, int ay) #include “cpoint.h” #include “crectangle.h” … void main() { CPoint P1; } class CPoint { int m_x , m_y; public: void Init(); bool Set(int ax, int ay); void Print(); int GetX() { return m_x; } int GetY() { return m_y; } }; #include “cpoint.h” void CPoint::Init() { … } bool CPoint::Set(int ax, int ay) class CPoint { int m_x , m_y; public: void Init(); bool Set(int ax, int ay); void Print(); int GetX() { return m_x; } int GetY() { return m_y; } }; #include “cpoint.h” void CPoint::Init() { … } bool CPoint::Set(int ax, int ay) copyrights © Elhanan Borenstein

A Few Notes about Objects Object Size The object size is the sum of all sizes of its data members. (member functions do not effect the object size). Why? The operator sizeof can be applied to both classes and objects: sizeof(CPoint)  sizeof(int + int) sizeof(P1)  the same sizeof(CRectangle)  sizeof(CPoint + CPoint + char) sizeof(R1)  the same copyrights © Elhanan Borenstein

A Few Notes about Objects Local Object Local objects (which were defined within a function), die when the function terminates (just like a local variable). Local objects can be used though as ByVal return-values (since a copy of it will be created on the stack). Global Objects Objects which are defined externally to the main function will be global objects (just like global variables). Global object will be deleted only after the main function terminates. copyrights © Elhanan Borenstein

Questions? copyrights © Elhanan Borenstein