Download presentation
Presentation is loading. Please wait.
1
OOP Spring 2007 – Recitation 11 Object Oriented Programming Spring 2007 Recitation 1
2
OOP Spring 2007 – Recitation 12 Administrative Details Course website: http://cs.haifa.ac.il/courses/oop_etgar Lecturer: Shlomo Berkovsky –Office hours: Teaching assistant: Liat Leventhal –Office hours:
3
OOP Spring 2007 – Recitation 13 What is OOP? OOP is a programming paradigm that focuses on objects and entities in the problem, rather than functions and algorithms. It is more natural for a human to think of a problem as a collection of objects that interact, than to divide the problem into functions that run consecutively.
4
OOP Spring 2007 – 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 Spring 2007 – Recitation 15 3 Main OOP Concepts Encapsulation Inheritance Polymorphism
6
OOP Spring 2007 – Recitation 16 Encapsulation We don’t really care how an object is implemented. –As long as a car works, we don’t care that it includes 4-cylinder engine, steel wheels, and 12V accumulator. We don’t care that the engine runs at 3,000 RPM and that the plugs make 700 ignitions per second. As long as cars exhibit some similar interface, we can drive them.
7
OOP Spring 2007 – Recitation 17 Encapsulation An object exposes to the world an interface, and we can use (without any change) any object that corresponds to that interface. All implementation details are hidden within the object, and don’t interest us.
8
OOP Spring 2007 – 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. Moreover, we certainly won’t “invent the wheel”, and design a land vehicle (that has steering wheel, engine, etc.) that is a car.
9
OOP Spring 2007 – 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 Spring 2007 – Recitation 110 There is no “ideal” design!
11
OOP Spring 2007 – Recitation 111 Polymorphism Knowing that ’83 Ford and ’06 BMW are both cars implies that they both accelerate when we press the gas pedal. But the BMW accelerates faster and the inner workings are different! This is called polymorphism – objects exhibit varying behavior, dependent on the exact type of the object.
12
OOP Spring 2007 – Recitation 112 Switching to C++
13
OOP Spring 2007 – Recitation 113 First C++ Program // First C++ Program #include int main() { std::cout << "Hello, world!\n"; return 0; }
14
OOP Spring 2007 – Recitation 114 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’.
15
OOP Spring 2007 – Recitation 115 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.
16
OOP Spring 2007 – Recitation 116 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.
17
OOP Spring 2007 – Recitation 117 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.
18
OOP Spring 2007 – Recitation 118 But beware of writing C code instead of C++ code!
19
OOP Spring 2007 – Recitation 119 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; }
20
OOP Spring 2007 – Recitation 120 #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
21
OOP Spring 2007 – Recitation 121 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). The class has a public part, a private part, and protected part. Unless specified, the members are private. The same with struct, except members are public by default.
22
OOP Spring 2007 – 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 Spring 2007 – 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 Spring 2007 – 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 Spring 2007 – 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 Spring 2007 – 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 Spring 2007 – 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 Spring 2007 – 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 Spring 2007 – 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 Spring 2007 – Recitation 130 Using Several Files
31
OOP Spring 2007 – 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 Spring 2007 – 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.