Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Etgar 2008 – Recitation 11 Object Oriented Programming Etgar 2008 Recitation 1.

Similar presentations


Presentation on theme: "OOP Etgar 2008 – Recitation 11 Object Oriented Programming Etgar 2008 Recitation 1."— Presentation transcript:

1 OOP Etgar 2008 – Recitation 11 Object Oriented Programming Etgar 2008 Recitation 1

2 OOP Etgar 2008 – Recitation 12 Administrative Details Course website: http://cs.haifa.ac.il/courses/oop_etgar Lecturer: Liat Leventhal –Office hours: TBD Teaching assistant: Moran Lefler –Office hours: Modays 15:00-16:00, at Jacobs Building, 1 st floor, room 102

3 OOP Etgar 2008 – Recitation 13 Procedural vs. Object-Oriented languages We usually deal with code and data. In C, these are functions and structures. In C++, they are merged into an object. An object-oriented program will usually model the real world such that every entity which can send and receive messages is transformed into an object.

4 OOP Etgar 2008 – Recitation 14 What is OOP? We see objects around us. An object has a state (its properties) and behavior (what it can do). –A car has state – color, max speed, model and behavior – drive, turn left, stop. Objects can interact by creating, changing and using one another. –A car can use the road, carry passengers, fill gasoline.

5 OOP Etgar 2008 – Recitation 15 3 Main OOP Concepts Encapsulation Inheritance Polymorphism

6 OOP Etgar 2008 – Recitation 16 Encapsulation An object is a black box – and you should never need / want to peak inside… This is because it interacts by sending and receiving messages Why is this good?

7 OOP Etgar 2008 – Recitation 17 Encapsulation Some things are meant to be private while other can be made public, just like in real life If we want to share something, we usually make other people aware of that

8 OOP Etgar 2008 – Recitation 18 Inheritance When creating something new we rarely create it from scratch. Rather we rely on existing objects. We specialize them. –When planning a car with automatic gear box, we won’t create a completely new car. We’ll specialize existing car so that it uses an automatic gear box. We want to re-use as much code as possible.

9 OOP Etgar 2008 – Recitation 19 Inheritance The inheritance models the is-a relationship. –The car is a land vehicle. –The car with automatic gear box is a car. We’ll say that a car inherits from land vehicle, and a car with automatic gear box inherits from car. The last inheritance is a design decision – a car can include different kinds of gear boxes, or subtype (use inheritance) them.

10 OOP Etgar 2008 – Recitation 110 Polymorphism It doesn’t matter what kind of car you own. It can ride, and can take turns, and can be fueled etc. This is called polymorphism – objects exhibit varying behavior, dependent on the exact type of the object.

11 OOP Etgar 2008 – Recitation 111 Switching to C++

12 OOP Etgar 2008 – Recitation 112 First C++ Program // First C++ Program #include int main() { std::cout << "Hello, world!\n"; return 0; }

13 OOP Etgar 2008 – Recitation 113 Line by Line // First C++ Program One line comments are now possible. Old /* */ comments work too. #include Standard C headers are replaced by C++ headers. Notice the lack of ‘.h’.

