Download presentation
Presentation is loading. Please wait.
1
Object Oriented Programming
Lecture #2 Elhanan Borenstein copyrights © Elhanan Borenstein
2
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
3
Classes & Objects copyrights © Elhanan Borenstein
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
Questions? copyrights © Elhanan Borenstein
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.