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

Slides:



Advertisements
Similar presentations
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Advertisements

OOP Spring 2007 – Recitation 11 Object Oriented Programming Spring 2007 Recitation 1.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
Guide To UNIX Using Linux Third Edition
CS-2303 System Programming Concepts
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
Basic Elements of C++ Chapter 2.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
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.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
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.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
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.
Fundamental Programming: Fundamental Programming Introduction to C++
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Looping and Counting Lecture 3 Hartmut Kaiser
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object-Oriented Programming in C++ More examples of Association.
Fundamentals of C++ Yingcai Xiao 09/03/08. Outline Class Definition IO Template vector C Pointer Dynamic Memory Allocation.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Object-Oriented Programming Chapter Chapter
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
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.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
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
Introduction to Data Structure
Capitolo 1 – Introduction C++ Programming
Classes C++ representation of an object
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

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

OOP Etgar 2008 – Recitation 12 Administrative Details Course website: 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

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.

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.

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

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?

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

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.

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.

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.

OOP Etgar 2008 – Recitation 111 Switching to C++

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

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

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.

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.

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.

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

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

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

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.

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.

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.

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

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

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;

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

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

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

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

OOP Etgar 2008 – Recitation 130 Using Several Files

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.

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