OOP Spring 2007 – Recitation 11 Object Oriented Programming Spring 2007 Recitation 1.

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
OOP Etgar 2008 – Recitation 11 Object Oriented Programming Etgar 2008 Recitation 1.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
CS-2303 System Programming Concepts
C++ fundamentals.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Class ~ The essence of Object Oriented Programming.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 25P. 1Winter Quarter C++: I/O and Classes Lecture 25.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Object-Oriented Programming in C++ More examples of Association.
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:
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Introduction to Object Oriented Programming Chapter 10.
Object-Oriented Programming (OOP) and C++
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Introduction to C++ (Extensions to C)
Classes C++ representation of an object
Andy Wang Object Oriented Programming in C++ COP 3330
CS3340 – OOP and C++ L. Grewe.
C++ Classes & Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Introduction to Data Structure
Chapter 11: Inheritance and Composition
C++ Programming Lecture 3 C++ Basics – Part I
Capitolo 1 – Introduction C++ Programming
Classes C++ representation of an object
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

OOP Spring 2007 – Recitation 11 Object Oriented Programming Spring 2007 Recitation 1

OOP Spring 2007 – Recitation 12 Administrative Details Course website: Lecturer: Shlomo Berkovsky –Office hours: Teaching assistant: Liat Leventhal –Office hours:

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.

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.

OOP Spring 2007 – Recitation 15 3 Main OOP Concepts Encapsulation Inheritance Polymorphism

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.

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.

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.

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.

OOP Spring 2007 – Recitation 110 There is no “ideal” design!

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.

OOP Spring 2007 – Recitation 112 Switching to C++

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

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’.

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.

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.

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.

OOP Spring 2007 – Recitation 118 But beware of writing C code instead of C++ code!

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; }

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

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.

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.

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]; }

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().

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;

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[].

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

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);

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()).

OOP Spring 2007 – Recitation 130 Using Several Files

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.

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