14 OOP Etgar 2008 – Recitation 114 Line by Line int main() { C++ starts executing from function main() (just as C). std::cout << "Hello, world!\n"; The new (and better) way of outputting. std::cout is a standard output stream usually connected to the screen. We write to it using operator<<. Control characters are the same as in C.

15 OOP Etgar 2008 – Recitation 115 Line by Line return 0; A non-void function must return a value. The convention is that main() returns 0 if it ended without errors.

16 OOP Etgar 2008 – Recitation 116 C++ as an “extended” C C++ is designed to be almost backwards- compatible with C. Most correct C programs will compile with a C++ compiler. The syntax is the same – opening and closing braces {}, function names and calls, variable definitions, etc.

17 OOP Etgar 2008 – Recitation 117 But beware of writing C code instead of C++ code!

18 OOP Etgar 2008 – Recitation 118 Input/output #include using namespace std; int main() { int a; // a is an integer variable char s [100]; // s is a string of max 99 characters cout << "This is a sample program" << endl; cout << "Type your age : "; cin >> a; cout << "Type your name: "; cin >> s; cout << endl; cout << "Hello " << s << " you're " << a << " years old." << endl; cout << endl << endl << "Bye!" << endl; return 0; }

19 OOP Etgar 2008 – Recitation 119 #ifndef __STACK_H__ #define __STACK_H__ // A stack of ints that cannot hold 0s. class Stack { public: int Pop(); bool Push(int element); void SetSize(); void EmptyStack(); private: int* m_Contents; int m_Size; int m_TopIndex; }; #endif //__STACK_H__ Stack Implementation – Stack.h

20 OOP Etgar 2008 – Recitation 120 A Class A class is a “recipe” for creating objects. It describes the state of the object (inner variables – data members) and its behavior (functions that work with this object – member functions or methods). In other words: An object is defined via a class. Objects are individual instances of a class.

21 OOP Etgar 2008 – Recitation 121 Access Control The class has a public part, a private part, and protected part. Unless specified, the members are private.

22 OOP Etgar 2008 – Recitation 122 Access Control Everything in the public part can be used and accessed by anyone, e.g. any other function or object. Everything in the private part is accessible only by the object itself. The protected part will be described later. Usually all data members and functions that are of “no interest to the world” are private.

23 OOP Etgar 2008 – Recitation 123 Stack.cpp bool Stack::Push(int element) { if (element == 0) return false; if (m_TopIndex == m_Size) return false; m_Contents[m_TopIndex] = element; m_TopIndex++; return true; } int Stack::Pop() { if (m_TopIndex == 0) return 0; m_TopIndex--; return m_Contents[m_TopIndex]; }

24 OOP Etgar 2008 – Recitation 124 Scope To specify that a function or variable reference refers to a particular class, we use the :: scope operator – Stack::pop(). To specify that a function or variable reference refers to a particular object, we use the. member access operator – s.pop().

25 OOP Etgar 2008 – Recitation 125 Memory Allocation In C++, memory is allocated using the new operator: int* p = new int; An array is allocated using new[] operator: int *p_arr = new int[10]; Memory is freed with delete operator: delete p; An array is deleted with delete[] operator: delete[] p_arr;

26 OOP Etgar 2008 – Recitation 126 Memory Allocation new and delete call constructors and destructors, so don’t use malloc() and free(). Remember to match new with delete and new[] with delete[].

27 OOP Etgar 2008 – Recitation 127 #include #include "Stack.h" using namespace std; int main() { Stack s; int element =5; s.SetSize(10); s.Push(element); cout << "Please enter a number: "; cin >> element; s.Push(element); cout << "Stack contains " << s.Pop(); cout << " and " << s.Pop() << endl; s.EmptyStack(); return 0; } main.cpp

28 OOP Etgar 2008 – Recitation 128 Object Definition and Use We define an object just as we defined a built-in type: Stack s; Member access is done using. operator (just as with struct): s.Push(10);

29 OOP Etgar 2008 – Recitation 129 Miscellany using namespace std; A namespace is a collection of related data, functions, objects, etc. Everything in the standard library is in the namespace std. This line eliminates the need for preceding everything with std:: cin >> element; The new way of inputting. cin is the standard input stream, usually connected to the keyboard. No need for & and specifying format (as in scanf()).

30 OOP Etgar 2008 – Recitation 130 Using Several Files

31 OOP Etgar 2008 – Recitation 131 Using Several Files The program should be separated into several files, each containing a logical unit. Usually – each class implementation in a separate.cpp file, each class declaration in a separate.h file, plus a file with main(). Do not forget to #include the header files you need (i.e. do not forget to #include Stack.h when using Stack). Do not forget the #ifndef-#define construct in header files.

32 OOP Etgar 2008 – Recitation 132 Compiling Together Assume we have the following files in a project called ‘Stack’: –main.cpp, stack.cpp, stack.h Compiling with Visual Studio.NET: –Open a new empty Win32 Console Project. –Add all source files (both.cpp and.h) to it. –Run “Build Stack” from “Build” menu. Compiling with g++: g++ main.cpp Stack.cpp –o Stack.exe


Download ppt "OOP Etgar 2008 – Recitation 11 Object Oriented Programming Etgar 2008 Recitation 1."

Similar presentations


Ads by Google